C++ vardiac() function
In the C++ programming language, the flexibility feature is provided by the variadic function. To understand more about flexibility, let's see the following syntax.
Syntax:
If we have to add two numbers, the syntax should be like this:
int addNumbers( int nNumberOne, int nNumberTwo )
{
return nNumberOne + nNumberTwo;
}
If we have to add three numbers, the syntax should be like this:
int addNumbers( int nNumberOne, int nNumberTwo, int nNumberThree )
{
return nNumberOne + nNumberTwo + nNumberThree;
}
As we need to add more digits, we have to keep adding the number of arguments in the function. But after this, the code becomes very lengthy and hard to maintain. So, C++ provides a unique feature to overcome this problem, i.e. variadic function. When we do not know the total number of arguments at that moment, we take the help of the variadic function. One variadic function can handle any number of arguments.
The built-in function already uses the concept of variadic function in C++.
For example, if we want to print a number, the syntax should be like:
printf(" the one number = %d", nOneNumber);
If we want to print two numbers, the syntax should be like:
printf(" the first number = %d, the second number =%d ", nOneNumber, nSecondNumber);
Also, we can see the implementation of the variadic function in stdio.h.
C Variadic Macros
First of all, we have to know the following things before knowing the variadic function.
These things are as follows:
- va_list
- va_start
- va_arg
- va_end
- va_copy
When we see the implementation of the variadic function, we should know that a variation or change must exist. The variation is that we are going to deal with the number of unknown arguments. There are two parts of the variadic function. These two are optional and mandatory arguments.
In C++, there is a need for at least one mandatory argument.
va_list
When there is an optional argument, va_list is used to implement that argument. The syntax of va_list is:
va_list someArgumentPointer;
The most appropriate thing is that we have to assign the type of data in the argument list.
va_start
With the help of va_start, we can connect ArgumentPointer with the argument list and also, we have to know how many elements are present in that function. The syntax for va_start is:
va_start( someArgumentPoiner, numberOfElements );
va_arg
With the help of va_arg macros, we can extract the data from the currently connected argument list. We also have to know what type of data is available in the list. The syntax for va_arg is:
va_arg( someArgumentPointer, someType );
va_end
With the help of va_end, we can stop some ArgumentPointer. It can also reset the position of the element in function lists. The syntax for va_end is:
va_end( someArgumentPointer );
va_copy
With the help of va_copy macros, we can save our current location. The syntax for va_copy is:
va_copy( va_list argumentDestination, va_list argumentSource );
In order to state that we have variadic function we need to state three dots as well, however that will be shown afterwards.
Example:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
int
addingNumbers( int nHowMany, ... )
{
int nSum =0;
va_list intArgumentPointer;
va_start( intArgumentPointer, nHowMany );
for( int i = 0; i < nHowMany; i++ )
nSum += va_arg( intArgumentPointer, int );
va_end( intArgumentPointer );
return nSum;
}
int
main( int argc,
char** argv)
{
system( "clear" );
printf( "\n\n Implementation of Variadic functions: \n\n" );
printf( "\n 7 + 5 = %d ", addingNumbers( 2, 7, 5 ) );
printf( "\n 3 + 6 + 9 = %d ", addingNumbers( 3, 3, 6, 9 ) );
printf( "\n 11 + 45 + 3 + 80 = %d ", addingNumbers( 4, 11, 45, 3, 80 ) );
printf( "\n\n" );
return EXIT_SUCCESS;
}
Output:
