Call a function: Difference between revisions

added ocaml
(→‎{{header|REXX}}: added the REXX language. -- ~~~~)
(added ocaml)
Line 282:
% arguments are passed by value, however Matlab has delayed evaluation, such that a copy of large data structures are done only when an element is written to.
</lang>
 
=={{header|OCaml}}==
 
* Calling a function that requires no arguments:
 
<lang ocaml>f ()</lang>
 
(In fact it is impossible to call a function without arguments, when there are no particular arguments we provide the type <code>unit</code> which is a type that has only one possible value. This type is mainly made for this use.)
 
* Calling a function with a fixed number of arguments:
 
<lang ocaml>f 1 2 3</lang>
 
* Calling a function with optional arguments:
 
For a function that has this signature:
 
<lang ocaml>val f : ?a:int -> int -> unit</lang>
 
here is how to call it with or without the first argument omited:
 
<lang ocaml>f 10
f ~a:6 10</lang>
 
Due to partial application, an optional argument always has to be followed by a non-optional argument. If the function needs no additional arguments then we use the type <code>unit</code>:
 
<lang ocaml>g ()
g ~b:1.0 ()</lang>
 
* Calling a function with a variable number of arguments:
 
This is not possible. The strong OCaml type system does not allow this.
The OCaml programmer will instead provide the variable number of arguments in a list, an array, an enumeration, a set or any structure of this kind.
(But if we really need this for a good reason, it is still possible to use a hack, like it has been done for the function <code>Printf.printf</code>.)
 
* Calling a function with named arguments:
 
Named arguments are called '''labels'''.
 
<lang ocaml>f ~arg:3</lang>
 
If a variable has the same name than the label we can use this simpler syntax:
 
<lang ocaml>let arg = 3 in
f ~arg</lang>
 
* Using a function in statement context:
 
<lang ocaml>(* TODO *)</lang>
 
* Using a function in first-class context within an expression:
 
functions in OCaml are first-class citizen.
 
* Obtaining the return value of a function:
 
<lang ocaml>let ret = f ()
let a, b, c = f () (* if there are several returned values given as a tuple *)
let _ = f () (* if we want to ignore the returned value *)
let v, _ = f () (* if we want to ignore one of the returned value *)</lang>
 
* Distinguishing built-in functions and user-defined functions:
 
There is no difference.
 
* Distinguishing subroutines and functions:
 
OCaml only provides functions.
 
* Stating whether arguments are passed by value or by reference:
 
OCaml arguments are always passed by reference. OCaml is an impure functional language, for immutable variables there is no difference if the argument is passed by value or by reference, but for mutable variables the programmer should know that a function is able to modify it.
 
* How to use partial application:
 
Just apply less arguments than the total number of arguments.
 
With partial application, the arguments are applied in the same order than they are defined in the signature of the function, except if there are labeled arguments, then it is possible to use these labels to partially apply the arguments in any order.
 
=={{header|PARI/GP}}==