×

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

Object in C++

In this article, we will learn about Object in C++. In short, an object is a stateful entity with behaviour. Data is referred to as state, and functionality is referred to...

3 minutes read.

Quick Sort in C++

Quick sort is an efficient, in-place, comparison-based sorting algorithm that uses a divide-and-conquer strategy to sort an array or list of elements. First a pivot element is selected from the...

4 minutes read.

C++ Variable

In this article, we will discuss variables in C++ with their types and examples. What are Variables? Variables are specific memory storage spaces that hold a value. During the execution of a...

4 minutes read.

C++ Deque

Definition: Deque or the Doubly ended queue is a data structure or operation performed under queue where insertion and deletion are allowed at both ends. A deque is an ordered collection of...

5 minutes 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.

Array program in C++

What is an Array? An array is a set of identically typed elements that are organized into contiguous memory locations and each element can be independently accessed using an index. We can...

16 minutes read.

Use of Inheritance in C++

What is inheritance in c++? Inheritance is fundamental in object-oriented programming (OOP) languages like C++. It allows a programmer to create a new class (called a derived class) that inherits the...

4 minutes read.

C++ If-else-if

Introduction: If-else-if control statement is an if statement used with an optional else if control statement to check multiple conditions. In this control statement, when any one of the condition returns...

4 minutes read.

Star pattern in C++ using For Loops

Star patterns are one of the most extensively utilized patterns in any programming language since they help to increase logical thinking and flow control understanding.In the C++ programming language, you...

3 minutes read.

Hashing in C++

Before understanding hashing, we need to know what the use of hashing is. Let us consider an example of a library which consists of many books.Having many books, searching for...

6 minutes read.

Maps in C++

Maps: Maps in C++ are the containers associated with key and mapped values. By keys and mapped values, we mean that the maps are used to store elements formed by the...

4 minutes read.

How to Use Getline in C++

What is getline in C++? Similar to the cin function, which allows the programmer to take input from the user getline() function also takes input from the user. But in this...

4 minutes read.

C++ vs C#

What exactly is C++ programming? Bjorne Stroustrup is the creator of the C++ programming language. His goal was to create a powerful object-oriented programming language with the capabilities of C. It...

4 minutes read.

Parameterize Constructor

C++ Parameterized Constructor A constructor having parameters is known as parameterize constructor. Parameterize constructor is used to assign different values. Syntax: className(data-type argument){   // Constructor definition   }   className(data-type argument, data-type argument){   // Constructor definition   } A parameterized constructor can be passed values to constructor function in two ways: 1)...

1 minute read.

C++ String Concatenation

C++ String Concatenation In this section, we will learn about C ++ String Concatenation, what it does, how it works, and will also see its programs. What is the String Concatenation? The + operator...

3 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.

fscanf() Function in the C++

In the C++ programming language, the fscanf() method can be used to read data from a file stream. Syntax: The syntax for the fscanf() function in the C++ programming language is as...

3 minutes read.

Reverse an Array in C++

The many approaches to reverse an array in the C++ programming language will be discussed in this section. The term "reverse of an array" refers to changing the order of...

8 minutes read.

C++ Try-Catch

Every useful program will eventually encounter unexpected outcomes. By entering data that are incorrect, users might create mistakes. Sometimes the program's creator didn't consider all of the options or was...

7 minutes read.

C++ Writing to file

In file handling, write() function is used to write data into the file. The write() uses ofstream or fstream library to write into the file. Syntax file-stream-class   file-stream-object;   file-stream-object.write((char *)&var , sizeof (var)); The write() takes two arguments. The first argument is the address of variable var...

2 minutes read.