The second type of functions being supported are generic or multiple line functions. These functions are defined with a FUNCTION statement and whose identifier can be any standard identifier name with an optional data type symbol. The general syntax for a function is:
FUNCTION function_name(arguments)
...
function_name = <return value>
...
END FUNCTION
The arguments will be optional. The arguments will be called by reference if a variable or array element is used as an argument, otherwise the the arguments are by values. The arguments will be treated as local variables, but when arguments are by reference, the variable or array element argument will also be modified is assigned. The by reference can be prevented by surrounding the argument in the function call by parentheses as in (A). Arrays can be passed by reference by using just the array name.
Somewhere before returning, the return value must be assigned using a standard assignment of the function name. This variable will work like a local variable within the function, so it can be used in an expression, for example:
function_name = function_name + 1
Unlike the single statement user functions, these functions may call themselves (i.e. recursion). It is up to the programmer to prevent an infinite loop, though eventually memory will run out as each function call will increase the size of the stack. Because of the way the function name is used as a local variable, a function with no arguments cannot call itself. This is not really an issue since without arguments, it is impossible that a function can determine an end condition when it simply returns instead of calling itself recursively without accessing variables outside the function - not good programming practice.
The function will return upon a RETURN statement or when the END FUNCTION statement is reached. The return value must have been set before reaching a RETURN or the END FUNCTION statement or an error will occur. The RETURN statement will also support an optional return value, which will override any value set in the function_name return value (programmer beware, no check for this will be made). The FUNCTION will not be executed upon reaching the FUNCTION statement, execution will proceed around the function to after the END FUNCTION statement. The function will be called when the function_name is used in an expression as (including within the function itself):
A = function_name(arguments)
B = function_with_no_args
Thursday, December 31, 2009
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
All comments and feedback welcomed, whether positive or negative.
(Anonymous comments are allowed, but comments with URL links or unrelated comments will be removed.)