Functions and Program Structure

Gamma: Version 4.0
Prev |  | Next

# Chapter 7. Functions and Program Structure

# 7.1. Function Definition

A function is defined in Gamma using a `function` statement:

        Gamma>  function thing (a) { random() * a;}
        (defun thing (a) (* (random) a))
        Gamma>  thing (5);
        2.3869852581992745399

        function pow (v, exp)
        {
            local result = 1;

            while (exp −− > 0)
            {
            result *= v;
            }

            result;
        }

No type specification is used since the type returned will be determined by the expressions within the function when it executes:

        Gamma> pow (2,3);
        8
        Gamma> pow (2.1, 3.25);
        19.4481

C programmers should note that this typeless function definition bears no similarity to a void function type, which does not exist in Gamma.

The value and type of a function is the return value of the last expression evaluated within the function. To return the value of a specific variable that only exists within the scope of the user function, that variable name is placed by itself on the last line of the function. This effectively causes that symbol to be evaluated, returning its value.

Functions do not need to be defined before they are referenced in a file, but they must exist before they are called. In other words, it is the run-time order, not the loading (reading) order that is important.

Prev | Home | Next
Error Handling - interactive session | Up | Function Arguments

Copyright 1995-2002 by Cogent Real-Time Systems, Inc.