Passing parameters to a function

C

csmith8933

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;
}
 
M

Martin.YorkAmazon

To call the function with our opassing the parameter:

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

csmith8933

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top