Skip to main content

Solve “control reaches end of non-void function” Error Neeraj Mishra The Crazy Programmer

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.



from The Crazy Programmer https://ift.tt/3iu3qqR

Comments

Popular posts from this blog

Difference between Web Designer and Web Developer Neeraj Mishra The Crazy Programmer

Have you ever wondered about the distinctions between web developers’ and web designers’ duties and obligations? You’re not alone! Many people have trouble distinguishing between these two. Although they collaborate to publish new websites on the internet, web developers and web designers play very different roles. To put these job possibilities into perspective, consider the construction of a house. To create a vision for the house, including the visual components, the space planning and layout, the materials, and the overall appearance and sense of the space, you need an architect. That said, to translate an idea into a building, you need construction professionals to take those architectural drawings and put them into practice. Image Source In a similar vein, web development and design work together to create websites. Let’s examine the major responsibilities and distinctions between web developers and web designers. Let’s get going, shall we? What Does a Web Designer Do?

A guide to data integration tools

CData Software is a leader in data access and connectivity solutions. It specializes in the development of data drivers and data access technologies for real-time access to online or on-premise applications, databases and web APIs. The company is focused on bringing data connectivity capabilities natively into tools organizations already use. It also features ETL/ELT solutions, enterprise connectors, and data visualization. Matillion ’s data transformation software empowers customers to extract data from a wide number of sources, load it into their chosen cloud data warehouse (CDW) and transform that data from its siloed source state, into analytics-ready insights – prepared for advanced analytics, machine learning, and artificial intelligence use cases. Only Matillion is purpose-built for Snowflake, Amazon Redshift, Google BigQuery, and Microsoft Azure, enabling businesses to achieve new levels of simplicity, speed, scale, and savings. Trusted by companies of all sizes to meet

2022: The year of hybrid work

Remote work was once considered a luxury to many, but in 2020, it became a necessity for a large portion of the workforce, as the scary and unknown COVID-19 virus sickened and even took the lives of so many people around the world.  Some workers were able to thrive in a remote setting, while others felt isolated and struggled to keep up a balance between their work and home lives. Last year saw the availability of life-saving vaccines, so companies were able to start having the conversation about what to do next. Should they keep everyone remote? Should they go back to working in the office full time? Or should they do something in between? Enter hybrid work, which offers a mix of the two. A Fall 2021 study conducted by Google revealed that over 75% of survey respondents expect hybrid work to become a standard practice within their organization within the next three years.  Thus, two years after the world abruptly shifted to widespread adoption of remote work, we are declaring 20