Go forward to Precedence.
Go backward to Conditional Exp.
Go up to Expressions.
Function Calls
==============
A "function" is a name for a particular calculation. Because it has
a name, you can ask for it by name at any point in the program. For
example, the function `sqrt' computes the square root of a number.
A fixed set of functions are "built-in", which means they are
available in every `awk' program. The `sqrt' function is one of these.
See Built-in Functions: Built-in, for a list of built-in functions
and their descriptions. In addition, you can define your own functions
in the program for use elsewhere in the same program.
See User-defined Functions: User-defined, for how to do this.
The way to use a function is with a "function call" expression,
which consists of the function name followed by a list of "arguments"
in parentheses. The arguments are expressions which give the raw
materials for the calculation that the function will do. When there is
more than one argument, they are separated by commas. If there are no
arguments, write just `()' after the function name. Here are some
examples:
sqrt(x^2 + y^2) # One argument
atan2(y, x) # Two arguments
rand() # No arguments
*Do not put any space between the function name and the
open-parenthesis!* A user-defined function name looks just like the
name of a variable, and space would make the expression look like
concatenation of a variable with an expression inside parentheses.
Space before the parenthesis is harmless with built-in functions, but
it is best not to get into the habit of using space to avoid mistakes
with user-defined functions.
Each function expects a particular number of arguments. For
example, the `sqrt' function must be called with a single argument, the
number to take the square root of:
sqrt(ARGUMENT)
Some of the built-in functions allow you to omit the final argument.
If you do so, they use a reasonable default. See Built-in Functions: Built-in, for full details. If arguments are omitted in calls to
user-defined functions, then those arguments are treated as local
variables, initialized to the null string (*note User-defined
Functions: User-defined.).
Like every other expression, the function call has a value, which is
computed by the function based on the arguments you give it. In this
example, the value of `sqrt(ARGUMENT)' is the square root of the
argument. A function can also have side effects, such as assigning the
values of certain variables or doing I/O.
Here is a command to read numbers, one number per line, and print the
square root of each one:
awk '{ print "The square root of", $1, "is", sqrt($1) }'