awk

AWK sorting function: the l and r parameters, what are they really about?


In AWK, in order to sort an array, I can define my own sorting function, both to control the traversal of an array in a for loop, or to call asort or asorti. The signature of the comparision function accepts up to 6 parameters, although the last two are optional:

function cmp_field(i1, v1, i2, v2, l, r) { whatever; }

What are the purpose of l and r? As I understand, they are just placeholders that are not used or inspected by AWK at all, but rather a convenience for myself so that I can store values inside them, as some sort of convenience helper variables. Like assigning in l and r temporary values calculated from v1 and v2. However, why do I need them or what's its purpose, if I can just declare my own variables like:

function cmp_field(i1, v1, i2, v2) { 
   l = computation(v1);
   r = computation(v2);
   whatever; 
}

Is there any other difference besides l and r being global variables in the second example? Or is there any other reasons besides that?


Solution

  • Function parameters are local. Other variables are global.

    From the specification:

    Variables and Special Variables

    Variables can be used in an awk program by referencing them. With the exception of function parameters (see User-Defined Functions ), they are not explicitly declared. Function parameter names shall be local to the function; all other variable names shall be global. The same name shall not be used as both a function parameter name and as the name of a function or a special awk variable. The same name shall not be used both as a variable name with global scope and as the name of a function. The same name shall not be used within the same scope both as a scalar variable and as an array. Uninitialized variables, including scalar variables, array elements, and field variables, shall have an uninitialized value. An uninitialized value shall have both a numeric value of zero and a string value of the empty string. Evaluation of variables with an uninitialized value, to either string or numeric, shall be determined by the context in which they are used.

    and:

    User-Defined Functions

    The awk language also provides user-defined functions. Such functions can be defined as:

    function name([parameter, ...]) { statements }

    A function can be referred to anywhere in an awk program; in particular, its use can precede its definition. The scope of a function is global.

    The number of parameters in the function definition need not match the number of parameters in the function call. Excess formal parameters can be used as local variables. If fewer arguments are supplied in a function call than are in the function definition, the extra parameters that are used in the function body as scalars shall evaluate to the uninitialized value until they are otherwise initialized, and the extra parameters that are used in the function body as arrays shall be treated as uninitialized arrays where each element evaluates to the uninitialized value until otherwise initialized.