Call a function: Difference between revisions
Added Bracmat example
m (→{{header|J}}) |
(Added Bracmat example) |
||
Line 193:
goto :eof
</lang>
=={{header|Bracmat}}==
* Calling a function that requires no arguments:
Strictly speaking, all Bracmat functions receive at least one argument. But empty strings are valid expressions, so you can do
<lang bracmat>aFunctionWithoutArguments$</lang>
or
<lang bracmat>aFunctionWithoutArguments'</lang>
Both function calls pass the right and side of the <code>$</code> or <code>'</code> operator. This is in fact still something: an empty string.
The <code>$</code> operator always evaluates its right hand side before passing it to the function, while the <code>'</code> does not. Therefore it is slightly faster to use the <code>functionName'</code> variant.
* Calling a function with a fixed number of arguments:
Bracmat passes exactly one argument to a function, called <code>arg</code>. The argument can be any Bracmat expression. In patterns, if a function call expression is a pattern component, a second argument <code>sjt</code> is added, the part of the subject that the pattern component is going trying to match.
* Calling a function with optional arguments
There is no special syntax for that. It is up to the programmer to define a datastructure with a variable part, e.g., a list.
* Calling a function with a variable number of arguments
Same answer.
* Calling a function with named arguments
There is no special syntax for that. You could pass a list of (name.value) pairs.
* Using a function in statement context
A f...
You can do
<lang bracmat>func$!myargument;</lang>
The <code>;</code> marks the end of a Bracmat statement.
* Using a function in first-class context within an expression
(Copied from JavaScript:) Bracmat functions are first-class citizens; they can be stored in variables and passed as arguments. Assigning to a variable <code>yourfunc</code> can be done in a few ways. The most common one is
<lang bracmat>(yourfunc=local vars.function body)</lang>
If there is already a function <code>myfunc</code> that you want to assign to <code>yourfunc</code> as well, do
<lang bracmat>('$myfunc:(=?yourfunc))</lang>
* Obtaining the return value of a function
<lang bracmat>myfunc$!myarg:?myresult</lang>
Notice that the returned value can be any evaluated expression.
* Distinguishing built-in functions and user-defined functions
You cannot list built-in functions that are implemented directly in C. Nor can such functions be passed as arguments or assigned to variables. There are also a number of built-in functions that are written in Bracmat. They are nothing special and can be deleted or redefined. You can see a list of all currently defined functions that are written in Bracmat with the function call <code>cat'</code>.
* Distinguishing subroutines and functions
You can ignore the return value of a function <code>myfunc</code> as follows:
<lang bracmat>myfunc$!myarg&yourfunc$!yourarg</lang>
But notice that if <code>myfunc</code> fails, the above expression returns the value produced by <code>myfunc</code>! To also ignore the success/failure of a function, do
<lang bracmat>`(myfunc$!myarg)&yourfunc$!yourarg</lang>
* Stating whether arguments are passed by value or by reference
Values are passed by reference, or by value if the reference counter, which is a very small integer, overflows. Most values are immutable, so for those there is no practical difference between passing by reference or value. The single exception of a mutable value is always passed by reference, and has an enormous reference counter. (The binary operator <code>=</code> introduces a mutable value and can be used for an object oriented style of programming.)
* Is partial application possible and how
There is no special syntax for that, but you can write a function that e.g., can take a list with one or with two elements and that returns a function in the first case.
<lang bracmat>( ( plus
= a b
. !arg:%?a ?b
& !b:
& '(.!arg+$a)
| !a+!b
)
& out$("1+2, not partial:" plus$(1 2))
& out$("1+2, partial:" (plus$1)$2)
);</lang>
Output:
<pre>1+2, not partial: 3
1+2, partial: 3</pre>
=={{header|C}}==
|