Start learning free

LESSON 47 OF 347 · FUNCTIONS I

Return in C#: Code After a return Never Runs

Quiz Read the full explanation

Quiz: What would this code produce if we were to copy-paste it in a code editor and run it?

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

void mainFunction() {
    string question = greetAndReturn();
    Console.WriteLine(question);
}

Important Note:

Do not use an actual code editor to get the answer! It would defy the whole purpose of the quiz!

Pick your answer

Next lesson

C# · FUNCTIONS I

Quiz Return 7 in C#, explained

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:

  1. the value after return becomes the result of the method call;
  2. the method ends immediately. Any statements after the return are 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:

  1. mainFunction starts and runs string question = greetAndReturn();. That calls greetAndReturn.
  2. Inside greetAndReturn, the first statement is return "How are you doing?";. The method ends here and hands that string back.
  3. The line Console.WriteLine("Hey bro!"); comes after the return, so it never executes. Hey bro! is never printed.
  4. Back in mainFunction, question now holds How are you doing?.
  5. 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 return first, 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. greetAndReturn prints nothing by itself. The text on screen comes from mainFunction printing question.
  • Typing the answer without quotes. string answer = B; makes the compiler look for a variable called B.

Key takeaways

  • return ends the method at once.
  • Statements after a return in 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.
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