×

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:

Variadic Functions Implementation in C++

Related Topics

Reverse function in C++

The function std::reverse() is included in the standard template library of C++. It takes in a beginning and ending iterator, reversing the order. To use the reverse statement, we need...

2 minutes read.

RTTI (Run-Time Type Information) in C++

In C++, RTTI or Run-Time Type Information reveals information about the data type of an object at runtime and only works with classes that have at least one virtual function....

3 minutes read.

Substring in C++

A substring is a function in c++ that is imported from the string library. An element of a string is called a substring. A substring contains two parameters length and...

2 minutes read.

The Stock Span Problem

It is necessary to determine the span of a stock's price throughout all n days in order to solve the stock span problem, which involves a set of n daily...

5 minutes read.

C++ Infinite loop

The term "infinite loop" refers to a loop that does not terminate the loop according to the condition. In some cases, an infinite loop may be required in programming, or...

4 minutes read.

C++ User Defined Exceptions

Overriding and inheriting exception class capabilities may be used to define the new exception. Exception handling can also be used with classes. We may also make an exception for user-defined...

4 minutes read.

C++ Constructor

Constructor is a specific method in C ++ that is automatically called when an object is created. Typically, it is used to set the data members of a new object....

4 minutes read.

C++ Friend Functions

In C++, friends are special functions that are not a part of a class yet have access to its private and protected members. The friend keyword is used to declare...

4 minutes read.

CPP Templates

C++ provides a powerful feature called template, which allows the definition of generic classes and generic functions. Generic programming is a technique where different algorithms work in communion by using...

4 minutes read.

C++ File Handling

File handling is a mechanism that manipulates the data stored in files. File handling store output data from the program to external file and read file data to the program. There...

3 minutes read.

Difference between OOP and POP in C++

Object-Oriented Programming (OOP) Prioritizes data over methods (functions) and treats data as an essential component of program development.  OOP prohibits the free flow of data throughout the system. Tighter ties to data manipulation...

4 minutes read.

Counting Frequencies of Array Elements in C++

We have an array of integer items with duplicate values, and our objective is to compute the frequencies of the different elements in the array. Methods: Methods that can be used to...

3 minutes read.

Features and Use of Pointers in C/C++

What is a pointer? A pointer is mainly used to store the address of another variable. The * operator creates a pointer variable, which points to a data type (like an...

7 minutes read.

C++ 11 vs C++ 14 vs C++ 17

C++ is a language that is used to create a high-performance application. C++ 11, C++ 14, and C++ 17 are the different version of C++. There are some differences between...

1 minute read.

Array of Object in C++

What is an Array? In C++, an array is a group of related data types, such as int, char, float, double, etc., that are easily accessed by index value alone and...

12 minutes read.

Program to arrange an array in alternate positive and negative numbers

Let’s say, there is given an array arr, arrange the array in such a way that every positive number is followed by a negative number. If there are extra positive...

4 minutes read.

goto statement in C and C++

goto statement in C and C++ The goto statement is a jump statement, also sometimes referred to as an unconditional jump statement. Within a function, the goto statement can be used...

3 minutes read.

How to make a password program in C++

Before understanding the password program, one must know about a password and why it is required. Password: A password is a word that permits access to somewhere or something. A password...

4 minutes read.

Structure Sorting (By Multiple Rules) in C++

To understand the concept of Structure Sorting (By Multiple Rules) in C++, it is recommended to know the Structures concept in the C++ programming language. Here the scenario is pretty...

3 minutes read.

Char Array to String in C++

Regardless of the programming language you use, data structure is critical to the success of your project. Although each programming language has its own collection of data structures, C++ contains a...

4 minutes read.