Difference between revisions of "Function"
Line 41: | Line 41: | ||
Sometimes you want to get a function reference ''without'' actually invoking the function. This can be done in two ways: | Sometimes you want to get a function reference ''without'' actually invoking the function. This can be done in two ways: | ||
− | # Use the | + | # Use the <c>@</c> operator before the function identifier. This (often read "address of") operator blocks the usual function invocation. Example: <c>f = @someFunc</c> |
# Fet the value of a map using square-brackets indexing rather than dot syntax. Example: <c>f = locals["someFunc"]</c> | # Fet the value of a map using square-brackets indexing rather than dot syntax. Example: <c>f = locals["someFunc"]</c> | ||
[[Category:Language]] | [[Category:Language]] | ||
[[Category:Keywords]] | [[Category:Keywords]] |
Revision as of 03:32, 25 January 2022
function
is a keyword used to define a subprogram in MiniScript. The term "function" can also refer to such a subprogram.
Defining a Function
A typical function definition looks something like this:
identifier = function(param1, param2, param3)
// function body
return 42;
end function
Parameters may have default values. If not otherwise specified, the default value of a parameter is null.
identifier = function(param1=1, param2="foo", param3)
// function body
return 42;
end function
If the function takes no parameters, then the declaration would look like this:
identifier = function
// function body
return 42;
end function
Unlike some other languages, functions in MiniScript do not have names. The function... end function
structure returns a function reference; typically this is assigned to some variable, and then you can call the function via that variable.
Calling a Function
Any time an identifier (with or without dot syntax) that contains a function reference is evaluated, the function is automatically invoked. Parentheses are needed around any arguments to the function if the call is part of a larger expression (rather than a command statement). If no arguments are to be passed to the function, then no parentheses are ever needed.
The following are all valid function calls.
someFunc
someFunc arg1, arg2
x = someFunc
y = someFunc(arg1, arg2)
Function References
Sometimes you want to get a function reference without actually invoking the function. This can be done in two ways:
- Use the
@
operator before the function identifier. This (often read "address of") operator blocks the usual function invocation. Example:f = @someFunc
- Fet the value of a map using square-brackets indexing rather than dot syntax. Example:
f = locals["someFunc"]