Jump to content

  • Log In with Google      Sign In   
  • 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.

  • You cannot reply to this topic
13 replies to this topic

#1 samurai_gator   Members   -  Reputation: 100

Like
0Likes
Like

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.

Sponsor:

#2 Black Knight   GDNet+   -  Reputation: 754

Like
0Likes
Like

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 :

AddemUp(3);

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

#3 KulSeran   Members   -  Reputation: 2535

Like
0Likes
Like

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)

#4 Zahlman   Moderators   -  Reputation: 1682

Like
0Likes
Like

Posted 26 September 2009 - 09:28 PM

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?

#5 samurai_gator   Members   -  Reputation: 100

Like
0Likes
Like

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?



#6 SiCrane   Moderators   -  Reputation: 9593

Like
0Likes
Like

Posted 27 September 2009 - 11:48 AM

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?


#7 samurai_gator   Members   -  Reputation: 100

Like
0Likes
Like

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.


#8 Zahlman   Moderators   -  Reputation: 1682

Like
0Likes
Like

Posted 27 September 2009 - 03:25 PM

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.

#9 samurai_gator   Members   -  Reputation: 100

Like
0Likes
Like

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



#10 Mantear   Members   -  Reputation: 251

Like
0Likes
Like

Posted 28 September 2009 - 06:40 AM

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 order
Foo(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?

#11 samurai_gator   Members   -  Reputation: 100

Like
0Likes
Like

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?

#12 phresnel   Members   -  Reputation: 949

Like
0Likes
Like

Posted 28 September 2009 - 08:40 PM

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) {}   // valid
void bar (int x=1, int y=2) {} // valid
void 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);  // okay
void foo (int x, int y=2); // okay
void 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); // okay
void 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);   // okay
void 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


#13 Mantear   Members   -  Reputation: 251

Like
0Likes
Like

Posted 29 September 2009 - 01:30 AM

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.


#14 samurai_gator   Members   -  Reputation: 100

Like
0Likes
Like

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.



PARTNERS