Control reaches end of non-void function C++
Tags: cpp, programming, oop, computerscience
Published: 2020-12-20 17:11:57

The error **‘control reaches end of non-void function C++’** occurs in compiling a code, due to misunderstanding of the flow of control in our algorithm. To overcome this error or the representation of the code is to flow the pseudocode. This warning is similar to the warning described in return with no value. If control reaches the end of a function and no return is encountered, GCC compiler assumes a return with no return value. However, for this, the function requires a return value. At the end of the function, add a return statement that returns a suitable return value, even if control never reaches there.
```
int main(void)
{
    my_strcpy(strB, strA);
    puts(strB);
    return 0;
}
```
This problem comes from a misunderstanding of the flow of control in the algorithm. Here's a representation of that control flow in pseudocode:
```
if (outer-condition-1)
    if (inner-condition-1)
        return 1
else if (outer-condition-2)
    if (inner-condition-2)
        return -1
else
    return 0
```
An else block is only ever related to one if...elseif...else construct at a time, so the last else block is attached only to the outer if conditions and not to the inner if conditions. If an outer condition evaluates to true and its inner condition evaluates to false, the else block will not be executed at all. This means the function will never encounter an explicit return statement, which is what the compiler doesn't like; every function must always return something, no matter what input it receives.

---
title: originally posted here 👇
canonical_url: https://kodlogs.com/blog/32/control-reaches-end-of-non-void-function-c
---

There are a few ways to fix this. The most direct solution is to add a return statement at the end of the function, outside of the conditional statements, but that's a bit messy, it can hide problems that would otherwise throw errors, making it a bit harder to find bugs.

Another way is to flatten your nested conditions, so that the pseudocode looks like this:
```
if (outer-condition-1 and inner-condition-1)
    return 1
else if (outer-condition-2 and inner-condition-2)
    return -1
else
    return 0
```
That might be appropriate if you're sure your program is working as intended with the current setup. But if you need something unique to happen when an outer condition is satisfied and an inner condition is not satisfied, you need to keep the nested structure but deal directly with every possible outcome.
```
if (outer-condition-1)
    if (inner-condition-1)
        return 1
    else
        return 2
else if (outer-condition-2)
    if (inner-condition-2)
        return -1
    else
        return -2
else
    return 0
```
In either case, once the program reaches the first if statement, **every path available to it will result in an explicit return statement, thus avoiding the control my reach end of non-void function.**

A related concept is "single entry, single exit" (SESE), which would demand that your function return a value only once at the end, rather than using the "early return" statements inside the conditional logic. This usually means defining an extra variable to hold the value that will be returned; for example:
```
declare int r
if (condition-1)
    r = 1
else if (condition-2)
    r = -1
else
    r = 0
return r
```
[Read more in original post](https://kodlogs.com/blog/32/control-reaches-end-of-non-void-function-c)