9.2.3.2 Controlling Variable Scope

There is no way to make a variable local to a { … } block in
awk, but you can make a variable local to a function. It is
good practice to do so whenever a variable is needed only in that
function.

To make a variable local to a function, simply declare the variable as
an argument after the actual function arguments
(see Definition Syntax).
Look at the following example where variable
i is a global variable used by both functions foo() and
bar():

Besides scalar values (strings and numbers), you may also have
local arrays. By using a parameter name as an array, awk
treats it as an array, and it is local to the function.
In addition, recursive calls create new arrays.
Consider this example:

At level 4, index 3 is not found in a
At level 4, index 4 is found in a
At level 3, index 2 is not found in a
At level 3, index 3 is found in a
At level 2, index 1 is not found in a
At level 2, index 2 is found in a