Start learning free

LESSON 48 OF 347 · FUNCTIONS I

Return in C#: Fixing Code After a return

Practice Read the full explanation

We've written a program and expected it to print:

Hey bro!
How are you doing?

but we get a different output. Fix our code so that it prints what we want.

STEP 1 OF 1
    Ctrl + Enter to check

    C# · FUNCTIONS I

    Buggy Code 6 in C#, explained

    What the code should do

    The program should print two lines:

    Hey bro!
    How are you doing?
    

    The first line is meant to come from inside GreetAndReturn. The second is the value GreetAndReturn returns, which MainFunction stores in question and prints. When you run the starting code, only How are you doing? shows up.

    public string GreetAndReturn() {
        return "How are you doing?";
        Console.WriteLine("Hey bro!");
    }
    

    What goes wrong

    A return statement ends the method on the spot and hands its value to the caller. Whatever follows it in the same block is never executed. In the buggy method, the return is the first statement, so the call to Console.WriteLine("Hey bro!") below it is dead code.

    C# even tells you. The compiler gives a warning, "unreachable code detected", for the print line. It's only a warning, not an error, so the program still builds and runs. That's why the bug shows up as missing output rather than a failure to compile.

    The fix, step by step

    1. Find the line that never runs. The print statement sits after the return.
    2. Move it above the return. Statements run in order, so the print happens first, and the return then ends the method.
    3. Leave MainFunction alone. It already prints the returned string.

    The solution in C#

    public string GreetAndReturn() {
        Console.WriteLine("Hey bro!");
        return "How are you doing?";
    }
    
    public void MainFunction() {
        string question = GreetAndReturn();
        Console.WriteLine(question);
    }
    

    Now trace one call of MainFunction:

    1. GreetAndReturn() is called. Its first statement prints Hey bro!.
    2. The return statement ends the method and passes back "How are you doing?".
    3. That string is stored in question.
    4. Console.WriteLine(question) prints How are you doing?.

    The output is exactly the two lines the task wants, in the right order. Notice that the first line is printed while the method is still running, and the second is printed afterwards by the caller. That is why they can't come out in the other order.

    Details worth knowing

    • The method is declared string, so every path through it must end by returning a string. If you removed the return, the compiler would report an error that not all code paths return a value.
    • Printing inside a method and returning from it are different things. Printing shows text. Returning gives a value to the caller. This method does both.
    • Code after a return is only unreachable when the return always runs. Inside an if block, a return skips the rest of the method only when the condition is true.

    A habit worth building

    When a method misbehaves, read it top to bottom and ask what the first return does. Everything after it is skipped. A good habit is to make return the last thing a block does, unless you deliberately want an early exit. Keeping the work first and the return last makes methods easy to read and avoids this kind of silent bug.

    Testing

    Run the fixed code and compare the console to the two expected lines. If Hey bro! is missing, the print is still after the return. If the lines come out in the other order, the print was moved into MainFunction after the call, rather than staying inside GreetAndReturn.

    Common mistakes

    • Deleting the print line. The output would then be missing Hey bro!.
    • Printing in MainFunction instead. That may produce the same text, but the lesson says the greeting comes from inside GreetAndReturn.
    • Changing the return to a print. Then question would have no value, and the method's string type would no longer match.
    • Ignoring the compiler warning. "Unreachable code detected" points straight at this kind of bug.
    Keep going with C#About 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free