8.10.2 Function Parameters | |
Parameters to script functions are declared in the <A nbsp;NAME> tag, after the NAME and scope attributes, and without a $ sign:
<A NAME=top PRIVATE title="" bg="white"> <HEAD><TITLE>$title</TITLE></HEAD> <BODY BGCOLOR=$bg> <H1>$title</H1> </A> |
Here the function top is declared, whose purpose is to print out header HTML at the top of a page. It takes the parameters title and bg , which have the default values of empty and white respectively. We can call the function in various ways:
<top title="Introduction" bg=yellow> <top title="Section Five"> <top> <top bg=red title=$mytitle> |
The first call above gives us the Introduction title on a yellow background.
Since both parameters have default values, either can be omitted. The second call does not specify bg , so it defaults to white .
The third call leaves both parameters to their defaults.
Named parameters can be specified in any order, like HTML attributes, so they can be reversed, as in the fourth call.
Function parameters have local scope, which means they are only visible in the function they are declared in, not to any sub-functions that it may call. Parameters only exist while the function is running: once it ends, the parameters are destroyed. If the function is re-entered before it ends (recursion), then another, distinct set of parameters is created for each call. If a global variable of the same name as a parameter was already in use, the parameter temporarily "masks" the global for the duration of the function call: only the parameter is visible.
By using parameters instead of global variables, a programmer can ensure a function is a "black box": all its data is clearly passed at the call, and any modification the function does on its parameters won't affect the caller.
The default value for a parameter can also be a variable:
<A NAME=bottom link=$HomePage> Go to the <A HREF="$link">next page</A>. </A> |
Here the default value for the parameter link is the global variable $HomePage . (Variable defaults are always taken as globals, even if a local variable of the same name exists at the call to <bottom> .)
If no default value is specified for a parameter, then it must be explicitly set when the function is called. For example, we could change the top function to require a title:
<A NAME=top title bg="white"> <HEAD><TITLE>$title</TITLE></HEAD> <BODY BGCOLOR=$bg> <H1>$title</H1> </A> |
Now if we call top we must specify the title parameter, though bg is still optional. If an entry function has parameters with no defaults, they are initialized from the same-name global variable.
Parameters to user functions are always unnamed, and therefore must be given in a specific order. Builtin functions have a mix of named and unnamed parameters.
With a <LOCAL> tag, we can declare variables that have the same local nature as parameters, but we can limit the scope to any part of a function, instead of the whole function:
<A NAME=test> ... no x y z ... <LOCAL x="" y z> ... x y z exist here ... </LOCAL> ... no x y z ... </A> |
The ending </LOCAL> tag is optional: without it the scope ends at the next ending block tag (eg. </LOOP> , </A> etc.) Local variables are useful for scratch-space computation: inside the block we can safely use $x , $y and $z without fear of destroying a global variable of the same name.
Back: Function Scope | Next: Pass by Value vs. Pass by Reference |