Call a function: Difference between revisions

Content added Content deleted
(Add Seed7 example)
Line 847: Line 847:
│ or: map(f(7,_,9),{1,...,9}) │
│ or: map(f(7,_,9),{1,...,9}) │
└────────────────────────────────────────────────────────────────────┘*/</lang>
└────────────────────────────────────────────────────────────────────┘*/</lang>

=={{header|Seed7}}==
* Seed7 provides two kinds of subroutines: ''proc'', which has no return value, and ''func'', which has a return value. The return value of a ''func'' must be used by the caller (e.g. assigned to a variable). If you don't want do deal with the return value, use a ''proc'' instead.

* Seed7 supports call-by-value, call-by-reference, and call-by-name parameters. Programmers are free to specify the desired parameter passing mechanism. The most used parameter passing mechanism is 'in'. Depending on the type 'in' specifies call-by-value (for integer, float, ...) or call-by-reference (for string, array, ...). It is prohibited to write something to an 'in' parameter.

* All parameters are positional.

* There are no differences between between calling built-in vs. user defined functions.<lang seed7>env := environment; # Call a function that requires no arguments.
env := environment(); # Alternative possibility to call of a function with no arguments.
cmp := compare(i, j); # Call a function with a fixed number of arguments.</lang>

* There are no optional arguments, but a similar effect can be achieved with overloading.<lang seed7>write(aFile, "asdf"); # Variant of write with a parameter to specify a file.
write("asdf"); # Variant of write which writes to the file OUT.</lang>

* Seed7 does not support functions with a variable number of arguments. But a function argument can be an array with as many values as you want:<lang seed7>const func integer: sum (in array integer: intElems) is func
result
var integer: sum is 0;
local
var integer: element is 0;
begin
for element range intElems do
sum +:= element;
end for;
end func;

s := sum([] (1, 2, 3)); # Use an aggregate to generate an array.
t := sum([] (2, 3, 5, 7));</lang>

* Concatenation operators can be used to concatenate arguments. This solution is used to provide the write function:<lang seed7>write("Nr: " <& num); # Use operators to concatenate arguments.</lang>

* The procedure ignore can be used to ignore a return value.<lang seed7>ignore(getln(IN)); # Using a function in statement context (ignore the result).</lang>

* Call-by-name parameters use a function in first-class context. The function [http://seed7.sourceforge.net/examples/map.htm doMap] from the examples section of the Seed7 homepage uses a given expression to modify the elements of an array:<lang seed7>seq := doMap([](1, 2, 4, 6, 10, 12, 16), x, succ(x));</lang>


=={{header|Smalltalk}}==
=={{header|Smalltalk}}==