here is the code and error is"control may reach end of non-void function [-Werror,-Wreturn-type]"

#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>
int collatz( int n, int c);
int main (void)
    printf("please enter a positive int\n");
    int n=GetInt();
    int count=0;
    collatz (n,count);
    int collatz (int n,int c)

    while (n>0)
        //base case is when n==1

        if (n==1)
            //print number of repition to make it 1
            printf("%i\n", c);
            return 0;
        // if even
       else if ((n%2)==0)
            return 1 + collatz(n/2,c++);
        //if odd
        return 1 + collatz(3*n+1,c++);


The problem is that the compiler believes that the collatz() function can finish processing without hitting a return. While it may logically not be possible, compilers are dumb. They can't analyze code well enough to see that all logical possibilities may be covered. Instead, they verify that all paths through the code will end at a return.

Because your code contains all of the returns in a while loop, the compiler sees that if the while loop doesn't start ( n is <= 0 ), no return statement will be executed, so it yells. In this case, there's nothing preventing entry of a negative number for n in main, so the called function can definitely fail because of a path with no return statement. Even if you did force n to be > 0, the compiler wouldn't be smart enough to recognize it.

The solution? Put a catchall return statement at the end of the function code so that if the code gets to the end, it will execute. Also, be careful about the return value. For example, if the function is searching for something to be true, the catchall should return the opposite - return false. Returning a number? Find a number to return that should never be returned and use it to indicate an error (or find some other mechanism to generate an error).

As for no return in main(), it's not a problem. The best practice is to always end processing with a return statement, but it isn't necessary in main and won't cause a compile error. You can prove this by writing a small test program. Remember the "Hello World" program?

For more detailed explanations, see the following two posts:

How to solve "control may reach end of non void function" error

How to Fix "control may reach end of non-void function" and "No such file or directory" Errors?

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

  • I see I was wrong. I'm glad you cleared it up at least! – kluvin Oct 1 '16 at 21:51

Your int main function is not returning a value, which is required if you specify a return type. Either change it to void main() or well, add a return type.

Hope that helps!

  • As @MARS said, a downvote without explanation is useless. If you don't tell us what you need or what we did wrong, we cannot improve our answers nor can we do better next time. – kluvin Oct 1 '16 at 19:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .