469,887 Members | 1,945 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,887 developers. It's quick & easy.

Passing parameters to a function

How do I write a function where the number of parameters it takes
varies?

This is what I have but it doesnt work.

// function prototype
void functionThree(int num1=1, int num2=2, int num3=3);

int main()
{
int number1 = 10;
int number2 = 20;
int number3 = 30;

cout << "Values when passing no parameters:\n";
functionThree( , , );

cout << endl << endl;

cout << "Values when passing one parameter:\n";
functionThree(number1, , );

cout << endl << endl;

cout << "Values when passing two parameters:\n";
functionThree(number1, number2, );

cout << endl << endl;

cout << "Values when passing all three parameters:\n";
functionThree(number1, number2, number3);

return 0;
}

void functionThree(int num1, int num2, int num3)
{
int sum;
sum = 0;

cout << num1 << endl
<< num2 << endl
<< num3 << endl;

sum = num1 + num2 + num3;

cout << num1 << " + " << num2 << " + " << num3
<< " = " << sum << endl;
}

--
Thanks,
Chris

Nov 7 '07 #1
2 1725


To call the function with our opassing the parameter:

functionThree();
functionThree(78);
functionThree(56,23);
functionThree(100,200,300);

Nov 7 '07 #2
On Nov 6, 8:46 pm, Martin.YorkAma...@gmail.com wrote:
To call the function with our opassing the parameter:

functionThree();
functionThree(78);
functionThree(56,23);
functionThree(100,200,300);
Thanks Martin. Obviously I'm new to this.

--
Chris

Nov 7 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Pavils Jurjans | last post: by
3 posts views Thread by domeceo | last post: by
17 posts views Thread by Charles Sullivan | last post: by
reply views Thread by Paul Allan | last post: by
2 posts views Thread by Nab | last post: by
12 posts views Thread by dave_dp | last post: by
1 post views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.