In the previous article, we have discussed about How to Measure Execution Time in CPP?. Let us learn How to Pass Variable Arguments in a function in C++ Program.
You may encounter a situation where you would like a function that will take a variable number of arguments, i.e., parameters, instead of a predefined number of parameters. The C/C++ programming language has a solution for this scenario, and you can specify a function that accepts a variable number of parameters based on your needs.
In this article, we will look at how to make a function a variable number of arguments.
Passing Variable Arguments in a function
- Variable Arguments
- Defining a function that can take variable arguments
- Reading Variable Arguments Within a Function
- Internal working of variable arguments
1)Variable Arguments
Create a function that returns the product of all the arguments passed to it.
findProduct(4, 1, 4, 3 ,2 ); // the total count of arguments passed after the primary arguments // is represented by the first argument. This function will // return 24 findProduct(5, 4, 5, 2, 3, 6); // the total count of arguments passed after the primary arguments // is represented by the first argument. This function will // return 720
As you can see in the preceding example, the function findProduct() accepts a variable number of arguments and uses them to display the answer.
As a result, we must write a function that can accept and use a variable number of arguments.
2)Defining a function that can take variable arguments
int findProduct(int number_of_Elements, ...);
Here… stands for “ellipsis,” which describes a variable number of claims.
Importance: The first argument is required in a function that accepts a variable list of arguments.
Also, the last argument in a function should be a variable number of arguments represented by… (ellipsis).
3)Reading Variable Arguments Within a Function
Using var_list, make a list of variable arguments
var_list varList;
Now use var_start() to start the list
var_start( varList, number_of_Elements );
The first argument is a varlist List, and the second argument is an argument passed just before the “…” in the function.
The varlist is started with this function var_start().
Now use the va_arg() function to read the first entry from this varList.
int k = va_arg(varList, int);
This function takes a va list and a form of argument as input and returns the next entry in the list.
By calling this function in a loop, we can iterate over all of the variable arguments passed to it.
However, how do we know how many elements are passed in this variable list?
We’ll use the first statement passed, noOfElements, for this. The number of claims in this statement will be determined by the user.
Below is the implementation:
#include <bits/stdc++.h> #include <cstdarg> using namespace std; int findProduct(int number, ...) { va_list var_list; int product = 1; int i; // Initialize the variable_list for the number of // arguments numbers. va_start(var_list, number); for (i = 0; i < number; i++) { product *= va_arg(var_list, int); } // clean memory reserved for variable_list va_end(var_list); return product; } int main() { cout << "Product = " << findProduct(5, 2, 3, 4, 5, 6) << endl; cout << "Product = " << findProduct(3, 6, 7, 5) << endl; }
Output:
Product = 720 Product = 210
4)Internal working of variable arguments
When we transfer an argument to a function, all of the arguments are stored in a stack, and if we know the address of the variable argument’s starting point, we can easily retrieve the next value.
As a result, in va start, we must move the argument that just precedes the variable arguments so that it can calculate the starting address of the variable arg list.
Then, in va arg, we must move the form of the next argument so that it can only fetch certain bytes to create the argument.
Related Programs:
- passing variable arguments to a function in java using varargs tutorial and example
- how to erase elements from a list in cpp using iterators
- cpp how to reverse a list or sub list in place
- python args how to pass multiple arguments to function
- python how to use global variables in a function
- how to copy all values from a map to a vector in cpp
- cpp how to find duplicates in a vector