Function definition: Difference between revisions

Line 2,150:
 
=={{header|langur}}==
Langur functions are first-order. They are pure in terms of setting values and in terms of I/O (unless declared impure).
A function body may use curly braces, but it is not required if it is a single expression.
 
A return statement may be used, but a function's last value is its implicit return value.
 
=== explicit parameters ===
Functions defined with explicit parameters may be closures, and those defined with implied parameters are not.
Explicit parametersParameters are defined withwithin parentheses after the ffn token, with no spacing. To specify no parameters, use an empty set of parentheses.
 
<syntaxhighlight lang="langur">val .multiply = ffn(.x, .y) { .x x* .y }
Langur functions are first-order. They are pure in terms of setting values and in terms of I/O (unless declared impure).
 
=== explicit parameters ===
Explicit parameters are defined with parentheses after the f token, with no spacing. To specify no parameters, use an empty set of parentheses.
<syntaxhighlight lang="langur">val .multiply = f(.x, .y) .x x .y
.multiply(3, 4)</syntaxhighlight>
 
=== impliedcurly parametersbraces ===
A function body may use curly braces, but it is not required if it is a single expression.
Parameters are implied when the f token is not immediately followed by parentheses without spacing. The implied order of implied parameters is based on the string sort order of their names, not their order within the function.
<syntaxhighlight lang="langur">val .multiply = f fn(.x, .y) .x * .y
.multiply(3, 4)</syntaxhighlight>
 
=== operator implied functions ===
Operator implied functions are built using an infix operator between curly braces on an ffn token.
 
<syntaxhighlight lang="langur">val .multiply = ffn{x*}
.multiply(3, 4)</syntaxhighlight>
 
=== nil left partially implied functions ===
These are built with an infix operator and onea right-hand operand inside the ffn{...} tokens.
 
<syntaxhighlight lang="langur">val .times3 = ffn{x* 3}
map .times3, [1, 2, 3]</syntaxhighlight>
 
=== impure functions (I/O) ===
Impure functions must be declared as such.
<syntaxhighlight>val .writeit = impure ffn(.x) { writeln .x }</syntaxhighlight>
 
Impure functions cannot be passed to pure functions.
885

edits