C++ variable arguments in functions
   C++ Variable argument functions are useful wherever we are not sure about the number of parameters to be passed. Not all the compilers have the ability to decipher variable arguments. But C++ compiler has the capability to decrypt a variable argument function and provide the parameter values inside the function.

   The first confusion that would arise when using a variable argument is to identify the type of the variable. The data type of the variable should be known prior to using it in a variable argument function. The algorithm/code inside the function should be able to manipulate the data properly.

   The argument values can be retrieved by using the va_arg, va_start and va_end macros. These macros assume that the function will be called with a fixed number of required parameters and variable number of optional parameters.

   The following sample program uses a function Add with variable arguments and returns the value of the added items.


#include <stdio.h>
#include <stdargs.h>

int Add(int a,int b,...)
{

//This one handles 4 arguments in total.
int l_ParamVal=0;
int total=0;
int i;

//Declare a va_list macro and initialize it with va_start
va_list l_Arg;
va_start(l_Arg,a);

//The required parameters can also be accessed directly
l_ParamVal = a;
printf("%d\n",l_ParamVal);
if(l_ParamVal != -1)
total = total +l_ParamVal;

l_ParamVal = va_arg(l_Arg,int);
printf("%d\n",l_ParamVal);
if(l_ParamVal != -1)
total = total +l_ParamVal;

l_ParamVal = va_arg(l_Arg,int);
printf("%d\n",l_ParamVal);
if(l_ParamVal != -1)
total = total +l_ParamVal;

l_ParamVal = va_arg(l_Arg,int);
printf("%d\n",l_ParamVal);
if(l_ParamVal != -1)
total = total +l_ParamVal;

va_end(l_Arg);

return total;

}

void main()
{

printf("Total of C++ Variable Arguments: %d\n",Add(2,3,4));

}


   The above sample takes some integer parameters and returns their added result. This function can handle up to 4 parameters.

Close    To Top
  • Prev Article-Programming:
  • Next Article-Programming:
  • Now: Tutorial for Web and Software Design > Programming > cplus > Programming Content
    Photoshop Tutorial
     

    Special Effect

      3D Effect
      Photoshop Articles
    Programming Tutorial
     

    C/C++ Tutorial

      Visual Basic
      C# Tutorial
    Database Tutorial
     

    MySQL Tutorial

      MS SQL Tutorial
      Oracle Tutorial
    Geek Tutorial
     

    Blogging Tutorial

      RSS Tutorial
      Podcasting Tutorial
    Graphic Design Tutorial
      Coreldraw Tutorial
      Illustrator Tutorial
      3D Tutorials
    Webmaster Articles
     

    Domain Service

      Web Hosting
      Site Promotion
    Java Tutorial/ Articles
     

    Java Servlets

      JavaEE Tutorial
     

    JavaBeans Tutorial

    XML Tutorial/ Articles
     

    XML Style

      AJAX Tutorial
      XML Mobile
    Flash Tutorial/ Articles
     

    Flash Video

      Action Script
      Flash Articles
    OS Tutorial/ Articles
      Linux Tutorial
      Symbian Tutorial
      MacOS Tutorial
    Personal Tech
      Hardware Tutorial
      Software Tutorial
      Online Auction