-1

This function returns a value even when it shouldn't.

#include<iostream>
using namespace std;
int foo(int a,int b)
{
    if(a>b)
        return a;
    else if(a<b)
        return b;
}
int main()
{
    int x=7,y=7;
    cout<<foo(x,y);
    return 0;
}

The output is:

7

Also it produces proper output only on a GCC compiler (I used Dev C++). Turbo C produced garbage value. Can someone explain how this happens?

1 Answers1

3

The behaviour on not returning a value on all program control paths is undefined.

The compiler is allowed to do anything.

Didn't your compiler warn you of this? (GCC ought too, Turbo C possibly not on account of its age).

Bathsheba
  • 231,907
  • 34
  • 361
  • 483