Functions Summary (The GNU Awk User’s Guide)
From Get docs
Gawk/docs/latest/Functions-Summary
Previous: Indirect Calls, Up: Functions [Contents][Index]
9.4 Summary
awk
provides built-in functions and lets you define your own functions.- POSIX
awk
provides three kinds of built-in functions: numeric, string, and I/O.gawk
provides functions that sort arrays, work with values representing time, do bit manipulation, determine variable type (array versus scalar), and internationalize and localize programs.gawk
also provides several extensions to some of standard functions, typically in the form of additional arguments. - Functions accept zero or more arguments and return a value. The expressions that provide the argument values are completely evaluated before the function is called. Order of evaluation is not defined. The return value can be ignored.
- The handling of backslash in
sub()
andgsub()
is not simple. It is more straightforward ingawk
’sgensub()
function, but that function still requires care in its use. - User-defined functions provide important capabilities but come with some syntactic inelegancies. In a function call, there cannot be any space between the function name and the opening left parenthesis of the argument list. Also, there is no provision for local variables, so the convention is to add extra parameters, and to separate them visually from the real parameters by extra whitespace.
- User-defined functions may call other user-defined (and built-in) functions and may call themselves recursively. Function parameters “hide” any global variables of the same names. You cannot use the name of a reserved variable (such as
ARGC
) as the name of a parameter in user-defined functions. - Scalar values are passed to user-defined functions by value. Array parameters are passed by reference; any changes made by the function to array parameters are thus visible after the function has returned.
- Use the
return
statement to return from a user-defined function. An optional expression becomes the function’s return value. Only scalar values may be returned by a function. - If a variable that has never been used is passed to a user-defined function, how that function treats the variable can set its nature: either scalar or array.
gawk
provides indirect function calls using a special syntax. By setting a variable to the name of a function, you can determine at runtime what function will be called at that point in the program. This is equivalent to function pointers in C and C++.
Previous: Indirect Calls, Up: Functions [Contents][Index]