0
votes

This is the code for finding the maximum of four numbers:

#include <iostream>
#include <cstdio>
using namespace std;

int max_of_four(int a,int b,int c,int d) {
    if(a>b&&a>c&&a>d)
    return a;
    else if(b>a&&b>c&&b>d)
    return b;
    else if(c>a&&c>b&&c>d)
    return c;
    else if(d>a&&d>c&&d>b)
    return d;
}

int main() {
  int a,b,c,d;
  scanf("%d %d %d %d",&a,&b,&c,&d);
  int ans;
  ans=max_of_four(a,b,c,d);
  printf("%d",ans);
  return 0;
}

But I am getting a warning like this:

control reaches end of non-void function -wreturn-type

What does this error mean?
Why does this error occur?

3
You haven't covered all the cases in your function. Just add ... else { return 0; }.NutCracker
Nothing is returned if none of the ifs is true. This is Undefined Behaviour.Yksisarvinen
Ask yourself what will happen if a, b, c, and d are all equal.john
@john ok but returning the 0 is not an error. I just added an example of what would fix the compile error. OP will then apply the logic to his case.NutCracker
By the way, there is a better way to calculate max. Something like max = a; if ( b) > max max = b; if ( c> max) max =c; if ( d > max ) max = d; return max;Gonen I

3 Answers

1
votes

Here is a simplified case that would cause this warning, hopefully it will make the warning's meaning clear:

// Not allowed - Will cause the warning
int answer(int question)
{
    if( question == 1 )
        return 100;
    else
        if ( question == 2 )
            return 200;  
}

What if the question is not 1 and not 2?

For example, what if the value of question is 3, or 10?

What will the function return?

It is undefined, and illeal. This is what the warning means.

When a function which returns a value ends, the value that it returns must be defined for all cases.

But your case is more similar to this, which still produces the warning:

// Not allowed - still causes the warning
int max_of_two(int a, int b)
{
    if( a>b )
        return a;
    else
        if ( b>=a ) // we covered all logical cases, but compiler does not see that
            return b;  
}

You are probably saying to yourself, "But I did cover all the cases! No other case is possible!" This is logically true, but the compiler does not know this. It does not build a logic table of all possible combinations of a>b, b<a etc.

So how can you correct this error? Make it more clear to the compiler that no other case is possible. In this case the correct code would be:

// OK - no warning
int max_of_two(int a, int b)
{
    if( a>b )
        return a;
    else  // The compiler now knows for sure that no other case is possible
        return b;  
}

The more interesting question is why does C++ let this off with a warning, and not produce a compiler error?

That question is discussed here: Why does flowing off the end of a non-void function without returning a value not produce a compiler error?

0
votes

If a function has a return value it has to return a value no matter what. So if you bind your return to a conditional statement, there has to be a case for any possible condition from a program perspective. You may know that your code will only feed the function with one of the given combinations, but your compiler doesn't know.

0
votes

control reaches end of non-void function -wreturn-type What does this error mean? Why does this error occur?

If your four conditions are false then what will the function return value?, so the compiler gives a warning.

So You can do something like,

#include <iostream>
#include <cstdio>

using namespace std;

int max_of_four(int a,int b,int c,int d)
{
    if(a>b&&a>c&&a>d)
    return a;
    else if(b>a&&b>c&&b>d)
    return b;
    else if(c>a&&c>b&&c>d)
    return c;
    else      // if above conditions are false then d is big
    return d;
}

int main()
{
  int a,b,c,d;
  scanf("%d %d %d %d",&a,&b,&c,&d);
  int ans;
  ans=max_of_four(a,b,c,d);
  printf("%d",ans);
  return 0;
}