Complicated awk programs can often be simplified by defining
your own functions. User-defined functions can be called just like
built-in ones (see section Function Calls), but it is up to you to define
them--to tell awk what they should do.

Definitions of functions can appear anywhere between the rules of an
awk program. Thus, the general form of an awk program is
extended to include sequences of rules and user-defined function
definitions.
There is no need in awk to put the definition of a function
before all uses of the function. This is because awk reads the
entire program before starting to execute any of it.

name is the name of the function to be defined. A valid function
name is like a valid variable name: a sequence of letters, digits and
underscores, not starting with a digit.
Within a single awk program, any particular name can only be
used as a variable, array or function.

parameter-list is a list of the function's arguments and local
variable names, separated by commas. When the function is called,
the argument names are used to hold the argument values given in
the call. The local variables are initialized to the empty string.
A function cannot have two parameters with the same name.

The body-of-function consists of awk statements. It is the
most important part of the definition, because it says what the function
should actually do. The argument names exist to give the body a
way to talk about the arguments; local variables, to give the body
places to keep temporary values.

Argument names are not distinguished syntactically from local variable
names; instead, the number of arguments supplied when the function is
called determines how many argument variables there are. Thus, if three
argument values are given, the first three names in parameter-list
are arguments, and the rest are local variables.

It follows that if the number of arguments is not the same in all calls
to the function, some of the names in parameter-list may be
arguments on some occasions and local variables on others. Another
way to think of this is that omitted arguments default to the
null string.

Usually when you write a function you know how many names you intend to
use for arguments and how many you intend to use as local variables. It is
conventional to place some extra space between the arguments and
the local variables, to document how your function is supposed to be used.

During execution of the function body, the arguments and local variable
values hide or shadow any variables of the same names used in the
rest of the program. The shadowed variables are not accessible in the
function definition, because there is no way to name them while their
names have been taken away for the local variables. All other variables
used in the awk program can be referenced or set normally in the
function's body.

The arguments and local variables last only as long as the function body
is executing. Once the body finishes, you can once again access the
variables that were shadowed while the function was running.

The function body can contain expressions which call functions. They
can even call this function, either directly or by way of another
function. When this happens, we say the function is recursive.

Instead it defines a rule that, for each record, concatenates the value
of the variable `func' with the return value of the function`foo'.
If the resulting string is non-null, the action is executed.
This is probably not what was desired. (awk accepts this input as
syntactically valid, since functions may be used before they are defined
in awk programs.)

When working with arrays, it is often necessary to delete all the elements
in an array and start over with a new list of elements
(see section The delete Statement).
Instead of having
to repeat this loop everywhere in your program that you need to clear out
an array, your program can just call delarray.

Here is an example of a recursive function. It takes a string
as an input parameter, and returns the string in backwards order.

A function call consists of the function name followed by the arguments
in parentheses. What you write in the call for the arguments are
awk expressions; each time the call is executed, these
expressions are evaluated, and the values are the actual arguments. For
example, here is a call to foo with three arguments (the first
being a stringconcatenation):

foo(x y, "lose", 4 * z)

Caution:whitespace characters (spaces and tabs) are not allowed
between the function name and the open-parenthesis of the argument list.
If you write whitespace by mistake, awk might think that you mean
to concatenate a variable with an expression in parentheses. However, it
notices that you used a function name and not a variable name, and reports
an error.

When a function is called, it is given a copy of the values of
its arguments. This is known as call by value. The caller may use
a variable as the expression for the argument, but the called function
does not know this: it only knows what value the argument had. For
example, if you write this code:

foo = "bar"
z = myfunc(foo)

then you should not think of the argument to myfunc as being
"the variable foo." Instead, think of the argument as the
string value, "bar".

If the functionmyfunc alters the values of its local variables,
this has no effect on any other variables. Thus, if myfunc
does this:

to change its first argument variable str, this does not
change the value of foo in the caller. The role of foo in
calling myfunc ended when its value, "bar", was computed.
If str also exists outside of myfunc, the function body
cannot alter this outer value, because it is shadowed during the
execution of myfunc and cannot be seen or changed from there.

However, when arrays are the parameters to functions, they are not
copied. Instead, the array itself is made available for direct manipulation
by the function. This is usually called call by reference.
Changes made to an array parameter inside the body of a functionare
visible outside that function.
This can be very dangerous if you do not watch what you are
doing. For example:

The body of a user-defined function can contain a return statement.
This statement returns control to the rest of the awk program. It
can also be used to return a value for use in the rest of the awk
program. It looks like this:

return [expression]

The expression part is optional. If it is omitted, then the returned
value is undefined and, therefore, unpredictable.

A return statement with no value expression is assumed at the end of
every function definition. So if control reaches the end of the function
body, then the function returns an unpredictable value. awk
will not warn you if you use the return value of such a function.

Sometimes, you want to write a function for what it does, not for
what it returns. Such a function corresponds to a voidfunction
in C or to a procedure in Pascal. Thus, it may be appropriate to not
return any value; you should simply bear in mind that if you use the return
value of such a function, you do so at your own risk.

Here is an example of a user-defined function that returns a value
for the largest number among the elements of an array:

You call maxelt with one argument, which is an array name. The local
variables i and ret are not intended to be arguments;
while there is nothing to stop you from passing two or three arguments
to maxelt, the results would be strange. The extra space before
i in the function parameter list indicates that i and
ret are not supposed to be arguments. This is a convention that
you should follow when you define functions.

Here is a program that uses our maxeltfunction. It loads an
array, calls maxelt, and then reports the maximum number in that
array: