Call a function: Difference between revisions

m
Line 23:
* Functions without parameters can be called by omitting the parameter list (no empty brackets!):<lang Ada>S: String := Ada.Text_IO.Get_Line;</lang>
 
* Ada provides forsupports functions with optional parameters:<lang Ada>function F(X: Integer; Y: Integer := 0) return Integer;
...
A : Integer := F(12);
Line 29:
C : Integer := F(12, 1); -- something different</lang>
 
* If the number of parameters of F where fixed to two, then B and C would be OK but A wouldn't.
* The caller is free to use either a positional parameters or named parameters, or a mixture of both (with positional parameters first) <lang Ada>Positional := H(A, F'Access);
Named := H(Int => A, Fun => F'Access);
Mixed := H(A, Fun=>F'Access); </lang>
 
* Ada does not support functions with a variable number of arguments. But a function argument can be an unconstrained array with as many values as you want:<lang Ada>type Integer_Array is array (Positive range <>) of Integer;
Line 46 ⟶ 44:
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);
Line 55 ⟶ 53:
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>Positional := H(A, F'Access);
Named := H(Int => A, Fun => F'Access);
Mixed := H(A, Fun=>F'Access); </lang>
 
=={{header|Icon}} and {{header|Unicon}}==
Anonymous user