7.11. User-Defined Functions

A user-defined function can be placed anywhere in the script that a pattern action rule can.

Format

function name ( parameter, parameter, parameter, … ) { 
   statements
   return expression
					(The return statement and expression are optional )
}

Variables are passed by value and are local to the function where they are used. Only copies of the variables are used. Arrays are passed by address or by reference, so array elements can be directly changed within the function. Any variable used within the function that has not been passed in the parameter list is considered a global variable; that is, it is visible to the entire awk program, and if changed in the function, is changed throughout the program. The only way to provide ...

Get Linux Shells by Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.