C++

Warning: control reaches end of non-void function

When we write the programs in C++. After executing programs, sometimes we get the error: ‘warning: control reaches the end of non-void function’, which means that certain functions that would have to return some values attain the termination. It might not give any value later. At this time, it is good to take a look at whether or not every control flow contains a return value. To recognize this fault, we first must recognize the working of the compiler.

There are two varieties of inaccuracies that happen whilst executing a program.

Compile-time errors: Fault which may be recognized with the aid of using the compiler without executing the code. Usually, those are syntax mistakes that are recognized without executing the code.

Run-time errors: Fault which happens while the program syntax is accurate; however, there’s a little trouble whilst the code is executing. Usually, invalid reminiscence access limitless loops fall below runtime mistakes.

This article will speak about common trouble confronted by novices and additionally skilled programmers. We often execute into the caution stating caution: ‘control reaches the end of non-void function’. Usually, we neglect this error due to the fact maximum of the time because the program nevertheless executes the same, even supposing this caution is coming.

Get the warning message:

Compiling this code executes, but an error occurs, which shows the warning ‘control reaches the end of non-void function’. Each function contains a return type that shows the kind of value the function maybe return. If the method isn’t returning any value, it’s far from a void return type. We obtain the above error, while the non-void function doesn’t return the value.

#include <bits/stdc++.h>
using namespace std;
int f(){
    if(false)
        return 0;
}        

int main() {
    f();
    return 0;

}

In this case, first, we integrate the header file and use the namespace standard. Further, we apply the if statement. Sometimes it can be the situation that we’ve numerous if-else if statements and all the statements contain the return value; however, if there’s no else assertion, we can nevertheless get the error due to the fact the compiler couldn’t comprehend that every situation is covered.

Here we get nothing in output, so it means the code is executed successfully, but the function doesn’t return any value.

Use If statement:

In this instance, if the condition is usually not true and consequently it’ll stop the function ‘f’ deprived of returning some value. And it is the reason behind receiving the attention message. Control within the attention message shows the flow of this code. Now let’s check whether it is a runtime error or compile-time error. The warning which we obtain right here is detected at assemble time, the use of simply the syntax of this code and the code isn’t always definitely executed to test if it’s far achieving the termination of any non-void method.

#include <bits/stdc++.h>
using namespace std;
int f(int a){
    if(a < 8)
        return 0;
}  
 
int main() {
    f(3);
    return 0;
}

We are always assured that if we execute the code, it is returning 0 value; however, nevertheless, we can obtain the same caution message due to the fact the compiler isn’t working smartly enough to recognize at compile-time that once this code is executed, it’s going to return the value each time. The compiler simply checks the syntax of the code, and it translates that for the function ‘f’ if the defined condition is not true, the flow of this code will attain line eight, and it couldn’t return the value, and hence we obtain the mistake.

After compiling the program, in output, we get nothing due to the error ‘control reaches the end of non-void function’.

Use the return statements:

When the conditional statements must not include any return statements, the control gets to the end of a non-void function in another way. Therefore, if the implementation within the non-void method is separated and if statements couldn’t execute each path, then at the end, we have to explicitly call ‘return’ for the function.

The subsequent instance validates the string handling function having conditional paths. Here we include three libraries for different purposes, and after that, we declare a function. On the other hand, a few instances are left now no longer assessed for the defined condition, which means that control flow may attain the cease of the function and produce errors.

Here we defined the function ‘reversestring’. We pass the string and condition as parameters to this function. The function body contains no return statement. However, it contains no arguments to call the function. So the compiler just shows the caution message. If we call the function, then we don’t get the error.

Conclusion:

In this article, we get information about the error: ‘control reaches the end of non-void function’ and see how we get this error. The non-void method contains the return type. Thus, the method wants to have a declaration that returns the items of the resultant type. If definite compiler standards are accepted, this error may get repressed completely, leading to run-time errors if the specified method calls in the code.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.