13.4.7 Functions
A function declaration specifies the name of the function and the formal parameters enclosed in brackets. A
function can also return a value. A function must be defined globally, i.e. declared outside of another function
and is globally available.
If the function declaration is followed by a semicolon (instead of a statement), this is a forward declaration of a
function.
The "return" statement sets the return value of the function. For example, the return value of the "sum"
function (see below) is the sum of both parameters. The "return" expression is optional.
sum(a, b)
{
return a + b
}
The arguments of a function are (declared implicitly) local variables for this function. The function call specifies
the values of the arguments. Another example of a complete definition of a function is "leap year" that
indicates "true" or "false" for the relevant year.
leapyear(y)
{
return y % 4 == 0 && y % 100 != 0 || y % 400 == 0
}
Details of the statements used in this example are provided in the chapter "Operators and expressions" on
page 218.
Generally, functions include local variable declarations and consist of a block statement.
Note:
In the next example, the "assert" statement prevents negative values for the exponent.
power(x, y)
{
/* returns x
y
*/
assert y >= 0
new r = 1
for (new i = 0; i < y; i++)
r *= x
return r
}
A function can comprise several "return" statements, for example, one is used to quickly terminate a function if
invalid parameters are transferred, or when it becomes apparent that the function has nothing to do. If a
function returns an array, all of the "return" statements must return an array with the same number of entries.
13.4.7.1 Function arguments ("call-by-value" versus "call-by-reference")
The "faulty" function in the next example has a parameter that is used in the loop to calculate the factorial of
this number. It must be noted that the function modifies the argument.
228
Rev. 05
Summary of Contents for myDatalogEASY V3
Page 2: ......
Page 13: ...Chapter 2 Declaration of conformity Chapter 2 Declaration of conformity Rev 05 13 ...
Page 14: ......
Page 42: ......
Page 76: ......
Page 88: ......
Page 102: ......
Page 110: ......
Page 116: ......
Page 234: ......
Page 244: ......
Page 252: ......
Page 254: ......
Page 266: ......
Page 276: ......