Call a function: Difference between revisions

m (added a ;Task: (bold) header.)
Line 2,037:
 
By default, arguments are passed readonly, which allows the implementation to decide whether pass-by-reference or pass-by-value is more efficient on a case-by-case basis. Explicit lvalue, reference, or copy semantics may be requested on a parameter-by-parameter basis, and the entire argument list may be processed raw if that level of control is needed.
 
=={{header|Phix}}==
Phix has three kinds of routines: procedure, function, and type. A procedure does not return a value, whereas a function does.
A type is a specialised kind of function, that permits declarations of instances of that type, which are
automatically validated whenever they are changed, and further the type routine always returns true or false.
* Phix does not allow implicit discard of function results. The explicit discard statement takes the form
<lang Phix>{} = myfunction()</lang>
* This is in fact a simple contraction of standard multiple assigment (which can be nested):
<lang Phix>{cities,populations} = columize(muncipalities)
{{},populations} = columize(muncipalities) -- discard result[1]
{cities,{}} = columize(muncipalities) -- discard result[2]
{cities} = columize(muncipalities) -- ""</lang>
* Calling a function with no parameters still requires the empty argument list.
* Optional arguments are denoted by the presence of a default, and must be grouped on the right:
<lang Phix>function myfunction(integer a, string b="default")
return {a,b}
end function
--? myfunction() -- illegal, compile-time error
?myfunction(1) -- displays {1,"default"}
?myfunction(2,"that") -- displays {2,"that"}</lang>
* Sequence parameters can be of any length, which is another way to implement optional/variable number of arguments.
* Named arguments can be specified in any order, with an error if any non-optional parameters are missing:
<lang Phix>?myfunction(b:="then",a:=3) -- displays {3,"then"}
--?myfunction(b:="though") -- compile-time error</lang>
* The programmer is free to use either positional parameters or named parameters, or a mixture of both (with positional parameters first).
* Phix does not support first-class functions, but instead uses an integer routine_id mechanism (and obviously integers are first class):
<lang Phix>constant integer r_my_func = routine_id("myroutine")
?call_func(r_my_func,{1}) -- displays {1,"default"}</lang>
The value of r_my_func can be passed as an argument to any routine, or stored in a table, and invoked in a similar fashion.<br>
Note however that for performance reasons some builtins do not have a proper routine_id; if you need one you must write a trivial one-line wrapper.<br>
(For a full list, see psym.e/syminit() calls to AutoAsm(), whereas calls to initialAutoEntry() therein indicate builtins that can have routine_ids.)<br>
(One day the compiler may be enhanced to automatically create one-line wrappers as needed, but that is quite near the end of a fairly long to-do list.)
* Partial application is usually achieved through a single variable-length "user_data" parameter within a call_func() expression.
* All arguments are passed by reference with copy-on-write semantics: to modify the value of a parameter you must both return and assign it, as in:
<lang Phix>s = append(s,item)</lang>
* Implicit forward calls are supported, as are optional explicit forward declarations, which occasionally give better compilation error messages.
 
=={{header|PicoLisp}}==
7,806

edits