Functions are blocks of code that can be called several times and from multiple locations in the graph code. A function is a subroutine which, unlike a procedure, returns a value at the end of its execution.
A function declares itself exactly as a procedure.
- Proc Name(s)
- Proc Test(x)
"Proc" is the keyword that always identifies a procedure or function. The "Test" label is the name of the function. The parameter "x" is the data that is passed to the function code. The parameters of a function are declared as local variables of the function.
In general, the structure of a function is identical to that of a procedure, so also refer to the chapter that talks about Procedures. In order for a procedure to become a function and return a value, it is necessary to valorize the variable "result" within it.
- Proc Test(x)
- result = x*2
A function, because it returns a value, can be used as an expression in assignments and operations. For example:
- I = Test(30)
The "I" variable contains the value 60, since the "Test" function returns a value and assigns it the data "x*2", in this case "30*2".
- I = Test(30)+7
In this case, the variable "I" contains the value 67 because an addition operation is carried out before the assignment.
A function can be called up as a procedure. In this case, the return value is ignored.
A function can be used as a parameter of a procedure or another function. For example:
- I = Test(Test(2))
The value assigned to "i" will be 8. The "Test" function has been used as a parameter of itself.
A procedure or function may recall itself in a recursive cycle, direct or indirect. Recursive cycles are monitored to avoid infinite loops. You can use recursion up to 256 times.