Call a function: Difference between revisions
Content added Content deleted
m (→{{header|Ada}}) |
|||
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.
* As a
* 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:
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>▼
X := H(Int => A, Fun => F'Access); -- named
X := H(A, Fun=>F'Access); -- mixture </lang>
Line 54 ⟶ 42:
A := Sum((1,2,3)); -- A = 6
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}}==
|