• Create Account

## C++ Function with multiple variable declarations?

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

13 replies to this topic

### #1samurai_gator  Members

Posted 26 September 2009 - 03:19 PM

I have the following code:

#include <iostream>
using namespace std;

int AddemUP ( int INT1, int INT2 = 1, int INT3 = 0 )
{
return INT1 + INT2 + INT3;

}

int main(
{

cout << AddemUP( 5 ) << endl;
cout << AddemUP(4,7 ) << endl;
cout << AddemUP(0, 4, 7 ) << endl;
return 0;
}


How come this code will work in it's current form, but not run when INT2 OR/AND INT3 are not initialized ( just like INT1 ABOVE )? I also know the values in AddemUP in main will replace the values in AddemUP at the top, with the values plugged in from left to right.

### #2Black Knight  GDNet+

Posted 26 September 2009 - 03:21 PM

If you removed the numbers from this :
int AddemUP ( int INT1, int INT2 = 1, int INT3 = 0 )

Then there is no default value to plug when you call it with :

And there is no method called AddemUp which takes only 1 argument so its a compile error.
[

### #3KulSeran  Members

Posted 26 September 2009 - 04:34 PM

int AddemUP ( int INT1, int INT2 = 1, int INT3 = 0 )

is just shorthand for writing out functions that look like this:
int AddemUP ( int INT1 ) { return AddemUP( INT1, 1, 0 ); }
int AddemUP ( int INT1, int INT2 ) { return AddemUP( INT1, INT2, 0 ); }
int AddemUP ( int INT1, int INT2, int INT3 ) { return INT1 + INT2 + INT3; }

Try commenting out any of those 3 lines, and the program in your main would fail to compile.

and in fact, writing
int AddemUP ( int INT1, int INT2 = 1, int INT3 = 0 ) { return INT1 + INT2 + INT3; }
int AddemUP ( int INT1, int INT2 ) { return AddemUP( INT1, INT2, 0 ); }

will confuse the compiler, because AddemUP(1,1) could match either function!

The compiler needs to be able to find a function that matches EXACTLY the number and type of parameters that you are trying to call with. It will try converting types to match a function, but the number of parameters must match. (unless the ... construct is used, like in printf)

### #4Zahlman  Members

Posted 26 September 2009 - 09:28 PM

Quote:
 Original post by samurai_gatorHow come this code will work in it's current form, but not run when INT2 OR/AND INT3 are not initialized ( just like INT1 ABOVE )?

What would you expect the result to be? And why?

### #5samurai_gator  Members

Posted 27 September 2009 - 05:17 AM

I would expect 5 in AddemUP( 5 ) to be added to 1 and 0 = 6.
Then 4 and 7 in AddemUP(4,7 ) to add up to 11. These numbers overwrite 1 in int INT2 = 1. This is when we have ( int INT1, int INT2 = 1, int INT3 = 0 ).

Then 0, 4, and 7 from AddemUP(0, 4, 7 ) to overwrite 1 and 0 and get 11.

Is it because you can only plug in new numbers, like 5 into INT1 because it has no default value? You can do the same when you overwrite older numbers with new ones. I don't think the code will work when you're doing the addition. You can't add an int with NO default such as AddemUP(4,7 ) = 4 + 7 + no default = compile error. Right?

### #6SiCrane  Moderators

Posted 27 September 2009 - 11:48 AM

Quote:
 Original post by samurai_gatorI would expect 5 in AddemUP( 5 ) to be added to 1 and 0 = 6.

Why do you think the compiler would know to do that if you don't tell it the default values?

### #7samurai_gator  Members

Posted 27 September 2009 - 12:34 PM

I'm not quite sure.
It worked for the code below.
Maybe it overwrites the first INT, regardless of whether it has no default value or just an integer. Then does the addition. But it doesn't function the same way when INT2 or INT3 or both have no default value.

Or probably because the arguments in function AddemUP have to match the arguments in AddemUP in main. Without a default value, the code probably fools the compiler into thinking there is nothing there. Just some thoughts.

### #8Zahlman  Members

Posted 27 September 2009 - 03:25 PM

Quote:
 Original post by samurai_gatorI'm not quite sure.It worked for the code below.

Because "the code below" does specify values for those parameters.

Quote:
 Maybe it overwrites the first INT, regardless of whether it has no default value or just an integer. Then does the addition. But it doesn't function the same way when INT2 or INT3 or both have no default value.

It's been explained to you multiple times.

The default values are used when nothing else is specified. If you don't specify a default value, then there is nothing to use when nothing else is specified - thus when nothing is specified, there is no way to determine a value.

### #9samurai_gator  Members

Posted 27 September 2009 - 04:35 PM

The example I got this from was:

Forward it to 3:56.
I adapted this code from VC++ to C++.
This code works, no compilation erros. A variable with no default variable can't be used. Is it skipped?

The output I get is:
1
11
8
#include <iostream>int AddemUP ( int INT1, int INT2 = 1, int INT3 = 0 ){  return INT1 + INT2 + INT3;}int main(){ using namespace std; cout << AddemUP( 0 ) << endl; cout << AddemUP( 4,  7 ) << endl; cout << AddemUP( 0,  4,  4 ) << endl; return 0;}

### #10Mantear  Members

Posted 28 September 2009 - 06:40 AM

Quote:
 Original post by samurai_gatorThe example I got this from was:Forward it to 3:56.I adapted this code from VC++ to C++.This code works, no compilation erros. A variable with no default variable can't be used. Is it skipped? The output I get is:1118*** Source Snippet Removed ***

What's your question? What don't you understand?

As others have already explained, this is what is going on. You have a function, AddemUp(), which takes 3 parameters. If the calling parameters you gave when you called AddemUp() do not match any of the function signatures, you get a compile time error. For example:
void Foo(int x, double y, char 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 double in that orderFoo(1, 2.0); // compile error!  There is no function 'Foo()' that takes just an int and a double

Now, what you can do is this:
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 second version of Foo(), I provided a default value for the third parameter. In this case, I can either call Foo() with 2 or 3 paramters (assuming all the paramters are of the proper types). If I provide the third parameter, the third parameter will be what I called Foo() with. If I don't provide a third paramter, the third paramter will be the default value.

Does that clear anything up for you?

### #11samurai_gator  Members

Posted 28 September 2009 - 10:18 AM

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?

### #12phresnel  Members

Posted 28 September 2009 - 08:40 PM

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

### #13Mantear  Members

Posted 29 September 2009 - 01:30 AM

Quote:
 Original post by samurai_gatorIs 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.

### #14samurai_gator  Members

Posted 29 September 2009 - 05:44 AM

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.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.