Difference between revisions of "Function"
(Created page with "<c>function</c> is a keyword used to define a subprogram in MiniScript. The term "function" can also refer to such a subprogram. Category:Language...") |
|||
Line 1: | Line 1: | ||
<c>function</c> is a [[:Category:Keywords|keyword]] used to define a subprogram in MiniScript. The term "function" can also refer to such a subprogram. | <c>function</c> is a [[:Category:Keywords|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: | ||
+ | |||
+ | <ms>identifier = function(param1, param2, param3) | ||
+ | // function body | ||
+ | return 42; | ||
+ | end function</ms> | ||
+ | |||
+ | Parameters may have default values. If not otherwise specified, the default value of a parameter is [[null]]. | ||
+ | |||
+ | <ms>identifier = function(param1=1, param2="foo", param3) | ||
+ | // function body | ||
+ | return 42; | ||
+ | end function</ms> | ||
+ | |||
+ | If the function takes no parameters, then the declaration would look like this: | ||
+ | |||
+ | <ms>identifier = function | ||
+ | // function body | ||
+ | return 42; | ||
+ | end function</ms> | ||
+ | |||
+ | Unlike some other languages, functions in MiniScript do not have names. The <c>function... end function</c> 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. | ||
+ | |||
+ | <ms>someFunc | ||
+ | someFunc arg1, arg2 | ||
+ | x = someFunc | ||
+ | y = someFunc(arg1, arg2)</ms> | ||
+ | |||
+ | == 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: <c>f = @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"]