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 Standalone 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. For more information, see Parameter modifiers.

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.

Example

The following example illustrates the elements of a function:
package com.CompanyB.CustomerPackage;

program BalanceCheck type BasicProgram 
  customer INT = 1;
  balance DECIMAL(7,2) = 0;

  function main()
    while (balance >= 0)
      balance = getCustomerBalance(customer);   // returns -1
      printBalance (balance);
    end  // while
  end // main

  function getCustomerBalance (custNo INT in) returns (DECIMAL(7,2))
    myCustomer CustomerRecord;  // defined in library

    myCustomer.customerNumber = custNo;
    try
      get myCustomer;
    onException (CustomerNotFound)
      myCustomer.customerBalance = -1;  // breaks loop
    end  // try block

    return (myCustomer.customerBalance);
  end // function

  function printBalance (pBal DECIMAL(7,2) in)
    writeStdOut (strLib.formatNumber(pBal));
  end // function
end  // program

Feedback