Getting Started Documentation Glish Learn More Programming Contact Us
Version 1.9 Build 1556
News FAQ
Search Home


next up previous contents index
Next: Function Definitions Up: Functions Previous: Functions

Simple Examples

Before delving into the details of functions, first look at some simple examples. Here's a function that returns the difference of its arguments:

    function diff(a, b) a-b
It can also be written:
    function diff(a, b)
        {
        return a - b
        }
Here's a version that prints its arguments before returning their difference:
    function diff(a, b)
        {
        print "a =", a
        print "b =", b
        return a - b
        }
In the following version the second parameter is optional and if not present is set to 1, so the function becomes a ``decrementer":
    function diff(a, b=1) a-b

Suppose you defined diff using this last definition. If you call it using:

    diff(3, 7)
it returns -4. If you call it using:
    diff(3)
it returns 2. If you call it using:
    diff(b=4, a=7)
it returns 3, because 7 - 4 = 3.

Every function definition is an expression (See Chapter 4, page [*]). When the definition is executed, it returns a value whose type is function. You can then assign the value to a variable or record field. For example,

    my_diff := function diff(a, b=1) a-b
assigns a function value representing the given function to my_diff. Later you can make the call:
    my_diff(b=4, a=7)
and the result will be 3, just as it will be if you call diff instead of my_diff. With this sort of assignment you can also leave out the function name:
    my_diff := function(a, b=1) a-b
Now my_diff is the only name of this function.


next up previous contents index
Next: Function Definitions Up: Functions Previous: Functions   Contents   Index
Please send questions or comments about AIPS++ to aips2-request@nrao.edu.
Copyright © 1995-2000 Associated Universities Inc., Washington, D.C.

Return to AIPS++ Home Page
2006-10-15