Next: , Up: Built-in   [Contents][Index]


9.1.1 Calling Built-in Functions

To call one of awk’s built-in functions, write the name of the function followed by arguments in parentheses. For example, ‘atan2(y + z, 1)’ is a call to the function atan2() and has two arguments.

Whitespace is ignored between the built-in function name and the open parenthesis, but nonetheless it is good practice to avoid using whitespace there. User-defined functions do not permit whitespace in this way, and it is easier to avoid mistakes by following a simple convention that always works—no whitespace after a function name.

Each built-in function accepts a certain number of arguments. In some cases, arguments can be omitted. The defaults for omitted arguments vary from function to function and are described under the individual functions. In some awk implementations, extra arguments given to built-in functions are ignored. However, in gawk, it is a fatal error to give extra arguments to a built-in function.

When a function is called, expressions that create the function’s actual parameters are evaluated completely before the call is performed. For example, in the following code fragment:

i = 4
j = sqrt(i++)

the variable i is incremented to the value five before sqrt() is called with a value of four for its actual parameter. The order of evaluation of the expressions used for the function’s parameters is undefined. Thus, avoid writing programs that assume that parameters are evaluated from left to right or from right to left. For example:

i = 5
j = atan2(i++, i *= 2)

If the order of evaluation is left to right, then i first becomes 6, and then 12, and atan2() is called with the two arguments 6 and 12. But if the order of evaluation is right to left, i first becomes 10, then 11, and atan2() is called with the two arguments 11 and 10.


Next: , Up: Built-in   [Contents][Index]