Click here to Skip to main content
15,884,628 members
Articles / Programming Languages / C++
Article

Variable Arguments in Functions

Rate me:
Please Sign up or sign in to vote.
1.70/5 (11 votes)
23 Jun 2004 39K   14   3
Simple brief on how to use variable arguments.

Introduction

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.

Variable Arguments

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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMust specify the number of args Pin
MontgomeryBurns17-Mar-05 22:11
MontgomeryBurns17-Mar-05 22:11 
GeneralRe: Must specify the number of args Pin
Muthukumar18-Mar-05 0:17
Muthukumar18-Mar-05 0:17 
Actually that is why in the last line it says "This function can handle up to 4 parameters.".

I think the problem is that, it was not highlighted in bold or some other style.

Moreover this was just supposed to be a very simple sample program.

Thanks for the feedback anyway.

Muthu
GeneralRe: Must specify the number of args Pin
MontgomeryBurns18-Mar-05 0:41
MontgomeryBurns18-Mar-05 0:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.