
main()
{
new v = 5
new f = faculty(v)
}
faculty(n)
{
assert n >= 0
new result = 1
while (n > 0)
result *= n--
return result
}
Regardless of what (positive) value the "n" variable has at the start of the "while" loop, "n" will equal zero at
the end of the function. In the "faculty" function, for example, the parameter is transferred as a value ("by
value"), which means that changes to the "n" variable are only valid locally in the "faculty" function. In other
words, the "v" variable in the "main()" function has the same value before and after the function is called up.
Arguments can be transferred as a value ("by value") or as a reference ("by reference"). A function argument
that is to be transferred as a reference must have the "&" prefix preceding the name. The arguments are
transferred to the function as a value by default.
Example:
swap(&a, &b)
{
new temp = b
b = a
a = temp
}
To transfer an array to a function, add a pair of brackets ("[]") to the name of the argument. The number of
entries can also be specified. This improves the error detection of the compiler's parser.
Example:
addvector(a[], const b[], size)
{
for (new i = 0; i < size; i++)
a[i] += b[i]
}
Arrays are always transferred as a reference.
Note:
The "b" array in the above-mentioned example is not changed in the function. This function
argument was declared as a "const" to make this explicit. In addition to the improved error detection, it
also enables the compiler to generate a more efficient code.
180
Rev. 01