Function Renaming

Gamma: Version 4.0
Prev | Chapter 7. Functions and Program Structure | Next

# 7.3. Function Renaming

When a function is defined, Gamma automatically assigns the function definition to the symbol that was provided as the function name, in the global scope. This does not mean that the symbol and the function definition are permanently related.

(A function definition is an independent data object which can be passed as an argument to a function or assigned to a symbol in any scope. For a C programmer this makes a Gamma function definition operate in much the same way as a function pointer in C. However, Gamma function definitions are much more versatile.)

It is possible to re-map a function definition at run-time to modify its behavior. For example, we may like to modify the function `pow` defined in the Function Definition section. We would like the improved `pow` to check the argument type and accept a string as its argument as well as a number. In Gamma, there are functions like `int_p`, `real_p`, and `string_p` that are used to determine the data type of a variable. (For the complete list of `-p` functions see Data Types and Predicates in the Reference Manual).

Thus, we rename the `pow` function defined in the section Function Definition to `__pow` and write the new version as follows:

        ...
        __pow = pow;
        function pow (v, exp)
        {
            local result;
            if (string_p (v))
            {
                result = "";
                while (exp −− > 0)
                    result = string(result, v);
            }
            else
                result = __pow(v, exp);
            result;
        }

Then the function call `pow("hello", 3)` will produce:

        "hellohellohello"

Prev | Home | Next
Function Arguments | Up | Loading files

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