www.want-tool.org
Default Parameters
   

By Andrew J. Wozniewicz
Milwaukee, August 16, 2008

WANTScript allows defining default parameters for a module, that is, parameters with default values.

You can give a default value to a parameter of a module and then call the module as a subroutine with or without the parameter, which makes the parameter optional. To provide a default value, end the parameter declaration with the equal symbol ("=") followed by a constant expression.

For example, given the module declaration

procedure TestDefParams(A, B, C = 3)
  WriteLn("A=",A," B=",B," C=",C)
end

all the following calls are equivalent

TestDefParams(A := 1, B := 2)
TestDefParams(1, B := 2)
TestDefParams(1, 2, 3)
TestDefParams(1, 2)
TestDefParams(A := 1, B := 2, C := 3)

and each produces the following output:

A=1 B=2 C=3

Note that parameters with default values must occur at the end of the parameter list, and must be passed by value. In other words, once a default value for a parameter is provided in the list of module parameters, all remaining parameters must have default values. Parameters passed by reference cannot have default values.

When calling modules with more than one default parameter, you cannot skip parameters, like in Visual BASIC. The following example illustrates this:

program DefaultParams //Test013

  TestAllDefParams
  TestAllDefParams(4)
  TestAllDefParams(4,5)
  TestAllDefParams(4,5,6)

  //These are invalid (cannot skip parameters):
  //TestAllDefParams(4,,6)
  //TestAllDefParams(4,5,)

  procedure TestAllDefParams(A = 1, B = 2, C = 3)
    WriteLn("A=",A," B=",B," C=",C)
  end

end

The commented-out lines would be illegal and generate an "invalid syntax" compiler error.

-Andrew