manpagez: man pages & more
info gawk
Home | html | info | man

File: gawk.info,  Node: Function Caveats,  Prev: Pass By Value/Reference,  Up: Function Calling

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 (*note 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 (*note Next
Statement::, and *note 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.(1)  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 ----------

   (1) Said person might even be you, sometime in the future, at which
point you will wonder, "what was I thinking?!?"

© manpagez.com 2000-2025
Individual documents may contain additional copyright information.