0

In the main function I created a local variable x . then passed it by reference to fun function . where the function declaration is int fun(int &x); but I am not understanding how the local variable is passing to fun function. if it would int &x=x where first x is the formal parameter and second x is the local variable . though the statement int &x=x shows error in a function .but why this is not showing any error or warning here. if anybody could not understand my question. plz give me details on how a reference variable would pass in a function. another thing I want to add is this. if a reference variable would been created in the memory

#include<iostream>
using namespace std;

int fun(int &x)
{
    return x;
}
int main()
{
    int x=10;
    cout << fun(x);
    return 0;
}
Oblivion
  • 7,176
  • 2
  • 14
  • 33
play store
  • 393
  • 1
  • 5

1 Answers1

1

Your function reads x by & and returns a copy of its value so nothing wrong with it

Oblivion
  • 7,176
  • 2
  • 14
  • 33