Introduction to functions

Functions are the fundamental units in EGL logic parts.

A function contains a series of EGL statements. Except for standalone functions, functions are not EGL parts (see Standalone Function part). Functions either contain the first executable code in the generatable part or are called from another function.

For information about function syntax, see Functions in the Reference section.

Parameter modifiers

Parameters correspond in number, type, and position to the arguments that you pass to a function. There are two kinds of parameters:
  • Reference parameters point to a location where a value is stored (see Reference variables).
  • Value parameters contain the value itself.
For example, an array is always a reference variable.
When you declare parameters, you must specify a parameter modifier:
in
Use this modifier if the parameter is used as input to the function. When you specify in, the function receives the argument value as input, but the calling function does not receive changes that are made to the parameter. You cannot specify the in modifier for a record that is used to access a file or database in either the current function or in a function that is called by the current function.
When you specify a limited-length string as a function parameter whose modifier is in, any text input is valid:
  • If the argument contains more characters than are valid in the parameter, EGL truncates the copied content to fit the available length.
  • If the argument contains fewer characters than are valid in the parameter, EGL pads the copied content with blanks to meet the specified length.
If the argument is a reference type, a copy passes to the corresponding function parameter. Any value you assign to the parameter does not affect the value that the reference points to.
out
Use this modifier if the parameter is used as output from the function. The function does not receive the argument value as an input; the parameter is initialized according to the rules described in Data initialization. When the function returns, a value is assigned to the argument of the calling program.
If the argument is a literal or constant, the parameter is treated as if the modifier is in. You cannot specify the out modifier for a record that is used to access a file or database in either the current function or in a function invoked by the current function.
When you specify a limited-length string as a function parameter whose modifier is out, the length limit must be the same in the parameter and the argument.
If the argument is a reference type, a null reference passes to the corresponding function parameter. Any value you assign to the parameter updates the corresponding variable in the calling function.
inOut
Use this modifier if the parameter is used as both input to and output from the function. The function receives the argument value as input, and the calling function receives all changes to the parameter when the function ends. If the argument is a literal or constant, it is treated as if the modifier is in.
If the argument is a record, the following rules apply:
  • If you intend to use the record to access a file or database in the current function or in a function called by the current function, you must specify the inOut modifier or accept that modifier by default.
  • If the type of record is the same for the parameter and the argument (for example, if both are serial records), the record-specific state information such as endOfFile status is available in the function and is returned to the caller, but only if the inOut modifier is in effect.
If the argument is a reference type, it passes to the corresponding function parameter as a reference. Any value you assign to the parameter updates the corresponding variable in the calling program.
sqlNullable
This modifier is available to relational database users. For more information, see sqlNullable.
You can use a question mark to indicate that a parameter is nullable, as in the following example:
function getCustomerBalance (custNo INT? inOut) returns (DECIMAL(7,2))
...
end

Overloaded functions

An overloaded function has multiple function signatures for one function name. The signature of a function is the combination of the name of the function with the number and type of its parameters. The return value of the function is not part of its signature. When multiple functions have the same name, EGL uses the signature to match function code to a function call. EGL does not permit two functions to have the same signature.

Many EGL system functions are overloaded. For example, you can call the sysLib.audit() function with one parameter or two. The second parameter specifies a journal ID. If you call the function with a single parameter, the function writes to the system journal. The two functions have the following signatures:
sysLib.audit(record BasicRecord in)
sysLib.audit(record BasicRecord in, jid SMALLINT in)
In another example, the mathLib.abs() function accepts a range of numeric types, and returns a value of the same type as the input. This requires a different signature for each numeric type:
mathLib.abs(inputVar SMALLINT)
mathLib.abs(inputVar INT)
mathLib.abs(inputVar BIGINT)
The following rules apply to creating overloaded functions:
  • The function cannot be a standalone function.
  • The overloaded functions must be in the same part. If two parts contain functions with the same name, even if the functions have different signatures, you cannot use an unqualified reference to that function name.
  • Because the function signature does not include the return value, you cannot have two functions that are identical except for the return value.
During the generation process, EGL uses the following rules to resolve references to unqualified function names:
  1. EGL searches in the current scope for all functions with matching names.
  2. If EGL finds matching function names in more than one container (logical part), the generator displays an ambiguous reference error.
  3. If more than one function matches the number of parameters, EGL continues to step 4. If no function matches, the generator displays an invalid arguments error.
  4. EGL searches for a function with parameters that match the types of the arguments in the function call. If no match is found, EGL continues to step 5. EGL cannot find multiple matching functions because doing so causes a validation error.
  5. EGL searches for a function with parameters that are assignment-compatible with the arguments in the function call. If no match is found, the generator displays an invalid arguments error. If multiple matches are found, EGL searches for the best match. For example, if the argument is a SMALLINT and EGL finds two functions, one with an INT parameter and the other with a BIGINT parameter, EGL will pick the function with the INT parameter, because an INT is closer in size to SMALLINT. If EGL cannot find such a match, the generator displays an ambiguous reference error.

Feedback