Translate

Friday, 2 October 2020

Solve “control reaches end of non-void function” Error

Hello everyone, In this article, we are going to discuss a common problem faced by beginners and also experienced programmers. Often when we run into the warning saying warning: control reaches end of non-void function. Generally, we ignore this error because most of the time as the program still runs the same even if this warning is coming.

Let’s first see a simple example then this kind of error can occur.

#include <bits/stdc++.h>

using namespace std;

int fun(){
    if(false)
        return 0;
}         // Error here

int main() {

    fun();
    return 0;

}

If we run this c++ code it works fine and terminates but we get an error at the 8th line saying control reaches end of non-void function.

To understand this error we first have to understand the working of the compiler a bit in detail. There are two important kinds of error which occurs while running a program.

Compile-time error: Error which can be identified by the compiler without running the program. Generally, these are syntax error which is identified without running the program.

Run-time error: Error which occurs when the program syntax is correct but there is some problem while the program is run. Generally invalid memory access, infinite loops fall under runtime error.

Every function has a return type which states the type of value the function will be returning. If the function is not going to return any value it is given a void return type, Any other return valued function is a non-void function.

We get the above error when the non-void function is not returning any value. 

In the above example the if condition is always false and therefore it will reach the end of the function without returning anything and that’s why we are getting the warning message. Control in the warning message means the flow of the program.

Now coming to the runtime and compile-time error part, the message which we are getting here is identified at compile time using just the syntax of the program and the program is not actually run to check if it is reaching the end of any non-void function.

Let’s take the below example:

#include <bits/stdc++.h>

using namespace std;

int fun(int x){
    if(x < 5)
        return 0;
}   // line 8

int main() {
    fun(4);
    return 0;
}

In this example, we are always sure that if we run the program it will return the values 0, but still we will get the same warning message because the compiler is not smart enough to understand at compile-time that when this program is run it will always return value. The compiler is just checking the syntax and it interprets that for the function fun if the if condition is false, the flow of the program will reach line 8 and it won’t be returning any value and therefore we get the error.

Sometimes it may be the case that we have several if-else if statements with return value which cover all the conditions but if there is no else statement we will still get the error because the compiler could not understand that all the conditions are covered.

How to Solve?

There is an easy solution to the problem, even if we understand that every condition is covered we should add a return statement at the end of the function so the compiler is sure that the non-void function will be returning some value. It may happen that flow is never reaching that part of the code but it is important to write for the compiler.

#include <bits/stdc++.h>

using namespace std;

int fun(int x){
    if(x < 5)
        return 0;
    return 1;    // line 8
}

int main() {
    fun(4);
    return 0;
}

The above code will not give any warning and the flow also won’t go to line 8 but still, it’s important to have the return statement there.

It’s a good practice to avoid this warning, many times code will run just fine even if warning is coming but for some cases, it may cause wrong behaviour of code, to avoid that its good practice to avoid this warning.

The post Solve “control reaches end of non-void function” Error appeared first on The Crazy Programmer.



No comments:

Post a Comment