An Awk Primer/User-defined Functions

Awk supports user-defined functions.

A function definition is specified outside of patterns and actions and looks like

function <name>(<comma-separated list of parameters>) { <actions> }

For example, the following program, for each record, prints sum of squares of its first two fields:


function print_sumsq(x, y) {print x*x+y*y}
NF>=2 {print_sumsq($1, $2)}

A function call should not contain any intervening whitespaces between the function name and the left parenthesis. This applies only to user-defined functions, and the goal is to avoid confusion with the concatenation operator:


function print_sumsq(x, y) {print x*x+y*y}
NF>=2 {print_sumsq($1, $2)} # Correct
NF>=2 {print_sumsq ($1, $2)} # Incorrect
NF>=2 {print(atan2($1, $2))} # Correct
NF>=2 {print(atan2 ($1, $2))} # Correct, as atan2 is not a user-defined function

In order to return a value, use the return statement:


function sumsq(x, y) {return x*x+y*y}
NF>=2 {print(sumsq($1, $2))}

In order to use local variables, they should be specified at the end of the parameter list, but not in the function call:


function sumsq(x, y,   u, v) {u=x*x; v=y*y; return u+v} # u, v are local
NF>=2 {print(sumsq($1, $2))}

By convention, local variables are separated from parameters by extra spaces.

As a GAWK extension, there is an indirect function call. It looks like this:

 @<a variable whose value is a function name>(comma-separated list of parameters>)

Example:


function print_sumsq(x, y) {print x*x+y*y}
BEGIN {myfun="print_sumsq"}
NF>=2 {@myfun($1, $2)}