Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

How Do I Write A C++ VARIADIC Function using va_start, va_arg, va_end Macros?

1. Introduction to VARIADIC Function

Variadic Function means a C/C++ function which takes a variable number of arguments. Think about the printf() function, which can take one are more parameters. Can you imagine how it can be implemented? Function overloading? No. C or C++ do this through Variable Argument List. The C printf function collects the arguments in a list and inside the function and then it will read the list to get all the parameters. In this example, we will write code to calculate the summation of numbers passed as a variable number of argument by raising its power to X. We pass the X as a standard parameter. We will also see how va_start, va_end, va_arg and va_list macros plays important role in extracting the variable number of parameters.

2. Syntax for VARIADIC Function

Let us say, you want to get minimum from the list of numbers passed as an individual argument to a function. Have a look at the function usage:

Calling VARIADIC Function (va_start, va_end, va_arg are used to pull the parameters)

Calling VARIADIC Function (va_start, va_end, va_arg are used to pull the parameters)

We declare a long variable in the first line. In the next statement, we call

GetMin

function with Five arguments. We call the same function with Eight number of Arguments in the third line. Here, the function

GetMin

is

Variadic

as it takes the various number of parameters. So when a function is a

Variadic

function, you can pass N number of arguments to it. In the above example, the first argument tells, how many numbers of arguments passed to the function followed by an exact number of arguments. The function will pick the minimum among the passed in Parameter List. Here, the first argument is Standard Parameter and we call the next coming arguments as a Parameter List. The below picture shows

GetMin

declaration:

Variadic Function Syntax Explanation

VARIADIC Function Syntax Explanation

In the above picture, we are seeing the

Variadic

function declaration. In our case, the function returns long data type and the name of the function is

GetMin

. C++ calls the first parameter ”

nop

” as a Standard Parameter or as a Named Parameter. The three dots which can also be called as an ellipsis signifies the Variable Argument List. That means, after passing the standard argument, one can pass ‘N’ number of arguments as shown in the first picture.

3. Va_start, Va_arg and Va_end

The

va_start

,

va_arg

and

va_end

macros are used to process the variable argument list passed to the function. Va_start will perform the population of

va_list

with the passed-in variable argument list. Va_arg will read the parameter from the current position in the list and advances the position to next one. Va_end is used to clear the

va_list

stating that the arguments are processed in the called function. Have a look at the below picture:

The va_list

The va_list

Here, the three dots state the Variable Argument List. C++ fills

va_list

with the arguments in the place-holder of ellipsis during the

va_start

function call.

4. Variadic Function C++ Example

4.1 Headers Required

First, we include the required headers in the CPP file. The header file

math.h

is included to use the pow function which raises a number to a power say x. To process the Variadic Argument List, we include the header file

stdarg.h

in our example. Below is the code change:

4.2 Variadic Method – Summation

The function Power_Summation do summation by raising each number to a power of the second passed-in argument. In the below-given code, the first argument

No_Params

tells how many Variadic arguments passed to the function. We use the second argument to raise each number to power N. After raising the power of individual argument the function also performs summation.

The param_list used here holds the variable number of arguments passed to the function. We set up this list by using the

va_start

macro. The first argument expected by the macro is the

va_list

to hold the variable number of arguments. The second argument passed to the macro is the last standard parameter, which is preceding the Variadic Arguments. In our case it is

RaiseTo

. Now the macro knows where the Variadic Argument starts, as it knows where the standard arguments end.

We form the For Loop by making use of the first parameter passed to the function. We pull the arguments one-by-one in each iteration using the macro

va-arg

. The

va_arg

macro will extract the variable in the current position and moves the extractor to next position in the Variadic List (

va_list

). Note,

va_arg

expects the

va_list

and the data type of the argument. In our case, we are passing double as the second argument to the

va_arg

list. After extracting the function arguments, we perform a cumulative summation. The

va_end

macro will clear the

va_list

. Below is the completed function:

4.3 Calling Code

In the Main() function, you can see the Variadic Function Power_Summation is called twice. When we call it for the first time, we pass three Variadic float parameters. And in the second case, we pass four Variadic Arguments. The first two parameters are standard parameters. First one tells the number of arguments and the second one gives the power argument. Below is the code:

Output of the above program is shown below:

Program Output

Program Output

Categories: C++

Tags: , , ,

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.