9.2.3.4 Other Points About Calling Functions

Some awk implementations allow you to call a function that has not been defined. They only report a problem at runtime, when the program actually tries to call the function. For example:

BEGIN {
    if (0)
        foo()
    else
        bar()
}
function bar() { ... }
# note that `foo' is not defined

Because the ‘if’ statement will never be true, it is not really a problem that foo() has not been defined. Usually, though, it is a problem if a program calls an undefined function.

If --lint is specified (see Command-Line Options), gawk reports calls to undefined functions.

Some awk implementations generate a runtime error if you use either the next statement or the nextfile statement (see The next Statement, and see The nextfile Statement) inside a user-defined function. gawk does not have this limitation.

You can call a function and pass it more parameters than it was declared with, like so:

function foo(p1, p2)
{
    ...
}

BEGIN {
    foo(1, 2, 3, 4)
}

Doing so is bad practice, however. The called function cannot do anything with the additional values being passed to it, so awk evaluates the expressions but then just throws them away.

More importantly, such a call is confusing for whoever will next read your program.66 Function parameters generally are input items that influence the computation performed by the function. Calling a function with more parameters than it accepts gives the false impression that those values are important to the function, when in fact they are not.

Because this is such a bad practice, gawk unconditionally issues a warning whenever it executes such a function call. (If you don’t like the warning, fix your code! It’s incorrect, after all.)


Footnotes

(66)

Said person might even be you, sometime in the future, at which point you will wonder, “what was I thinking?!?”