User-Defined Functions

The awk statements that we have covered so far are sufficient to write almost any data processing program. Because human programmers are poor at understanding large blocks of code, we need a way to split such blocks into manageable chunks that each perform an identifiable job. Most programming languages provide this ability, through features variously called functions, methods, modules, packages, and subroutines. For simplicity, awk provides only functions. As in C, awk functions can optionally return a scalar value. Only a function's documentation, or its code, if quite short, can make clear whether the caller should expect a returned value.

Functions can be defined anywhere in the program at top level: before, between, or after pattern/action groups. In single-file programs, it is conventional to place all functions after the pattern/action code, and it is usually most convenient to keep them in alphabetical order. awk does not care about these conventions, but people do.

A function definition looks like this:

function name(arg
            1, arg
            2, ..., arg
            n)
{
    statement(s)
}

The named arguments are used as local variables within the function body, and they hide any global variables of the same name. The function may be used elsewhere in the program by calls of the form:

            name(expr
            1, expr
            2, ..., expr
            n)             Ignore any return value

result = name(expr
            1, expr
            2, ..., expr
            n)    Save return value in result

The expressions at the point of each call provide initial values for ...

Get Classic Shell Scripting now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.