Skip to main content

  | मौसम…

411 समाचार
Public archive edition · Attributed source reporting with original dates · AI drafts are excluded

411 REFERENCE · Wikibooks

An Awk Primer/User-defined Functions

Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.

Awk supports user-defined functions. Reusing code 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 print_sumsq ($1, $2); # Incorrect print(atan2($1, $2)); # Correct 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)) } Local variables 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, v are local u = x * x; v = y * y; return u + v } NF >= 2 { print(sumsq($1, $2)) } By convention, local variables are separated from parameters by extra spaces. Indirect function calls 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) }