# Thread: variable arguments in functions

1. ## 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?

2. You could take the args in a STL vector. That would be the simplest solution. You can then use the member function size().

Rahul

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

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

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

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

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

8. And what would you do with the any, then?

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

10. Originally Posted by CornedBee
And what would you do with the any, then?
Don't follow, CornedBee

11. Assuming you pass a vector of boost::anys. What would you do with them inside the function?

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