What happens to unused function return values?

后端 未结 7 1625
太阳男子
太阳男子 2020-12-10 16:48

If I have a program:

#include 

using namespace std;

int TestIntReturn(int &x, int &y)
{
    x = 1000;
    y = 1000;
    return x+y;         


        
7条回答
  •  半阙折子戏
    2020-12-10 17:17

    The return value simply gets discarded. Depending on the exact scenario the optimizer might decide to optimize away the whole function call if there are no observable side effects (which is not the case in your example).

    So, upon return from TestIntReturn, the function will push the return value on the stack, the caller will then adjust the stack frame accordingly, but won't copy the returned value from the stack into any variable.

提交回复
热议问题