Variable Function Arguments

 

SPL routines can accept a variable number of input arguments. For example:

 

/* maximum of one or more inputs */

vmax(argv)

{

    local i, s;

 

    /* 0 or 1 arg case */

    if (argc < 2)

    {

        if (argc < 1)

        {

            s = max();

        }

        else

        {

            s = max(getargv(1));

        }

    }

    else

    {

        /* initialize */

        s = max(getargv(1), getargv(2));

 

        /* compare input args */

        loop (i = 3..argc)

        {

            s = max(s, getargv(i));

        }

    }

 

    return(s);

}

 

vmax(1, 3, 2, -1, 0)

 

returns 3.

 

The argv specifier indicates the SPL function accepts variable arguments and getargv(i) returns the ith argument.