Start learning free

LESSON 29 OF 347 · FUNCTIONS I

Defining a Method Without Calling It in C#: Quiz

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?

void sayMyName() {
	Console.WriteLine("Heisenberg!");
}

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 Defining Without Calling in C#, explained

The question

The quiz shows a method that prints a line and asks what happens if you run the code:

void sayMyName() {
    Console.WriteLine("Heisenberg!");
}

The options are: A, it prints Heisenberg!; B, it produces errors; C, it prints nothing; D, it prints sayMyName. The answer goes in a string variable named answer.

Reading the code

The code has exactly one thing in it: a method definition. The header is void sayMyName(), and the body is the single Console.WriteLine line between the braces. There is no line anywhere that writes sayMyName() as a call.

A definition tells the compiler that a method exists and what it does. It doesn't ask for that code to run. The body executes only when something calls the method by writing its name followed by parentheses.

Compare it with a version that does call the method. Here the last line is a call, and only then does the body run:

void sayMyName() {
    Console.WriteLine("Heisenberg!");
}

sayMyName();   // prints: Heisenberg!

The quiz code is that same program with the last line removed.

Going through the options

  • A, prints Heisenberg!. This would be right only if the method were called, and it isn't.
  • B, errors. The definition is well formed: it has a return type, a name, parentheses, and a body in braces. Nothing in it breaks a rule. Defining a method and never calling it is perfectly legal, and the compiler stays quiet.
  • C, prints nothing. This is the one. The method is stored but never used, so the print statement never runs.
  • D, prints sayMyName. Nothing here prints the method's name. A method's name is how you refer to it in code, and it isn't output unless you print it as a string yourself.

The answer in C#

Option C is correct, so the solution assigns that letter as a string:

string answer = "C";

/* Explanation:

Remember, the code inside a function will not be executed unless we call that function!
In our code, we only define the function sayMyName(), without calling it afterwards, so the Console.WriteLine() won't be executed and nothing will be printed to the console.

*/

A few C# details are worth noting. The letter is in double quotes because a string holds text, and double quotes are what mark a string literal. The statement ends with a semicolon. The variable's name has to be exactly answer for the checker to find it, and the letter has to be a capital, since "c" and "C" are different strings.

Why the quiz says not to run it

You could paste the code into an editor and see the result, but that skips the skill the quiz is after: reading code and predicting what it does. Predicting output without running anything is how you find bugs by reading, and it's how you'll trace programs through the rest of the course. Trace by asking, line by line, "does anything actually execute this?" For a definition alone, the answer is no.

What to remember

  • Defining a method and calling it are two separate things.
  • A method that is defined but never called produces no output, and no error.
  • To run sayMyName, you would add the call sayMyName();, and then the output would be Heisenberg!.

The lesson's examples use lowercase method names such as sayMyName to match the quiz text. C# convention is PascalCase, so you'd normally write SayMyName. Both compile, and the quiz's answer is the same either way.

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