To fix this problem all i need to do is return something in the else part of the if statement, but it seems so pointless and messy because the program is designed to recurse until a value is returned. Is there a way around this without returning a value?

 char get_pf()
        printf("Calculating the present or future Value: P/F:\n");
        char p_f = get_char();

        if(p_f == 'P' || p_f == 'p' || p_f == 'F' || p_f == 'f')
            return p_f;
            printf("Invalid Selection:\n");

You need to do it because compilers are dumb. They can't see complex logic that will always result in a return path.

I'm guessing you're getting a "warning" that "control may reach end of non-void function"? It's an error because a compile flag says treat all warnings as errors. It's also bad practice not to have the return because nobody can predict what happens in unpredictable or error scenarios.

Having said that, the recursion is flawed. Say that the code passes into the else clause. It will call itself again, we'll call that the child. Say that the child returns 'P' to the parent. What happens next?

The parent doesn't do anything at all with the result, so nothing gets returned to the original call - the code is broken.

THIS is why there needs to be a return. ;-)

Review these two sites for more info, particularly the part on recursive returns (the first link):




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

  • Thank you, great answer. So if i were to return get_pf() would it then be fine? – Michael Dec 28 '17 at 13:10

You must log in to answer this question.

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