Chapter 7. Functions and Program Structure

Table of Contents

7.1. Function Definition
7.2. Function Arguments
7.2.1. Variable number of arguments
7.2.2. Optional arguments
7.2.3. Protection from evaluation
7.2.4. Variable, optional, unevaluated arguments
7.2.5. Examples
7.3. Function Renaming
7.4. Loading files
7.5. The main Function
7.6. Executable Programs
7.7. Running a Gamma Program
7.8. Command Line Arguments

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 [minus ][minus ] > 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.