Function

From Mario Fan Games Galaxy Wiki
(Redirected from Functions)
 Stub.png This article or section is in need of expansion.

Please add further information.

Functions are the main backbone of programs; they are what make an application actually do something. In many cases, a function takes a set of parameters and "transforms" itself into a set of one or more return values. However, neither a parameter set nor a return value are required in some languages.

Generally, functions are recognized by a name followed by parenthesis, such as main(), but this is language-dependent. In pseudocode, a function to calculate distance between two points might look like:

distance(x1, y1, x2, y2)
{
    dx = x2 - x1;
    dy = y2 - y1;
    return sqrt(dx * dx + dy * dy)
}

A method is another type of function, but it carries a reference to the particular object instance that called it, crucial for object-oriented languages. Keywords that refer to the calling object are commonly this or self. Methods are sometimes called via a different calling syntax than a regular function (for example, Lua uses var.function() for regular calls and var:method() for method calls).

In some languages, functions can be treated as though they were normal variables, allowing for more flexibility in some situations.