Function

From Mario Fan Games Galaxy Wiki
Revision as of 20:27, 27 November 2008 by Xgoff (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
 Stub.png This article or section is in need of expansion.

Please add further information.

A function is a piece of code that runs only when it is called. In fact, most if not all code in a program is contained inside functions, with code that runs continuously contained in a special function; this function is usually called 'main'. However, some languages, such as Lua, do not have a 'main' function and rely on the host program to run the functions.

Functions are generally formatted like functionName(), though this is dependent on the actual language. Functions can take parameters (usually inside parenthesis), which are used by the function to perform tasks; afterward the function may return a value. In pseudocode, this might look like:

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

When a function returns, it's essentially "replaced" by its return value in the expression. Some variable assignments use functions to calculate a certain value to assign, by using this property.