Functions

A function contains a series of EGL statements. Functions contain either the first executable code in a program or are called from another program. The function is not itself an EGL part (standalone functions are a special case; see Stand-alone function part). For an overview of functions, see Introduction to functions.

Functions can include the following elements:

The name main() is reserved for the top-level function that runs first whenever you start or call a program. Every program part must contain a function named main() that has no parameters or return type.

When declaring parameters, specify whether each is used as input to the function (in), output from the function (out), or both (inOut). The inOut modifier is the default if you do not specify one. Other modifiers include const, field, and sqlNullable. For more information, see "Parameter modifiers" in this topic.

EGL does not permit two functions to have the same signature (the combination of function name and number and type of parameters). Overloaded functions (same name but different signatures) are allowed. For more information, see Overloaded functions.

Syntax

Syntax for the function
functionName
The name you assigned to this function.
parameters
A list of variable names, types, and optional modifiers (see "Parameter modifiers" in this topic) that corresponds to the list of arguments with which the function is called. The parameters must be separated with commas. Although the parameters are optional, the parentheses are required.
type
A type that describes the value that the function returns. This type must match that of the variable that receives the return value in the invoking function. This can be a primitive type, a data item, a dictionary, or a record. The calling function can then access the changed record.
statement
Any EGL statement.

Parameter modifiers

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
If you specify multiple modifiers, specify them in the following order:
  1. sqlNullable (compatible with const and with any choice of in or inout or, in the absence of const, out)
  2. const (compatible with sqlNullable and with either in or inout)
  3. field (compatible with inout)
  4. in, out, or inout
Here are details about the modifiers:
const
Use this modifier if the function does not change the value of the parameter. When you use const, the EGL editor prevents you from changing the value, and in some cases, the generated code is more efficient.

You can use the const modifier for a parameter that is primitive or a dynamic array. Always use const if you want to pass a constant to a parameter that is modified by inout.

Restriction:
  • The const modifier cannot be accompanied by the out or field modifier, but can be accompanied by the sqlNullable modifier.
  • The const modifier cannot be used for a parameter in the following functions:
    • A constructor or other function in an external type
    • A function in a service or native library
  • Within a function, avoid updating a globally defined variable if it was passed to a function parameter that is modified by const. In the following example, the value of myParameter might or might not be changed by an update of a globally defined variable:
    program example
       myGlobal int = 4;
       
       function main()
          myFunction (myGlobal);
       end
    
       function myFunction(myParameter INT const in)
    
          // print 4, twice
          sysLib.writeStdOut(myGlobal);
          sysLib.writeStdOut(myParameter);
    
          // update the global variable and print 17
          myGlobal = 17;
          sysLib.writeStdOut(myGlobal);
    
          // print the parameter value, which may be 4 or 17
          sysLib.writeStdOut(myParameter);
       end
    end
  • A function is not compatible with a delegate if a parameter in one is modified by const and the equivalent parameter in the other is not modified by const. Here are examples:
    Delegate MyDelegatePart( za int in, zb int const in ) end
    
    Function F1( xa int in, xb int const in ) end
    Function F2( xa int const in, xb int const in ) end
    Function F3( xa int in, xb int in ) end
    
    Function example()
       myDelegateVariable MyDelegatePart;
       myDelegateVariable = F1;  // OK
       myDelegateVariable = F2;  // Invalid: xa has const, za does not
       myDelegateVariable = F3;  // Invalid: zb has const, xb does not
    end
field
Use this modifier if you are passing a text- or print-form field to the function. When you specify the field modifier, you can test the parameter for form field characteristics such as blanks, cursor, or numeric. You can use the parameter in a set statement, as in the following example:
set myField red, bold;

The field modifier must be accompanied by the inout parameter, whether stated explicitly or by default. The field modifier cannot be accompanied by the const 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 in for a record that accesses 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 that you assign to the parameter does not affect the value that the reference points to. However, if you change an element of an array without changing the original value of the parameter, the calling program detects the change. The program detects the change because the parameter still points to the same area of memory that the original argument 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 out for a record that accesses 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 for both the parameter and the argument.
If the argument is a reference type, a null reference passes to the corresponding function parameter. Any value that 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 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 EmpRecord type), and inOut is in effect, the record-specific state information, such as endOfFile status, is available in the function and is returned to the caller.
If the argument is a reference type, it passes to the corresponding function parameter as a reference. Any value that you assign to the parameter updates the corresponding variable in the calling program.
sqlNullable
For details, see “sqlNullable.”

Feedback