Difference between revisions of "Function"

From MiniScript Wiki
Jump to navigation Jump to search
(Link to section)
(remove semicolons)
 
Line 7: Line 7:
 
<ms>identifier = function(param1, param2, param3)
 
<ms>identifier = function(param1, param2, param3)
 
     // function body
 
     // function body
     return 42;
+
     return 42
 
end function</ms>
 
end function</ms>
  
Parameters may have default values.  If not otherwise specified, the default value of a parameter is [[null]].
+
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)
 
<ms>identifier = function(param1=1, param2="foo", param3)
 
     // function body
 
     // function body
     return 42;
+
     return 42
 
end function</ms>
 
end function</ms>
  
Line 21: Line 21:
 
<ms>identifier = function
 
<ms>identifier = function
 
     // function body
 
     // function body
     return 42;
+
     return 42
 
end function</ms>
 
end function</ms>
  

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:

  1. Use the @ operator before the function identifier. This (often read "address of") operator blocks the usual function invocation. Example: f = @someFunc
  2. 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.