Global Functions
Declaring Global Functions
A function declaration associates an identifier with a block of code as a function. That function can then be called in your program within an expression. The syntax for a function declaration is:
Function {function-name} Global [{type1} {param1} {type2} {param2} …] ;
Returns {return-type}
[{variable-declarations}]
{
statement 1
}
{
statement 2
}
…
{
statement n
}
Function_Return {return-value}
End_Function
Where:
- {function-name} is the identifier associated with the function. You cannot use a DataFlex reserved word as a function name.
- {type1} is the data type of parameter {param1}.
- {type2} is the data type of {param2}, etc.
- {return-type} is the declared type of the function. The value that is returned by the function will be compatible with this type.
- {variable-declarations} are the local variables that you can declare for the function.
- {statement 1-n} are the statements that are executed whenever the function is called, and {return-value} is the value returned by the function.
You can define zero or more parameters for each function. Each parameter behaves like a local variable that is initialized by the statement that calls the function. The data types of each parameter can be any of the DataFlex simple types (Integer, Number, Real, etc.) as well as struct types and arrays.
By reference parameters can also be declared. In this case, the parameter's name must be preceded by the keyword "ByRef". When a parameter is passed by reference, the procedure has direct read/write access to the passed variable. For more information, refer to Parameter Passing.
Each function must return a value. Execution of the function stops immediately after a Function_Return statement has been executed.
Here are a few examples of function declarations:
Function PowerOf Global Integer iX Integer iY Returns Integer
Integer iReturnVal
Integer iCount
Move 1 To iReturnVal
For iCount From 1 To iY
Move (iX * iReturnVal) To iReturnVal
Loop
Function_Return iReturnVal
End_Function
Function CreateValues Global Returns Integer[]
...
End_Function
// Assuming a struct type tCustomer has been declared somewhere else
Function CreateCustomer Global Returns tCustomer
...
End_Function
Calling Global Functions
Once a function has been declared, you can execute it in your program within an expression. The syntax for calling a function in an expression is:
({function-name}([{param1}, {param2}, … {paramN}]))
Where:
- {function-name} is the name of the previously declared function that you are executing.
- {param1 .. N} are the parameters that you are passing to the called function. The number and order of parameters that you pass must match the list of parameters in the function's declaration. The parameters you pass can be either constants, variables, or expressions, so long as they are of comparable type to the matching parameter in the function declaration.
Examples
If (CurrentUser() = "ADMIN") Showln "Administrator"
Move (PowerOf(2, 8)) To iAnswer
Move (FormatString(sMyString)) To sMyString
Parameter Passing and By Reference Parameters
The parameters of a procedure or function can be passed by reference, as well as by value.
When a parameter is passed by reference, the called procedure or function has direct read/write access to the passed variable. Changes made to the reference parameter inside the routine are reflected in the passed variable outside the method.
For more information, refer to Parameter Passing.