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...") |
(remove semicolons) |
||
(5 intermediate revisions by 2 users not shown) | |||
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]]. Default values must be literals (not expressions, variables, function calls, etc.). | ||
+ | |||
+ | <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_References|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 <c>@</c> operator before the function identifier. This (often read "address of") operator blocks the usual function invocation. Example: <c>f = @someFunc</c> | ||
+ | # Fetch the value out of a map using square-brackets indexing rather than dot syntax. Example: <c>f = locals["someFunc"]</c> | ||
+ | |||
+ | A function reference has the type [[FuncRef|funcRef]]. | ||
[[Category:Language]] | [[Category:Language]] | ||
[[Category:Keywords]] | [[Category:Keywords]] | ||
− | |||
− |
Latest revision as of 15:36, 27 April 2023
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. Default values must be literals (not expressions, variables, function calls, etc.).
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
- Fetch the value out of a map using square-brackets indexing rather than dot syntax. Example:
f = locals["someFunc"]
A function reference has the type funcRef.