Syntax
procedure parameter (argument-list)
{
statement ...
parameter = expression;
}
function parameter (argument-list)
{
statement ...
parameter = expression;
}
Description
A procedure is a named block of code that runs a sequence of one or more statements, and which may declare zero or more parameters. Each parameter is a variable that may optionally have a default value. If a parameter is declared with a default value, then all following parameters must also be declared with a default value. A procedure terminates when the final statement is run or when a return statement is run.
Variables and parameters declared within the procedure have local scope and are discarded when the procedure terminates. If an identifier is referenced within a procedure, the local scope of the procedure is checked first for a variable or parameter with a matching name. If one cannot be found, then the containing scope is checked for a variable with a matching name. If a matching variable still cannot be found, a new variable is declared, with a scope local to the procedure.
A procedure is invoked by specifying the name of the procedure and providing values for each parameter in a comma-separated argument list contained within parentheses. No argument is required if the matching parameter has a default value; in this case, the parameter will be assigned its specified default value.
A procedure may be declared using the procedure or function keywords. Historically, a function returns a value whereas a procedure does not; however, the parser will permit any procedure to return a value regardless of the keyword used. The choice of using the procedure or function keyword is stylistic. If a procedure ends without a return statement, a variable with the same name as the procedure is treated as the return value.
Examples
Procedure with no parameters:
procedure include_defaults() { include "/opt/quest/qpm4u/policies/defaults.conf"; } include_defaults();
Procedure with two parameters, one of which has a default value:
procedure process_include_file(fname, fdir="") { topdir = "/opt/quest/qpm4u/policies"; fpath = topdir + "/" + (fdir == "" ? "" : fdir + "/") + fname; if (fileexists(fpath)) { include fpath; } } process_include_file(user + ".conf"); # default value of "" is assigned to parameter fdir process_include_file(user + ".conf", "users"); # parameter fdir is assigned the value "users"
Procedure with a parameter that masks a top-level variable with the same name. This print 1,2,1:
x = 1; procedure foo(x) { print(x); } print(x); foo(2); print(x);