# variable arguments in functions

• 02-21-2008
Stonehambey
variable arguments in functions
Hi,

I can define a function like this

Code:

```int someFunction(int x, int y) {     some code }```
But I would like to have a variable argument. For example I can make the above function find the mean average of two numbers. But I would like to have it find the mean average of a variable number of numbers, namely the numbers defined in a vector in another part of the program. i.e. The user enters how many numbers he would like to find the average of, then enters those numbers, and the function finds the average. I tried something like this

Code:

```int someFunction(somePreviouslyDefinedVector) {     some code }```
But this was pretty much a stab in the dark and I wasn't surprised when it didn't compile :P

Is it a pretty complicated technique, or is it a simple fix?

• 02-21-2008
rahulss
You could take the args in a STL vector. That would be the simplest solution. You can then use the member function size().

Rahul
• 02-21-2008
laserlight
Quote:

But I would like to have it find the mean average of a variable number of numbers, namely the numbers defined in a vector in another part of the program. i.e. The user enters how many numbers he would like to find the average of, then enters those numbers, and the function finds the average.
Instead of using variable arguments, I suggest using the idiom of a range denoted by an iterator pair. One iterator points to the first element of the range, the other element points to one past the last element of the range. If the callers wants the mean of the entire vector, the caller would pass the iterators returned by vec.begin() and vec.end() for a vector named vec.

This idiom is used by the standard library containers and generic algorithms, and in fact you can already calculate the mean by using std::accumulate to calculate the sum.
• 02-21-2008
Elysia
Since the numbers are located in a vector, why not pass the vector itself, as rahulss suggests?
Other solutions are more complicated. More than they need to be, for this.
• 02-21-2008
laserlight
Quote:

Since the numbers are located in a vector, why not pass the vector itself, as rahulss suggests.
Other solutions are more complicated. More than they need to be, for this.
Passing a range would allow one the flexibility of switching to another container, and in the end one may end up using iterators within the function anyway.
• 02-21-2008
cpjust
You could use the std::accumulate() function in <numeric> to add all numbers in the range (eg. v.begin() to v.end() ), then divide by the number of elements in the range.
• 02-21-2008
Mario F.
And if, by any chance the arguments don't share a similar type you can use boost::any with the suggestion produced by laserlight by simply passing beginning and end iterators to it.
• 02-22-2008
CornedBee
And what would you do with the any, then?
• 02-22-2008
Stonehambey
I will admit that I don't fully understand most of the answers given here. But I'm still learning so maybe I was going a little over my head. Whenever I learn a new technique I always try to create an idea for a program using those techniques and sometimes I ask a bit much of myself!

Thanks to all for the kind replies :)
• 02-22-2008
Mario F.
Quote:

Originally Posted by CornedBee
And what would you do with the any, then?

Don't follow, CornedBee
• 02-22-2008
CornedBee
Assuming you pass a vector of boost::anys. What would you do with them inside the function?
• 02-22-2008
Mario F.
Yes. I see your point. Makes no sense, does it?
I would still have to convert them or find their types. Thank you for the correction.