Previous: Portability Constraints Up: Portability Constraints Next: Data Types

Variable Arguments

On most machines, there is no way for a subroutine to tell how many arguments are actually passed into it at run time. For that reason, routines that have optional arguments are not allowed. Optional arguments are quite common in VMS VICAR because the VAX is one of the few machines that passes the number of arguments to a subroutine. These subroutines and the programs that call them will all have to be changed. The preferred solution is to give all arguments on every call to a subroutine. An alternate solution is to have several subroutine names, each with a different number of arguments. That should only be done in very unusual circumstances, however.

There is a standard way to do variable arguments in C, and that is using the <varargs> interface (see any C reference for details). That is the way printf() is done, for example. The VICAR Run-Time Library uses this mechanism to handle the ``keyword'',``value'' style of optional arguments. The key to <varargs> is that the subroutine must be able to tell how many and what type of arguments are passed in by looking at the previous arguments. For example, printf() knows what arguments to expect by examining the format string. The RTL knows what type of argument to expect by the keyword, but it has to know how many arguments to expect. This is handled by adding a terminator as the last keyword in the argument list. See Section , RTL Calling Conventions, for details. VICAR application code may use the <varargs> interface if absolutely required; however, it should generally be avoided.

There is no portable way at all to do variable arguments in Fortran. Fortran can call <varargs> routines (such as the RTL), but routines that accept variable arguments cannot be written in Fortran. In addition, code should not be written that accepts variable arguments from a Fortran routine. The RTL does this, but it uses some special tricks that are difficult to port. VICAR application code should not use these tricks.

rgd059@ipl.jpl.nasa.gov