logo
Powered by QM on a Rpi server
Home
About OpenQM
Sales and Downloads
Help and Support
About
Login

KnowledgeBase 00074: Variable Argument Lists

Last updated: 22 Jul 2016
Applies to: All versions
Search  
Top level index       Full Index Search Tips
Previous article     Next article

This article was originally published as a Tip of the Week.

Introduction

In common with some other programming languages, QM provides the concept of variable length argument lists in calls to subroutines or to public functions and subroutines in class modules.


The SUBROUTINE and FUNCTION Statements

The SUBROUTINE and FUNCTION statements have an optional VAR.ARGS qualifier:

   SUBROUTINE MYSUB(FVAR, ID, MODE) VAR.ARGS 
The number of argument variables in the subroutine declaration sets the upper limit on the number of arguments that can be passed. Where a CALL statement or function reference has fewer arguments, the unspecified trailing arguments are passed as unassigned variables.

Default values may be provided that will be used for absent arguments:

   SUBROUTINE MYSUB(FVAR, ID="$DEFAULT", MODE=0) VAR.ARGS 


The ARG.COUNT() Function

A subroutine that uses the VAR.ARGS qualifier needs to be able to test how many arguments were actually included in the call. The ARG.COUNT() function returns the total number of arguments, not including those for which default values were substituted as described above. For example, the following program fragment could be used to test whether the MODE argument in the subroutine declaration above was passed in the call.

   IF ARG.COUNT() >= 3 THEN ... 


The ARG.PRESENT() Function

An alternative to use of ARG.COUNT() is provided by the ARG.PRESENT() function to check if a specific named argument was passed.

   IF ARG.PRESENT(MODE) THEN ... 


The ARG() Function

A specific argument can be referenced by position using the ARG() function.

   TOTAL = 0 
   FOR I = 1 TO ARG.COUNT() 
      TOTAL += ARG(I) 
   NEXT I 


The SET.ARG Statement

A specific argument can be set by positional reference using the SET.ARG statement.

   SET.ARG 3, NEW.VALUE 


Public Subroutines in Class Modules

The VAR.ARGS qualifier can be used with public subroutines and functions in class modules. In this case, an additional feature is available to allow unnamed arguments by use of ... as the final argument in the declaration.

   PUBLIC SUBROUTINE MYSUB(A, B, ...) 
The values of any arguments beyond B in this example can be accessed using the ARG() function. The maximum number of arguments that can be passed is determined by the MAX.ARGS option to the CLASS statement or, if this is not present, defaults to 32.


Related Articles

None.



Please tell us if this article was helpful
Very     Slightly     Not at all
Comments
Email (optional)