C++ Programming - References - Discussion

Discussion Forum : References - General Questions (Q.No. 12)
12.
Which of the following statements is correct?
  1. We can return a global variable by reference.
  2. We cannot return a local variable by reference.
Only 1 is correct.
Only 2 is correct.
Both 1 and 2 are correct.
Both 1 and 2 are incorrect.
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
23 comments Page 3 of 3.

Vinay said:   1 decade ago
We can return the global variable by reference and it is wrongly answered here. The correct option is C.

Rahul said:   1 decade ago
@nidhi : Agreed !!! both options are correct..

Nidhi said:   1 decade ago
Why is 1st wrong??


int& ret ()
{
int &d=g;
d=100;cout<<g;
return(d);
}
works fine where g is a global variable

nd in main i can do
ret()+=10;
cout<<"global value"<<g<<endl;

its 110;


Post your comments here:

Your comments will be displayed after verification.