C control reaches end of non-void function
warning: control reaches end of non-void function [-Wreturn-type]
This warning means a function that promised to return a value, such as an int or double function, can reach its end without a return. A non-void function is a function that is not void, in other words one that returns a value. The line number in the warning points to the function's closing brace }. Follow each path through that function and look for one that reaches } without hitting a return.
Cause 1. You didn't write a return
If you start a function with int and only print inside it, the value it promised to return is missing. The code below never uses the returned value, so the output is correct, but you still get the warning.
Forgetting to return a value you calculated is more dangerous. The caller receives an indeterminate value, so you can't know what will come out.
Wrong code
#include <stdio.h> int print_sum(int a, int b) { printf("%d\n", a + b);} int main(void) { print_sum(3, 4); return 0;}main.c: In function ‘print_sum’:
main.c:5:1: warning: control reaches end of non-void function [-Wreturn-type]
5 | }
| ^
7Fixed code
Declare a function that has nothing to return as void. If the function should return a value, add return value;.
#include <stdio.h> void print_sum(int a, int b) { printf("%d\n", a + b);} int add(int a, int b) { return a + b;} int main(void) { print_sum(3, 4); printf("%d\n", add(3, 4)); return 0;}7 7
Cause 2. One branch of the if has no return
Even when every if and else if has a return, if there is still a case that matches none of the conditions, that path ends without a return. The function below has nothing to return when x is 0. It is only called with 5 and -3, so the output is right for now, but if you pass 0 you can't know what value will come out.
Wrong code
#include <stdio.h> int sign(int x) { if (x > 0) { return 1; } else if (x < 0) { return -1; }} int main(void) { printf("%d %d\n", sign(5), sign(-3)); return 0;}main.c: In function ‘sign’:
main.c:9:1: warning: control reaches end of non-void function [-Wreturn-type]
9 | }
| ^
1 -1Fixed code
Make the last branch a plain else with no condition, or put one return at the end of the function.
#include <stdio.h> int sign(int x) { if (x > 0) { return 1; } else if (x < 0) { return -1; } return 0;} int main(void) { printf("%d %d %d\n", sign(5), sign(-3), sign(0)); return 0;}1 -1 0
You can also get this warning on code that looks complete to a person, such as if (x >= 0) ... else if (x < 0) .... Change the last else if (condition) to else and it goes away. When you return only inside a switch, you also need a default: or a return at the end of the function.
Cause 3. The only return is inside a loop
This is common in functions that return as soon as they find something. When nothing is found by the end, the loop simply finishes, and there is no return after it.
Wrong code
#include <stdio.h> int find(int a[], int n, int key) { for (int i = 0; i < n; i++) { if (a[i] == key) { return i; } }} int main(void) { int a[] = {4, 8, 15}; printf("%d\n", find(a, 3, 8)); return 0;}main.c: In function ‘find’:
main.c:9:1: warning: control reaches end of non-void function [-Wreturn-type]
9 | }
| ^
1Fixed code
After the loop, return a value that means "not found". For a function that returns an index, -1 is the usual choice because it can't be an index.
#include <stdio.h> int find(int a[], int n, int key) { for (int i = 0; i < n; i++) { if (a[i] == key) { return i; } } return -1;} int main(void) { int a[] = {4, 8, 15}; printf("%d %d\n", find(a, 3, 8), find(a, 3, 7)); return 0;}1 -1
What to check when you see this error
- Find the function named on the
In functionline of the warning. The line number is that function's closing}. - If the function has nothing to return, change
inttovoid. - Follow each branch of the
if,else ifandswitch, and look for a path that gets out without areturn. - If the only
returnis inside a loop, add areturnafter the loop ends. - It is only a warning, so the program runs, but if the caller uses a value that was never returned, the result is not defined. You get a bug that is right some of the time and wrong the rest.
- The message differs between compilers. clang says
non-void function does not return a value, and Visual Studio sayswarning C4715(not all control paths return a value).