Reference lets a function to return a variable . But strange things happen :(:(

  • Thread starter karthikbalaguru
  • Start date
K

karthikbalaguru

Hi,

I wonder how the 'm' value is 10 in the program below . It should be
7.
Further, it turns later to 11. Strange. :( How is it possible ? Why ?
Need clarification. Any ideas ?

using namespace std;
#include <iostream>
double &biggest (double &r, double &s)
{
if (r > s) return r;
else
return s;
}

int main ()
{
double k = 3;
double m = 7;
cout << "k: " << k << endl; // Displays 3
cout << "m: " << m << endl; // Displays 7
cout << endl;
biggest (k, m) = 10;
cout << "k: " << k << endl; // Displays 3
cout << "m: " << m << endl; // Displays 10 /* How is this
possible . m was actually 7. how it prints 10 */
cout << endl;
biggest (k, m) ++;
cout << "k: " << k << endl; // Displays 3
cout << "m: " << m << endl; // Displays 11 /* How is it 11
here */
cout << endl;
return 0;
}

Any ideas ?

Thx in advans,
Karthik Balaguru
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

Hi,

I wonder how the 'm' value is 10 in the program below . It should be
7.
Further, it turns later to 11. Strange. :( How is it possible ? Why ?
Need clarification. Any ideas ?

using namespace std;
#include <iostream>
double &biggest (double &r, double &s)
{
if (r > s) return r;
else
return s;
}

int main ()
{
double k = 3;
double m = 7;
cout << "k: " << k << endl; // Displays 3
cout << "m: " << m << endl; // Displays 7
cout << endl;
biggest (k, m) = 10;

This sets the value of m to 10, what did you think the '=' meant?
cout << "k: " << k << endl; // Displays 3
cout << "m: " << m << endl; // Displays 10 /* How is this
possible . m was actually 7. how it prints 10 */
cout << endl;
biggest (k, m) ++;

Here you increase the value of m by one, what else did you expect the
'++' to do?
cout << "k: " << k << endl; // Displays 3
cout << "m: " << m << endl; // Displays 11 /* How is it 11
here */
cout << endl;
return 0;
}

Any ideas ?

Read up on references, you clearly have not understood what they are.
 
A

Alf P. Steinbach

* karthikbalaguru:
[posting HOMEWORK]

Please see the FAQ about posting homework.

I wonder how the 'm' value is 10 in the program below . It should be
7.
Further, it turns later to 11. Strange. :( How is it possible ? Why ?
Need clarification. Any ideas ?

using namespace std;
#include <iostream>
double &biggest (double &r, double &s)
{
if (r > s) return r;
else
return s;
}

int main ()
{
double k = 3;
double m = 7;
cout << "k: " << k << endl; // Displays 3
cout << "m: " << m << endl; // Displays 7
cout << endl;
biggest (k, m) = 10;
cout << "k: " << k << endl; // Displays 3
cout << "m: " << m << endl; // Displays 10 /* How is this
possible . m was actually 7. how it prints 10 */
cout << endl;
biggest (k, m) ++;
cout << "k: " << k << endl; // Displays 3
cout << "m: " << m << endl; // Displays 11 /* How is it 11
here */
cout << endl;
return 0;
}

Any ideas ?

Yes, see the FAQ item about posting HOMEWORK.
 
K

karthikbalaguru

karthikbalaguru said:
Hi,

I wonder how the 'm' value is 10 in the program below . It should be
7.
Further, it turns later to 11. Strange. :( How is it possible ? Why ?
Need clarification. Any ideas ?

using namespace std;
#include <iostream>
double &biggest (double &r, double &s)
{
if (r > s) return r;
else
return s;
}

int main ()
{
double k = 3;
double m = 7;
cout << "k: " << k << endl; // Displays 3
cout << "m: " << m << endl; // Displays 7
cout << endl;
biggest (k, m) = 10;
cout << "k: " << k << endl; // Displays 3
cout << "m: " << m << endl; // Displays 10 /* How is this
possible . m was actually 7. how it prints 10 */
cout << endl;
biggest (k, m) ++;
cout << "k: " << k << endl; // Displays 3
cout << "m: " << m << endl; // Displays 11 /* How is it 11
here */
cout << endl;
return 0;
}

Any ideas ?

Thx in advans,
Karthik Balaguru

Hey, I got it correct now.
biggest(k,m) returns m as &m which is an alias for m and so m gets
modified to 10 .
and the same thing happens with biggest(k,m)++ and so the m gets
incremented to 11.

Thx,
Karthik Balaguru
 
K

karthikbalaguru

Erik said:
This sets the value of m to 10, what did you think the '=' meant?


Here you increase the value of m by one, what else did you expect the
'++' to do?


Read up on references, you clearly have not understood what they are.

Apologies, there was some misunderstanding. :(:(
I did not look into it properly. :(:(

Karthik Balaguru
 
B

Barry

karthikbalaguru said:
Hi,

I wonder how the 'm' value is 10 in the program below . It should be
7.
Further, it turns later to 11. Strange. :( How is it possible ? Why ?
Need clarification. Any ideas ?

using namespace std;
#include <iostream>
double &biggest (double &r, double &s)
{
if (r > s) return r;
else
return s;
}

int main ()
{
double k = 3;
double m = 7;
cout << "k: " << k << endl; // Displays 3
cout << "m: " << m << endl; // Displays 7
cout << endl;
biggest (k, m) = 10;
cout << "k: " << k << endl; // Displays 3
cout << "m: " << m << endl; // Displays 10 /* How is this
possible . m was actually 7. how it prints 10 */

10 is right, what else you expect?
cout << endl;
biggest (k, m) ++;
cout << "k: " << k << endl; // Displays 3
cout << "m: " << m << endl; // Displays 11 /* How is it 11
here */

11 is right too.
cout << endl;
return 0;
}

Any ideas ?

biggest() always return a reference to m, as m is always bigger than k
changes made to the returned reference (m), will directly affect referee
m. which is a reference do.

see cout you use
cout << ".." << ".." is also the case,

which is changing your buffer in the output stream that cout references
 
T

terminator

Here you increase the value of m by one, what else did you expect the
'++' to do?

hey,incing a double???
I used to think that '++' operates on integral types.Were I
wrong ?????

thanks,
FM.
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

hey,incing a double???
I used to think that '++' operates on integral types.Were I
wrong ?????

It can be used on any arithmetic (integral or floating-point) type.
 
B

BobR

terminator said:
Here you increase the value of m by one, what else did you expect the
'++' to do?
/* """
hey,incing a double???
I used to think that '++' operates on integral types.Were I
wrong ?????

""" */

for( double v( 1.0 ); v <= 3; ++v ){
std::cout<<"v = "<< v <<std::endl;
} // for(v)
/* - output -
v = 1.000000
v = 2.000000
v = 3.000000
*/

I was surprised it worked when I tried it.
 
T

terminator

Actually it can be used to any arithmetic type or pointers to any
completely defined types.

pointers look much like integrals indeed.but double/float was news to
me.

thanks every 1,
FM
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top