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 2 of 3.

Will said:   8 years ago
It would be nice if s/o with access to modify the answer would correct it, because you can return a reference to a global variable and can not return a reference to a local variable.

Meet said:   9 years ago
Correct option is D:

It is perfectly valid & fine to return a reference to the global variable.

#include <iostream>
int s=9;
int& foo()
{
return s;
}
int main()
{
std::cout<<"value of s = "<<s<<'\n';
foo()=3;
std::cout<<"value of s = "<<s<<'\n';
}

It is syntactically valid to return a reference to a local variable but it doesn't make any sense to return a reference a local variable. Because the local variable is destroyed when execution of function finishes. So the reference to such variable is invalid & results in undefined behavior in C++.

Consider following program:

#include <iostream>
int& foo()
{
int s(9);
return s;
}
int main()
{
std::cout<<foo()<<'\n';
}

Compiler shows only warning when compiling above program.
[Warning] reference to local variable 's' returned [-Wreturn-local-addr]

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;

Varunrao.rao@gmail.com said:   9 years ago
#include<iostream.h>
int i=9;
int& myfunc()
{
return i;
}
int main() {
int g=myfunc();
cout<<g<<endl;
return 0;
}

Here, we can return global variable by reference.

Yogesh said:   9 years ago
We can return global variable by reference.

But can not return a local variable by reference.

The code written by @Pravee is working fine because after returning from the function, the value goes out of scope but that memory is not allocated by some other variable. So we are able to print proper value. But if that memory will be allocated by some other variable, then we will not able to get the correct value.

Raj said:   1 decade ago
We can not return a global variable by reference.

We can return a local variable by reference.

Wouter van ooijen said:   1 decade ago
You CAN do both in the sense that it syntactically allowed, but returning a local variable by reference yields a program that has undefined runtime behavior. So the point is what is meant by 'CAN'.

Abhirav Kumar said:   1 decade ago
@Pravee. Already proved that 2 is correct. Is there someone who knows why 1 is incorrect?

Rishi said:   1 decade ago
We can Return a global variable, but not a local as a local variable goes out of scope once a function call is over.

Pravee said:   1 decade ago
But if you see. ith following code works perfectly fine. It displays the value 9.

int& myfunc()
{
int i=9;
return i;
}
int main() {
int g=myfunc();
cout<<g<<endl;
return 0;
}


Post your comments here:

Your comments will be displayed after verification.