C++ return value without return statement

前端 未结 6 684
眼角桃花
眼角桃花 2020-11-27 22:43

When I ran this program:

#include 

int sqr(int&);

int main()
{
    int a=5;
    std::cout<<\"Square of (5) is: \"<< sqr(a)          


        
6条回答
  •  孤独总比滥情好
    2020-11-27 23:28

    That's some garbage that will depend on a handful of factors. Likely that's the value stored in memory where the function would put the result if it had a return statement. That memory is left untoched and then read by the caller.

    Don't think of it too much - just add a return statement.

提交回复
热议问题