C++ Programming - References - Discussion
Discussion Forum : References - General Questions (Q.No. 12)
12.
Which of the following statements is correct?
- We can return a global variable by reference.
- We cannot return a local variable by reference.
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;
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:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers