The question
The quiz shows a method that returns a string and then has a print statement after the return. A second method calls it, stores the result in question and prints it. You have to pick which of four outputs the program produces, without running it. The answer is stored in a string variable named answer.
How return works
When a method reaches a return statement, two things happen at once:
- the value after
returnbecomes the result of the method call; - the method ends immediately. Any statements after the
returnare skipped.
Statements run from top to bottom, and return is an exit. Code placed after it in the same block can never be reached.
Tracing the quiz
Follow the program the way the computer does:
mainFunctionstarts and runsstring question = greetAndReturn();. That callsgreetAndReturn.- Inside
greetAndReturn, the first statement isreturn "How are you doing?";. The method ends here and hands that string back. - The line
Console.WriteLine("Hey bro!");comes after the return, so it never executes.Hey bro!is never printed. - Back in
mainFunction,questionnow holdsHow are you doing?. - The next line prints
question, so the console shows one line.
How are you doing?
That output is option B. Option A and C each contain Hey bro!, which never prints. Option D shows only Hey bro!, which is the one line that can't run, and leaves out the line that does.
Why the order of lines matters
Many beginners expect a method to run all of its lines and then return. Reverse the two lines in greetAndReturn and the answer changes: the method would print Hey bro! first, return the question, and the caller would print it, giving option A. The same two statements produce different output depending on which side of the return they sit on. That is exactly what the quiz tests.
What the compiler says
The quiz asks about running the code, but the C# compiler also notices the problem. A statement that can never run produces a warning, "unreachable code detected". It's a warning rather than an error, so the program still compiles and runs, and the dead line is still skipped at run time. Treat that warning as a hint that you've put a statement in the wrong place.
The solution in C#
The exercise asks you to declare answer with the letter you chose:
string answer = "B";
/* Explanation:
Remember, whenever C# hits a "return" statement inside a function, the value is being returned from the function
and the function call ends immediately after that. Even if there are more lines of code coming after the "return", they will not be executed.
In our case, when we call greetAndReturn(), the string "How are you doing?" is being returned and the function call ends.
Thus, the Console.WriteLine("Hey bro!") will never get executed.
However, variable greeting will be assigned the returned string "How are you doing?", which will be printed to the console.
*/
The letter goes in double quotes because it is a string. The comment block is only an explanation. Comments between /* and */ are ignored by the compiler.
Common mistakes
- Thinking both lines print. It's natural to read all the lines and imagine each one running. Look for
returnfirst, and stop reading the method there. - Thinking the order is reversed. Option C has the right idea of "the string comes first" but adds a line that never runs.
- Confusing returning with printing.
greetAndReturnprints nothing by itself. The text on screen comes frommainFunctionprintingquestion. - Typing the answer without quotes.
string answer = B;makes the compiler look for a variable calledB.
Key takeaways
returnends the method at once.- Statements after a
returnin the same block are unreachable and never run. - The returned value is what the caller receives, and it's printed only if the caller prints it.