Getting Started | Documentation | Glish | Learn More | Programming | Contact Us |
Version 1.9 Build 1556 |
|
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-bIt 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-bassigns 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-bNow my_diff is the only name of this function.