C++ Function with multiple variable declarations?

Started by
12 comments, last by samurai_gator 14 years, 6 months ago
Yes, your explanation is clear. I understand that the parameters in Foo MUST match the data types ( int, double, char ) inside function Foo when it is called. The variables in a function must also have the same order in the function that is called.

void Foo(int x, double y, char z = 'z'){   std::cout << "x: " << x << ", y: " << y << ", z: " << z << std::endl;}Foo(1, 2.0, 'a'); // compiles, prints "x: 1, y: 2.0, z: a"Foo(1, 'a', 2.0); // compile error!  There is no function 'Foo()' that takes an int, a char, and a float in that order.Foo(1, 2.0); // compiles, prints "x: 1, y: 2.0, z: z"


In the code above: x and y aren't assigned a default value. If you assign a value to a variable with a default or without a default, it can be used. If you have a variable with no default, it can't be used
In Foo(1, 2.0, 'a'), a overwrites z.

Right?

void AddemUP ( int INT1, int INT2 = 1, int INT3 = 7 ){  //return INT1 + INT2 + INT3;  cout << " INT1 = " << INT1 << " INT2 = " << INT2 << " INT3 = " << INT3 << endl;}AddemUP( 3, 2);


The above code will work because 3 is now the value for INT1 even though it had no default. 2 overwrites 1 and 7 comes naturally. This program won't compile if you have a variable with no default value and you don't specify a value or it to use.
Is that correct?
Advertisement
Let us reduce your problem:

In C++, an argument takes either the value of ...

* the parameter you pass to it when calling the function
* the default value, if given

If there is no default value for an argument X, and you don't provide a parameter for that X in the function call, your code is invalid.

void foo (int x) {}int main () {    foo(); // invalid}


void foo (int x=2) {}int main () {    foo(); // valid, as if you passed 2 to foo()}


void foo (int x=2) {}int main () {    foo(2); // valid, actually compiles into the same as above}


void foo (int x, int y=2) {}int main () {    foo (1);   // valid, inside foo() x==1, y==2    foo (5);   // valid, inside foo() x==5, y==2    foo (7,8); // valid, inside foo() x==7, y==8    foo ();    // invalid, no default value for x}


Then, in C++ you must specify default values from right to left:
void foo (int x, int y=2) {}   // validvoid bar (int x=1, int y=2) {} // validvoid frob (int x=2, int y) {}  // not valid



Further: For advanced C++ programmers only!

You may specify multiple declarations of a function (but always only one definition), and each declaration may define more default values, from right to left, and only for arguments that don't have a default value yet.
void foo (int x, int y);  // okayvoid foo (int x, int y=2);  // okayvoid foo (int x=1, int y);  // okay, actually, we have foo(int=1,int=2) now!


Imagine it like this:
Applying the first declaration:columns: 0,1    foo ( , )  + foo ( ,=)  -----------  = foo ( ,=)Applying the second declaration:    foo ( ,=)  + foo (=, )  -----------  = foo (=,=)


To have your program well formed, ...

0)

every column above the line must at maximum contain a single "=". If there are two or more, you get "argument xyz already has a default value".

E.g.:
void frob (int x, int y=2); // okayvoid frob (int x, int y=3); // fail, y already has a default value


1)

for every "=" in some column x, one of the above declarations need a "=" in column x+1, otherwise you get "no standard arguments for the arguments on the right side of xyz have been defined".

E.g.:
void bar (int x, int y);   // okayvoid bar (int x=1, int y); // fail, because y does not have a default vale yet                           // more general: not every argument on the right side of                           //               x has a default value
Quote:Original post by samurai_gator
Is that correct?

That is correct.

However, keep in mind that default parameters should be used very sparingly. There is usually a better way to do whatever it is you're doing than by using default parameters.
Thanks for the explaining phresnel. In C++, an argument takes the value of in 2 ways.
1. parameters supplied in the called function
2. A default value if given.

As long as one or both of the conditions are true, then there shouldn't be a problem. I understood that variables work from right to left. But is it necessary to understand the following:

Applying the first declaration:

columns: 0,1

foo ( , )

+ foo ( ,=)

-----------

= foo ( ,=)

Applying the second declaration:

foo ( ,=)

+ foo (=, )

-----------

= foo (=,=

Thanks for the input Mantear, I was just playing around with the functions.

This topic is closed to new replies.

Advertisement