Coronavirus (Covid-19): Latest updates and information
Skip to main content Skip to navigation

User-defined functions

Function definitions have the form:

function-definition:
        function-declarator  function-body

function-declarator:
        func identifier
        proc identifier

function-body:
        { para-aliasopt  local-var-declopt  statement-listopt }

para-alias:
        para identifier-listopt ;

local-var-decl:
        auto identifier-listopt ;

identifier-list:
        identifier
        identifier , identifier-list

The identifier is declared to be a function using the keywords func or proc. There is no difference between func and proc. The word proc is more meaningful when the function serves as a procedure, i.e. a function which does not return a value.

  • All parameters are passed by value. The actual parameters forms a list called $ (dollar sign); notice that there is no parameter list after the identifier. The first argument is $[1], the second argument is $[2], and so forth. For convenience, $[1], $[2], ... can be replaced by $1, $2, ... .
  • para gives nicknames to the parameters. The first identifier in para-alias matches $1 and so forth. The number of identifiers does not required to match the number of actual parameters. $n can be referenced by either $n, $[n] or the nickname given.
  • $# is the current number of arguments. List operations are applicable to $.
  • All parameters are passed by value.
  • Local variables must be declared at the beginning of the function body and are preceded by the keyword auto (means dynamically allocated). All local variables are RWV's. There is no need to declare the type of local variables because EDEN does the run-time type checking. The syntax is:
    auto identifier-list ;
    
    where identifier-list is a list of identifiers separated by commas.
  • The return statement returns the value of the expression. The syntax is:
    return expressionopt ;
    
    If the expression is omitted, @ will be returned. Flowing off the end of a function is equivalent to return @.
  • The value returned can be ignored by the caller. In this case, the function acts as a procedure. The keyword func can be replaced by proc. There are no differences between these two keywords. Thus they can be interchanged. The purpose of having another keyword is to self-comment the program. It is intended (not restricted) that func should be used for defining side-effect-free operators.
    For example:
    func max    /* returns the max. value of its arguments */
    {
        para m;              /* m is the first argument $1 */
        auto i;              /* declare local variables */
    
        for (i = 2; i <= $#; i=i+1) {
                             /* for the other arguments */
            if ($[i] > m)    /* the ith argument */
                m = $[i];
        }
        return m;            /* returns max to the caller */
    }
    
    defines a function named max which returns the maximum value of its arguments.
  • To call a function, the arguments must be put in parentheses preceded by the function name. For example,
    MaxNumber = max(0,i,j,k);
    
    evaluates the maximum value of 0, i, j and k, and stores the results in the RWV MaxNumber. The parentheses cannot be omitted even if no arguments are passed to the function.
  • The function name, itself, denotes an entry point (a pointer) of the function code. It is a valid value (a pointer to a function) that can be used in an expression. Thus, if
    F = max;
    
    then the statement
    MaxNumber = F(1,3,2);
    
    assigns 3 to MaxNumber.
    Also a function can be put into a list. For example, if
    G = [ min, max ];
    
    where min is supposed to be a function that returns the minimum value of its arguments, then
    Num = G[n](1,3,2);
    
    Num will have the value 1 if n is 1 (G[1]=min), 3 if n is 2 (G[2]=max).
  • Once an identifier is used as a name of a function/procedure/action, it cannot be re-used to designate a RWV/formula variable. This restriction is posed by the interpreter (not by the language) to prevent destroying a function definition accidentally.