Here are two programs. Both of them have a function fun() whose return type is reference to an integer. Only the difference between two functions is that in one function x is declared as static int while in other it is not. The output to the first question is 10 and the output to the second question is 30. How?
Program 1:
#include<iostream>
using namespace std;
int &fun()
{
int x = 10;
return x;
}
int main()
{
fun() = 30;
cout << fun();
return 0;
}
Program 2:
#include<iostream>
using namespace std;
int &fun()
{
static int x = 10;
return x;
}
int main()
{
fun() = 30;
cout << fun();
return 0;
}