[Solved] how f() works in the given program in title to returning reference [closed]


At the beginning,f( )is declared as returning
a reference to a double, and the global variable val is initialized to 100. Next, the
following statement displays the original value of val:

cout << f() << '\n'; // display val's value

When f( ) is called, it returns a reference to val. Because f( ) is declared as returning
a reference, the line

return val; // return reference to val

automatically returns a reference to val. This reference is then used by the cout
statement to display val’s value.
In the line

newval = f(); // assign value of val to newval

the reference to val returned by f( ) is used to assign the value of val to newval.
The most interesting line in the program is shown here:

f() = 99.1; // change val's value

This statement causes the value of val to be changed to 99.1. Here is why: Since
f( ) returns a reference to val, this reference becomes the target of the assignment
statement. Thus, the value of 99.1 is assigned to val indirectly, through the reference
to it returned by f( ).

Finally, in this line

cout << f() << '\n'; // display val's new value

the new value of val is displayed when a reference to val is returned by the call to
f( ) inside the cout statement.

1

solved how f() works in the given program in title to returning reference [closed]