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
- Find the line that never runs. The print statement sits after the
return. - Move it above the
return. Statements run in order, so the print happens first, and thereturnthen ends the method. - Leave
MainFunctionalone. 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:
GreetAndReturn()is called. Its first statement printsHey bro!.- The
returnstatement ends the method and passes back"How are you doing?". - That string is stored in
question. Console.WriteLine(question)printsHow 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 thereturn, 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
returnis only unreachable when thereturnalways runs. Inside anifblock, areturnskips 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
MainFunctioninstead. That may produce the same text, but the lesson says the greeting comes from insideGreetAndReturn. - Changing the return to a print. Then
questionwould have no value, and the method'sstringtype would no longer match. - Ignoring the compiler warning. "Unreachable code detected" points straight at this kind of bug.