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
- Find the output line in the method. It builds the correct text,
"Hey, " + name, so the text is not the problem. - Replace the printing with a return. Swap
Console.WriteLine(...)forreturn ...;. The expression stays exactly the same. - Leave the printing to the caller.
MainFunctionalready has aConsole.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, Andytwo times, once from the method and once from the caller. - Making the method
void. Then the callstring greeting = GetGreeting("Andy");inMainFunctionfails, because avoidmethod gives no value. You are told not to edit that code. - Returning something unrelated.
return name;compiles and printsAndy, which is not the expected text. - Missing the space after the comma.
"Hey,"with no space printsHey,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.
