Call a function: Difference between revisions

Content added Content deleted
Line 3,668: Line 3,668:


=={{header|langur}}==
=={{header|langur}}==
There are 3 ways to call a function in langur.
User-defined and built-in functions can be called using parentheses.

Built-in functions can also be called using an "unbounded list," which ends at a line return, EOF, closing parenthesis, curly brace, or square bracket, or before a comma preceding a line return.


=== parentheses ===
=== parentheses ===
You can always call a function using parentheses.
<syntaxhighlight lang="langur">.x()
# call user-defined function</syntaxhighlight>
<syntaxhighlight lang="langur">x()/syntaxhighlight>

<syntaxhighlight lang="langur">write(.key, ": ", .value)
# call built-in with parentheses</syntaxhighlight>


<syntaxhighlight lang="langur">write(somestring)/syntaxhighlight>
=== unbounded lists ===
<syntaxhighlight lang="langur">write .key, ": ", .value
# call built-in with unbounded list</syntaxhighlight>


=== unbounded argument lists ===
<syntaxhighlight lang="langur">writeln "numbers: ", join ", ", [.a1, .a2, .a3, .a4]
In statement context, you can call a function with an unbounded list of arguments.
# unbounded lists on writeln and join
# later function join takes remaining arguments</syntaxhighlight>


<syntaxhighlight lang="langur">writeln "numbers: ", join(", ", [.a1, .a2, .a3, .a4]), " === "
<syntaxhighlight lang="langur">writeln a, b, c</syntaxhighlight>
# unbounded list on writeln
# join using parentheses so it doesn't take remaining arguments</syntaxhighlight>


=== forwarding operator ===
<syntaxhighlight lang="langur">val .sum = foldfrom(
When passing a single argument, you can use the forwarding operator.
fn(.sum, .i, .c) { .sum + number(.c, 36) * .weight[.i] },
0,
pseries len .code,
split .code,
)
# split, pseries, and len using unbounded lists, ending before comma preceding line return</syntaxhighlight>


<syntaxhighlight lang="langur">for .key in sort(keys .tests) {
<syntaxhighlight lang="langur">a -> len
# passes a to the len() function</syntaxhighlight>
...
}
# unbounded list on keys bounded by closing parenthesis of sort</syntaxhighlight>


=={{header|Latitude}}==
=={{header|Latitude}}==