gawk: Return Statement

1 
1 9.2.4 The 'return' Statement
1 ----------------------------
1 
1 As seen in several earlier examples, the body of a user-defined function
1 can contain a 'return' statement.  This statement returns control to the
1 calling part of the 'awk' program.  It can also be used to return a
1 value for use in the rest of the 'awk' program.  It looks like this:
1 
1      'return' [EXPRESSION]
1 
1    The EXPRESSION part is optional.  Due most likely to an oversight,
1 POSIX does not define what the return value is if you omit the
1 EXPRESSION.  Technically speaking, this makes the returned value
1 undefined, and therefore, unpredictable.  In practice, though, all
1 versions of 'awk' simply return the null string, which acts like zero if
1 used in a numeric context.
1 
1    A 'return' statement without an EXPRESSION is assumed at the end of
1 every function definition.  So, if control reaches the end of the
1 function body, then technically the function returns an unpredictable
1 value.  In practice, it returns the empty string.  'awk' does _not_ warn
1 you if you use the return value of such a function.
1 
1    Sometimes, you want to write a function for what it does, not for
1 what it returns.  Such a function corresponds to a 'void' function in C,
1 C++, or Java, or to a 'procedure' in Ada.  Thus, it may be appropriate
1 to not return any value; simply bear in mind that you should not be
1 using the return value of such a function.
1 
1    The following is an example of a user-defined function that returns a
1 value for the largest number among the elements of an array:
1 
1      function maxelt(vec,   i, ret)
1      {
1           for (i in vec) {
1                if (ret == "" || vec[i] > ret)
1                     ret = vec[i]
1           }
1           return ret
1      }
1 
1 You call 'maxelt()' with one argument, which is an array name.  The
1 local variables 'i' and 'ret' are not intended to be arguments; there is
1 nothing to stop you from passing more than one argument to 'maxelt()'
1 but the results would be strange.  The extra space before 'i' in the
1 function parameter list indicates that 'i' and 'ret' are local
1 variables.  You should follow this convention when defining functions.
1 
1    The following program uses the 'maxelt()' function.  It loads an
1 array, calls 'maxelt()', and then reports the maximum number in that
1 array:
1 
1      function maxelt(vec,   i, ret)
1      {
1           for (i in vec) {
1                if (ret == "" || vec[i] > ret)
1                     ret = vec[i]
1           }
1           return ret
1      }
1 
1      # Load all fields of each record into nums.
1      {
1           for(i = 1; i <= NF; i++)
1                nums[NR, i] = $i
1      }
1 
1      END {
1           print maxelt(nums)
1      }
1 
1    Given the following input:
1 
1       1 5 23 8 16
1      44 3 5 2 8 26
1      256 291 1396 2962 100
1      -6 467 998 1101
1      99385 11 0 225
1 
1 the program reports (predictably) that 99,385 is the largest value in
1 the array.
1