passing values to functions.

Hello, I am trying to get some confirmation about how to pass to functions. If you want to assign default values to certain parameters, and have others defined inside the body of int main(), then the parameters which will have default values go at the end of the list. Is that correct?

i.e. The following code is wrong, because we cannot leave a black in the function call on the third line of the main function. However, if we switch the prototype to void Passing (int a, int c, int b = 1); and the function definition to void Passing (int a , int c, int b) everything will be okay and we can call the function as Passing (a, c).

In brief, we cannot do this EVER:
Passing( a, , c)right?

#include <iostream>

using namespace std;

void Passing (int a , int b = 1 , int c);

int main()
{
int a = 0;
int c = 0;
Passing ( a, , c);
}

void Passing(int a, int b, int c)
{
cout << a << b << c << endl;
}
You are right, If some parameter has a default argument then all other parameters after it also shall have default arguments.

So this alone declaration

void Passing (int a , int b = 1 , int c);

is invalid. However these two declarations that follow each other are valid

void Passing (int a , int b , int c = 0);
void Passing (int a , int b = 1 , int c);

So you call the function as for example

Passing( 3 ); // equivalent to Passing( 0, 1, 0 );
Passing( 3, 4 ); // equivalent to Passing( 3, 4, 0 );

But you can not call it as

Passing( 0, , 0 );
Last edited on
So then you meant the last one would be:

void Passing (int a, int b = 1, int c =0)

right?

And I dont understand how Passing(3) is equivalent to Passing(0,1,0)
Are you referring to :
void Passing(int a, int b = 1, int c = 0)
then
Passing (3) is equivalent to Passing(3,1,0)
?

Thank you very much vlad from moscow for your quick reply!
Last edited on
@melvyndrag

And I dont understand how Passing(3) is equivalent to Passing(0,1,0)


It is a typo. Of course there should be Passing( 3, 1, 0 ); :)
Thanks again
спасибо
Topic archived. No new replies allowed.