Call a function: Difference between revisions

Content added Content deleted
Line 19: Line 19:
* Ada provides two kinds of subroutines: procedures, without return values, and functions, with return values. The return values of procedures must be used by the callers.
* Ada provides two kinds of subroutines: procedures, without return values, and functions, with return values. The return values of procedures must be used by the callers.


* As a general rule Ada compiler is free to pass arguments by value or by reference. Parameters have a mode, however: either 'in' or 'out' or 'in out'. It is prohibited to write somthing to an 'in' parameter. The next language Standard, Ada 2012, will support functions with 'out' and 'in out' mode parameters, so far, only procedures could have parameters with non-'in' modes.* There are no differences between between calling built-in vs. user defined functions (or procedures). Then, the only distinction between functions and procedures in Ada is the existence of a return value.
* As a rule of thumb, an Ada compiler is free to pass arguments either by value or by reference. Parameters have a mode, however: either 'in' or 'out' or 'in out'. It is prohibited to write somthing to an 'in' parameter. The next language Standard, Ada 2012, will support functions with 'out' and 'in out' mode parameters, so far, only procedures could have parameters with non-'in' modes.* There are no differences between between calling built-in vs. user defined functions (or procedures). Then, the only distinction between functions and procedures in Ada is the existence of a return value.


* Functions without parameters can be called by omitting the parameter list (no empty brackets!):<lang Ada>S: String := Ada.Text_IO.Get_Line;</lang>
* Functions without parameters can be called by omitting the parameter list (no empty brackets!):<lang Ada>S: String := Ada.Text_IO.Get_Line;</lang>
Line 27: Line 27:
A : Integer := F(12); -- F(12) is the same as F(12, 0)</lang>
A : Integer := F(12); -- F(12) is the same as F(12, 0)</lang>


* The caller is free to use either a positional parameters or named parameters, or a mixture of both (with positional parameters first) <lang Ada>X := H(A, F'Access); -- positional
* One can realize first-class functions by defining an access to a function as a parameter. Note that the function H below is a function with a fixed number of parameters (exactly two):<lang Ada>function H (Int: Integer;
Fun: not null access function (X: Integer; Y: Integer)
return Integer);
return Integer;

...

X := H(A, F'Access) -- assuming X and A are Integers, and F is a function
-- taking two Integers and returning an Integer.</lang>

* The caller is free to use either a positional parameters
or named parameters, or a mixture of both (with positional parameters first)
<lang Ada>X := H(A, F'Access); -- positional
X := H(Int => A, Fun => F'Access); -- named
X := H(Int => A, Fun => F'Access); -- named
X := H(A, Fun=>F'Access); -- mixture </lang>
X := H(A, Fun=>F'Access); -- mixture </lang>
Line 54: Line 42:
A := Sum((1,2,3)); -- A = 6
A := Sum((1,2,3)); -- A = 6
B := Sum((1,2,3,4)); -- B = 10</lang>
B := Sum((1,2,3,4)); -- B = 10</lang>

* One can realize first-class functions by defining an access to a function as a parameter. Note that the function H below is a function with a fixed number of parameters (exactly two):<lang Ada>function H (Int: Integer;
Fun: not null access function (X: Integer; Y: Integer)
return Integer);
return Integer;

...

X := H(A, F'Access) -- assuming X and A are Integers, and F is a function
-- taking two Integers and returning an Integer.</lang>


=={{header|Icon}} and {{header|Unicon}}==
=={{header|Icon}} and {{header|Unicon}}==