Unspecified arguments should be a last resort. When you can't use arrays, vectors, inheritance, etc. only then is okay to use unspecified arguments.
Put your parameters inside an array and pass to the function a pointer to the array.
Now, about your question: if you read the parameter list you yourself defined, you will notice than the first parameter is the number of extra parameters being passed. Now look at your function call and notice that the first parameter is 1, the second is 2, and the third is 3.
Tell me, now: how much is 0+2?
Sorry, I am new to C++. I've pick up it from one website reference then i want to make for my own function Sum(1,2,3,... so on) the same as function in Excel.
"This FindMax functionn takes as its first parameter the amount of additional arguments it is going to get."
That is what you copied. The first parameter to your sum() function is the number of additional arguments. You call sum( 1, 2, 3 ), which means you are telling your function that there is 1 number to add, and that number is 2, since it is the next parameter. So as helios has said, 2 plus nothing is still 2.
If you call sum( 3, 1, 2, 3 ) then your function will add 1 + 2 + 3 == 6.
However as helios also suggested a far better approach is to pass an array of integers.
1 2 3 4 5 6
int sum( int a[], int count ) {
int summation = 0;
for( int i = 0; i < count; ++i )
summation += a[ i ];
return summation;
}
And there are many better ways to add numbers than this.
It's impossible. You need SOME way to tell the function in advance how many parameters you're passing. This value MUST be the first parameter, no matter what.
This is the nearest that can be done (which you already have):
sum(1,1)==1
sum(2,1,2)==3
sum(3,1,2,3)==6
sum(4,1,2,3,4)==6
There's no point in doing what you ask, anyway, because the parameters can't be changed dynamically (during run-time).
Just pass a pointer to an array or to a vector. That's all you need and all you'll ever need, trust me.