472,138 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,138 software developers and data experts.

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 1828


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

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.