You specify a variable number of function arguments as follows:
varargs [of data-type] list-id
Variable | Description |
---|---|
data-type | The data type of the variable arguments. |
list-id | An identifier specifying the name of the list variable that holds the variable arguments. |
Regardless of how many arguments are passed to a function, each argument is stored in a list named list-id that is accessible to the function.
Following are several examples of functions.
The ProcName function declaration below specifies that the second argument can be NULL and that the third argument is optional. The test case that follows calls ProcName.
[-] ProcName(BOOLEAN b, STRING sName NULL, INTEGER iStart optional) [-] if(sName == NULL) [ ] sName = "Smith" [-] if(iStart == NULL) [ ] iStart = 0 [ ] Print(b, sName, iStart) [ ] [-] testcase null_and_optional_example() [ ] BOOLEAN b = TRUE [ ] INTEGER i [ ] STRING s [ ] i = 1 [ ] s = "foo" [ ] ProcName(b, s, i) // Prints: TRUE foo 1 [ ] s = NULL [ ] ProcName(b, s) // Prints: TRUE Smith 0 [ ] b = NULL [ ] ProcName (b, s, i) // This statement causes an exception because [ ] // the first arg to ProcName cannot be NULL
The function MyFunc1 uses different argument pass modes. The test case calls MyFunc1.
[-] BOOLEAN MyFunc1(STRING s, out INTEGER iNew, inout INTEGER iChange) [ ] s = "bar" [ ] iNew = 1234 [ ] iChange = iChange + 1 [ ] return TRUE [ ] [-] testcase pass_modes_example() [ ] STRING sIn = "foo" [ ] INTEGER iOut [ ] INTEGER iInOut = 1 [ ] BOOLEAN bReturnValue [ ] bReturnValue = MyFunc1(sIn, iOut, iInOut) [ ] // prints: foo 1234 2 TRUE [ ] Print(sIn, iOut, iInOut, bReturnValue)
The function MyFunc2 is declared to take a variable number of arguments.
[-] MyFunc2(INTEGER iTestNum, varargs OF STRING lsArg) [ ] Print(iTestNum, lsArg)