C++ return value without return statement

前端 未结 6 658
眼角桃花
眼角桃花 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:16

    Your function sqr() has no return statement. The function has undefined behavior concerning return value. Your first output shows this return value.

    The compiler should show a diagnostic though.

    try this:

    int sqr(int x)
    {
      return x*x;
    }
    

提交回复
热议问题