Function definition: Difference between revisions
Content added Content deleted
Langurmonkey (talk | contribs) |
Langurmonkey (talk | contribs) |
||
Line 2,150: | Line 2,150: | ||
=={{header|langur}}== |
=={{header|langur}}== |
||
⚫ | |||
⚫ | |||
A return statement may be used, but a function's last value is its implicit return value. |
A return statement may be used, but a function's last value is its implicit return value. |
||
⚫ | |||
Functions defined with explicit parameters may be closures, and those defined with implied parameters are not. |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
.multiply(3, 4)</syntaxhighlight> |
.multiply(3, 4)</syntaxhighlight> |
||
=== |
=== curly braces === |
||
⚫ | |||
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 = |
<syntaxhighlight lang="langur">val .multiply = fn(.x, .y) .x * .y |
||
.multiply(3, 4)</syntaxhighlight> |
.multiply(3, 4)</syntaxhighlight> |
||
=== operator implied functions === |
=== operator implied functions === |
||
Operator implied functions are built using an infix operator between curly braces on an |
Operator implied functions are built using an infix operator between curly braces on an fn token. |
||
<syntaxhighlight lang="langur">val .multiply = |
<syntaxhighlight lang="langur">val .multiply = fn{*} |
||
.multiply(3, 4)</syntaxhighlight> |
.multiply(3, 4)</syntaxhighlight> |
||
=== nil left partially implied functions === |
=== nil left partially implied functions === |
||
These are built with an infix operator and |
These are built with an infix operator and a right-hand operand inside the fn{...} tokens. |
||
<syntaxhighlight lang="langur">val .times3 = |
<syntaxhighlight lang="langur">val .times3 = fn{* 3} |
||
map .times3, [1, 2, 3]</syntaxhighlight> |
map .times3, [1, 2, 3]</syntaxhighlight> |
||
=== impure functions (I/O) === |
=== impure functions (I/O) === |
||
Impure functions must be declared as such. |
Impure functions must be declared as such. |
||
<syntaxhighlight>val .writeit = impure |
<syntaxhighlight>val .writeit = impure fn(.x) { writeln .x }</syntaxhighlight> |
||
Impure functions cannot be passed to pure functions. |
Impure functions cannot be passed to pure functions. |