Start learning free

LESSON 41 OF 347 · FUNCTIONS I

Forgotten Return in C#: Why the Code Does Not Compile

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?

int getDouble(int n) {
	n * 2
}

void mainFunction() {
	int result = getDouble(10);
	Console.WriteLine(result);
}

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 Forgotten Return in C#, explained

The quiz

The question shows a method that is supposed to double a number, and a caller that prints the result:

int getDouble(int n) {
	n * 2
}

void mainFunction() {
	int result = getDouble(10);
	Console.WriteLine(result);
}

You choose between three outcomes: it prints 20, it produces errors, or it prints 10. The answer is B: it produces errors. The program never runs at all, so neither 20 nor 10 is ever printed.

Why it fails

The header int getDouble(int n) makes a promise: every call to this method gives back an int. The body then breaks the promise in two ways.

  1. n * 2 is not a statement. C# lets only a few kinds of expressions stand alone as a statement: assignments, calls, increments and decrements, await and object creation. A bare multiplication is none of those, because computing a product and doing nothing with it has no effect. The compiler rejects it as error CS0201.
  2. There is no return. Even if the line were allowed, the value would be computed and thrown away. The method would reach its closing brace without handing an int back, and the compiler reports error CS0161: not all code paths return a value.

There is also a smaller problem on the same line: the statement has no semicolon. C# ends each statement with ;, and the compiler reports a missing semicolon before it gets as far as the two errors above. Fixing one error often uncovers the next.

How to reason through it

You do not need to run anything to answer this kind of quiz. Follow these checks in order:

  1. Read the return type. It is int, not void, so the method must return an integer.
  2. Look for return. There is none. A non-void method with no return on some path cannot compile.
  3. Conclude errors. If the program does not compile, nothing runs, so options that describe printed output are wrong.

Option C, printing 10, is a tempting wrong answer for people who think the method hands back its input unchanged. C# has no such rule. In some other languages a missing return quietly produces a default or empty value. In C# the compiler stops you before the program starts.

The answer in the editor

The lesson asks you to declare a string variable named answer that holds the letter you chose:

string answer = "B";

/* Explanation:

GetDouble promises to return an int, but its body only computes n * 2 and never returns it.

C# won't compile it: not all code paths return a value (CS0161), and n * 2 on its own isn't a valid statement (CS0201).

The fix is to return the result: return n * 2;

*/

The letter goes in double quotes because it is a string value, and the comment after it is only an explanation that the compiler ignores.

The fix

Put return in front of the expression, and the method keeps its promise:

public int GetDouble(int n) {
    return n * 2;
}

With this change, GetDouble(10) evaluates to 20, which the caller stores in result and prints. The semicolon after the expression ends the return statement, and the method now compiles.

Common mistakes

  • Assuming the last expression is returned. Some languages return the last value of a block automatically. C# method bodies do not: you must write return.
  • Changing int to void and keeping the call. The caller says int result = getDouble(10), and a void method has no value to store. That is a different compile error.
  • Only fixing the semicolon. Adding ; after n * 2 removes the first error, but CS0201 and CS0161 remain.
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