6 Replies - 1131 Views - Last Post: 16 August 2007 - 12:00 PM Rate Topic: -----

#1 nitin25   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 15-August 07

RETURNING REFERENCE

Post icon  Posted 15 August 2007 - 02:21 PM

int &add(int &ref)
{
 
ref+=1;
return ref;
}

int main()
{

int b=5;
int &mref=b;
add(mref);	//POINT OF DOUBT
return 0;
}





FUNCTION ADD IS RETURNING A REFERENCE BUT WHEN USED IN MAIN ITS RETURN TYPE IS NOT ASSIGNED TO ANYTHING. SO WHAT WILL HAPPEN TO REFERENCE RETURNED BY ADD IN MAIN.
Is This A Good Question/Topic? 0
  • +

Replies To: RETURNING REFERENCE

#2 1lacca   User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: RETURNING REFERENCE

Posted 15 August 2007 - 02:43 PM

It doesn't matter since the program ends there anyway :)

Just print it out on the next line, and you'll see.



spoiler: both mref, and b will be 6.
the reference returned by add is "lost", because you dont assign it to anything, however the things done in the function are not lost, because they are done on a reference.
Was This Post Helpful? 0
  • +
  • -

#3 Xing   User is offline

  • D.I.C Addict
  • member icon

Reputation: 19
  • View blog
  • Posts: 725
  • Joined: 22-July 06

Re: RETURNING REFERENCE

Posted 15 August 2007 - 10:04 PM

View Postnitin25, on 16 Aug, 2007 - 02:51 AM, said:

FUNCTION ADD IS RETURNING A REFERENCE BUT WHEN USED IN MAIN ITS RETURN TYPE IS NOT ASSIGNED TO ANYTHING. SO WHAT WILL HAPPEN TO REFERENCE RETURNED BY ADD IN MAIN.

You can ignore return values in C and C++.

PS: Do not use all caps. It is termed as shouting in this part of world.
Was This Post Helpful? 0
  • +
  • -

#4 vijaysnkar   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 55
  • Joined: 11-August 07

Re: RETURNING REFERENCE

Posted 15 August 2007 - 11:59 PM

Yes, The return values are ignored in this case because the program will end up.

View PostXing, on 15 Aug, 2007 - 10:04 PM, said:

View Postnitin25, on 16 Aug, 2007 - 02:51 AM, said:

FUNCTION ADD IS RETURNING A REFERENCE BUT WHEN USED IN MAIN ITS RETURN TYPE IS NOT ASSIGNED TO ANYTHING. SO WHAT WILL HAPPEN TO REFERENCE RETURNED BY ADD IN MAIN.

You can ignore return values in C and C++.

PS: Do not use all caps. It is termed as shouting in this part of world.

Was This Post Helpful? 0
  • +
  • -

#5 Xing   User is offline

  • D.I.C Addict
  • member icon

Reputation: 19
  • View blog
  • Posts: 725
  • Joined: 22-July 06

Re: RETURNING REFERENCE

Posted 16 August 2007 - 02:29 AM

View Postvijaysnkar, on 16 Aug, 2007 - 12:29 PM, said:

Yes, The return values are ignored in this case because the program will end up.

What do you mean program ends up?
Ignoring of return values has nothing to do with ending of program.
Was This Post Helpful? 0
  • +
  • -

#6 vijaysnkar   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 55
  • Joined: 11-August 07

Re: RETURNING REFERENCE

Posted 16 August 2007 - 03:35 AM

OK

View PostXing, on 16 Aug, 2007 - 02:29 AM, said:

View Postvijaysnkar, on 16 Aug, 2007 - 12:29 PM, said:

Yes, The return values are ignored in this case because the program will end up.

What do you mean program ends up?
Ignoring of return values has nothing to do with ending of program.

Was This Post Helpful? 0
  • +
  • -

#7 nitin25   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 15-August 07

Re: RETURNING REFERENCE

Posted 16 August 2007 - 12:00 PM

Thank's to all for explaining the above code.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1