
Chapter 12 Device Logic (Pawn)
12.5.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 169.
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.
12.5.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.
Rev. 01
179
Содержание myDatalogEASY IoTmini
Страница 2: ......
Страница 11: ...Chapter 2 Declaration of conformity Chapter 2 Declaration of conformity Rev 01 11 ...
Страница 12: ......
Страница 34: ......
Страница 66: ......
Страница 78: ......
Страница 82: ......
Страница 186: ......
Страница 194: ......
Страница 196: ......
Страница 202: ......
Страница 205: ...Chapter 19 Document history Chapter 19 Document history Rev Date Changes 01 28 08 2020 First version Rev 01 205 ...
Страница 206: ......
Страница 208: ......