How a function returns a reference in C++ ?

A function can return a reference. However, you have to make sure that the object which is being referred should not go out of scope after the termination of the function.

#include <iostream>

using namespace std;

char &replace(int x);

char s[80] = “More Crocess”;

int main()


replace(6) = ‘P';

cout << s;

return 0;


char &replace(int x)


return s[x];


Output: More Process


Leave a Reply