Before delving into the details of functions, we first look at some simple examples. Here's a function that returns the difference of its arguments:
function diff(a, b) a-bIt could 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 }Here's a version in which 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 we have defined diff using this last definition. If we call it using:
diff(3, 7)then it returns -4. If we call it using:
diff(3)it returns 2. If we call it using:
diff(b=4, a=7)it returns 3, since 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 we could make the call:
my_diff(b=4, a=7)and the result would be 3, just as it would be if we'd called diff instead of my_diff. With this sort of assignment we could also leave out the function name:
my_diff := function(a, b=1) a-bNow my_diff would be the only name of this function.