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. Functions either
contain the first executable code in the program or are called from
another function.
The name main() is reserved for the top-level
function that runs first when you start or call a program. Every Program
part must contain a function named main() that
has no parameters or return type.
Function parameters correspond in number, type, and position to
the arguments that you pass to a function. Two kinds of parameters
are available:
- Reference parameters point to a location where a value is stored
(see Reference variables).
- Value parameters contain the value itself.
For example, a dynamic array is a reference variable.
When you declare a parameter, you can specify one or more of the
following parameter modifiers:
- const indicates that an input argument
is treated as a constant
- field indicates that a text- or print-form
field is being passed to the parameter
- in, out, and inout are
mutually exclusive; they indicate whether the parameter is for input,
output, or both
- sqlNullable is only for code migrated
from VisualAge® Generator
and indicates that an argument (as used for database access) can be
null
For information about function syntax and parameter modifiers,
see Functions.
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)
You can create overloaded
functions if these prerequisites are met:
- 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 generation, EGL uses these rules to resolve references
to unqualified function names:
- EGL searches in the current scope for all functions with matching
names.
- If EGL finds matching function names in more than one container
(logical part), the generator displays an ambiguous reference error.
- 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.
- 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 that
causes a validation error.
- 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 another with a BIGINT parameter, EGL chooses 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.