Start learning free

LESSON 44 OF 674 · FUNCTIONS I

Fixing a C# Method That Prints Instead of Returning

Practice Read the full explanation

We've written a program and expected it to print Hey, Andy but when we run it we get some errors.

Fix our code so that it works and prints what we want.

STEP 1 OF 1
    Ctrl + Enter to check

    C# · FUNCTIONS I

    Buggy Code Print Instead Of Return in C#, explained

    What the code should do

    The program should print Hey, Andy, exactly once. The method GetGreeting is supposed to build the greeting for a name and give it back, and MainFunction calls it, keeps the result in greeting and prints it. The code you start with does not compile:

    public string GetGreeting(string name) {
        Console.WriteLine("Hey, " + name);
    }
    
    
    // Do not change the code below:
    public void MainFunction() {
        string greeting = GetGreeting("Andy");
        Console.WriteLine(greeting);
    }
    

    Only the first method may change. MainFunction is correct.

    What goes wrong

    The compiler stops with error CS0161, whose message ends with not all code paths return a value. It points at GetGreeting.

    The header says the method returns a string. The body does something else: it sends Hey, Andy to the console and ends. When execution reaches the closing brace, the method has not given back any string. C# checks this rule before the program runs, for every path through the method, and refuses to build if one path can end without a return.

    Two actions look alike here and are very different:

    • Printing displays text. The text goes out of the program and cannot be used again.
    • Returning passes a value back to the caller, which can store it, print it or use it in a calculation.

    The caller needs the second one. In string greeting = GetGreeting("Andy");, the call has to produce a string to put into greeting.

    Fixing it step by step

    1. Find the output line in the method. It builds the correct text, "Hey, " + name, so the text is not the problem.
    2. Replace the printing with a return. Swap Console.WriteLine(...) for return ...;. The expression stays exactly the same.
    3. Leave the printing to the caller. MainFunction already has a Console.WriteLine(greeting). If the method printed as well, the greeting would appear twice.

    The fixed code in C#

    public string GetGreeting(string name) {
        return "Hey, " + name;
    }
    
    
    // Do not change the code below:
    public void MainFunction() {
        string greeting = GetGreeting("Andy");
        Console.WriteLine(greeting);
    }
    

    The flow is now: MainFunction calls GetGreeting("Andy"); the method joins "Hey, " and "Andy" with + and returns "Hey, Andy"; the caller stores that in greeting and prints it. The output is one line, Hey, Andy.

    Strings in C# are immutable, so + does not change name. It creates a new string, and that new string is what the method returns.

    Why returning is the better design

    A method that returns its text can be used in many situations. The caller might print it, as here, or save it, or add more text to it, or compare it with an expected value in a test. A method that prints has already decided what happens to the text, and nobody else can use it. Return values keep a method focused: it computes, and the caller decides what to do next.

    Common mistakes

    • Keeping the print and adding a return. This builds the text twice and prints Hey, Andy two times, once from the method and once from the caller.
    • Making the method void. Then the call string greeting = GetGreeting("Andy"); in MainFunction fails, because a void method gives no value. You are told not to edit that code.
    • Returning something unrelated. return name; compiles and prints Andy, which is not the expected text.
    • Missing the space after the comma. "Hey," with no space prints Hey,Andy.

    Checking your fix

    Run the code in the editor above. It should compile and print Hey, Andy on a single line. If it prints the line twice, the method is still printing. If it fails to compile, check that every path in GetGreeting ends in a return statement.

    Keep going with C#650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free