[<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
    A User-Defined function is invoked in a pattern or action through the
    name by which it was defined.

    The function to find the maximum of two arguments would be defined:

    function max(a1,a2) {
        return a1 > a2 ? a1 : a2;
    }

    The function could be invoked as:  r = max(1,2);
    and would return 2 in this case.

    A function may be defined with  no arguments.  To define a  function to
    find the maximum value in the current input record:

    function max_input() {
        local i;
        local max = $1;

        for ( i = 2 ; i <= NF ; i++ ) if ( max < $i ) max = $i;
        return max;
    }

    This example is notable in the use of 'local' variables in the function
    definition.   The 'local'  variable 'i'  and 'max'  disappear upon exit
    from the function.

    The function  could be  invoked in  a pattern  or action  in one of two
    possible ways:

    record_max = max_input();

    or

    record_max = max_input;

    For user-defined functions with no arguments the use of the parenthesis
    is optional.

    QTAwk will  check  the  number of  arguments  passed  to a user-defined
    function.   If the  number is  less than  the number  defined, an error
    message is issued and execution  halted.  If the predefined  variable,
    _arg_chk is set to TRUE, then if the number of arguments passed is  not
    exactly equal  to the  number defined  an error  message is  issued and
    execution halted.

See Also: Defining Variable _arg_chk
This page created by ng2html v1.05, the Norton guide to HTML conversion utility. Written by Dave Pearson