Skip to main content

Specifying Method Arguments: Basics

Specifying Method Arguments: Basics

A method can take any number of arguments. The method definition must specify the arguments that it takes. It can also specify the type and default value for each argument. (In this context, type refers to any kind of class, not specifically data type classes.)

Consider the following generic class method definition:

ClassMethod MethodName(Arguments) as Classname [ Keywords]
{
 //method implementation
}

Here Arguments has the following general form:

argname1 as type1 = value1, argname2 as type2 = value2, argname3 as type3 = value3, [and so on]

Where:

  • argname1, argname2, argname3, and so on are the names of the arguments. These names must follow the rules for variable names.

  • type1, type2, type3, and so on are class names. This part of the method definition is intended to describe, to programmers who might use this method, what type of value to pass for the corresponding argument. Generally it is a good idea to explicitly specify the type of each method argument.

    Typically the types are data type classes or object classes.

    The class name can be a fully qualified class name or a short class name. For details, see Package Use When Referring to Classes.

    You can omit this part of the syntax. If you do, also omit the as part.

  • value1, value2, value3, and so on are the default values of the arguments. The method automatically sets the argument equal to this value if the method is called without providing a value for the argument.

    Each value can either be a literal value ("abc" or 42) or an ObjectScript expression enclosed in curly braces. For example:

    ClassMethod Test(flag As %Integer = 0)
    {
     //method implementation
    }

    For another example:

    ClassMethod Test(time As %Integer = {$horolog} )
    {
     //method implementation
    }

    You can omit this part of the syntax. If you do, also omit the equals sign (=).

For instance, here is a Calculate() method with three arguments:

ClassMethod Calculate(count As %Integer, name, state As %String = "CA")
{
 // ...
}

where count and state are declared as %IntegerOpens in a new tab and %StringOpens in a new tab, respectively. By default, arguments are of the %StringOpens in a new tab data type, so that an argument of unspecified type is a %StringOpens in a new tab. This is the case for name in the above example.

FeedbackOpens in a new tab