How to Use Variable Parameter Functions in C++

Most functions accept a fixed number of arguments. Some applications, however, require functions to accept variable parameters. A well-known example is the print function of C. For such cases, C++ offers a powerful solution that lets the compiler decipher the arguments at run time. Read on for a tutorial that demonstrates how to implement such functions.

Things You'll Need

  • Intermediate C++
  • C++ compiler with IDE, such as Microsoft Visual C++

Instructions

    • 1

      Add the STDARG.H library file into your program. That's where you define the macros. On the first line of the file, insert #include < stdarg.h >.

    • 2

      Use the macros va_arg, va_start and v_end that collaborate with the va_type in stdarg.h.

    • 3

      Establish a variable of type va_list to function as a list marker.

    • 4

      Access the individual parameters by using the va_arg macro. Find a way to tell the va_arg macro what type of argument you want it to retrieve to supply the right number of bytes from the stack.

    • 5

      Cast the result from Step 4 to the type you want it to be.

    • 6

      Call the va_end macro to end the processing of variable parameters.

    • 7

      Look at the example below that demonstrates Steps 2 to 6. Function myprintf accepts a format string that specifies the type and position of each argument. Following the format string is the list of arguments. The function prints the arguments. For simplicity it can only process int, float and char:

      // declaration

      void myprintf( char format, ... );

      // defintion

      void myprintf( char
      format, ... ){

      va_list vl;

      int i;

      va_start( vl, args );

      for( i = 0; args[i] != '\0'; ++i ){

      union any_t {

      int i;

      float f;

      char c;

      } any;

      if( args[i]=='i' ){

      any.i = va_arg( vl, int );<br>
      printf( "%i\n", any.i );<br>

      }else

      if( args[i]=='f' ){

      any.f = va_arg( vl, float );<br>
      printf( "%f\n", any.f );<br>

      }else

      if( args[i]=='c' ){

      any.c = va_arg( vl, char );<br>
      printf( "%c\n", any.c );<br>

      }

      else{

      throw SomeException;

      }

      va_end( vl );

      }

Related Searches

Resources

You May Also Like

  • How to Declare a Static Variable in C

    Static variables are a useful part of C. They give you flexibility in making functions which return more complex data, but they...

  • How to Initialize Variables in C

    C does not initialize variables automatically, so if you do not initialize them properly, you can get unexpected results. Fortunately, C makes...

  • How to Use Functions in C++

    A function is code that can be executed repeatedly in a program. A C++ function consists of a label, an input argument...

  • How to Declare a Function in C

    Functions in C are the key to manageable structured programming. Every good program is written by taking the task and dividing it...

  • How to Graph Parametric Curves

    A mathematical parameter is a variable that is used to express a relationship between two or more other variables, but which does...

  • How to Use Command Line Parameters in C++

    Command-line parameters are special words that convey technical information to a program during launch. A user can pass them externally from the...

  • How to Use Void Pointers in C

    Pointers are one of the most powerful -- and most difficult -- features of the C programming language. When working with pointers,...

  • What Is the F Parameter for Chkdsk?

    Chkdsk is a DOS command that checks the computer's hard disks for errors including bad sectors and cross-linked files. The "F" parameter...

  • How to Pass Pointers to Functions

    Define the pointer type in the function arguments list by placing an asterisk after the variable type, directly before the variable's name....

  • List of Class C Felonies

    Felony classification depends on the state, and every state decides the specific minimum and maximum punishable sentences. Under federal law, class C...

  • How to Solve a Differential Equation (Separation of Variables)

    Differential equations in the form dy/dx = f(y)g(x) can be solved by separation of variables. If you are stuck on a math...

  • How to Use Callback Function in C

    Callback functions are advanced features in C programming language. A callback function helps in isolating the implementation of a library function from...

Related Ads

View Blog Post

Enter the Geek Vs Geek Giveaway and Win a Roku!