#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;
}
C++ Function with multiple variable declarations?
I have the following code:
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.
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 :
AddemUp(3);
And there is no method called AddemUp which takes only 1 argument so its a compile error.
int AddemUP ( int INT1, int INT2 = 1, int INT3 = 0 )
Then there is no default value to plug when you call it with :
AddemUp(3);
And there is no method called AddemUp which takes only 1 argument so its a compile error.
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)
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)
Quote:Original post by samurai_gator
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 )?
What would you expect the result to be? And why?
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?
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?
Quote:Original post by samurai_gator
I 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?
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.
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.
Quote:Original post by samurai_gator
I'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.
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
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;}
Quote:Original post by samurai_gator
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
*** 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?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement