Start learning free

LESSON 31 OF 348 · FUNCTIONS I

Defining Without Calling in TypeScript: Function Quiz Explained

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?

function sayMyName() {
	console.log("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

TYPESCRIPT · FUNCTIONS I

Quiz Defining Without Calling in TypeScript, explained

Problem overview

The quiz shows a function and asks what happens when the code is run:

function sayMyName() {
    console.log("Heisenberg!");
}

The four options are: it prints Heisenberg!, it produces errors, it prints nothing, or it prints sayMyName. The answer goes into a variable named answer as a one-letter string.

Approach: ask what actually runs

The way to answer any "what does this print" question is to find the lines that execute, not the lines that merely exist. Here the whole program is one function declaration. Before picking an option, ask two things: is the code valid, and does anything call the function?

The code is valid: it has a header, parentheses and a body in curly braces. Nothing calls the function, though. There is no sayMyName() anywhere in the file.

Step by step

  1. Read the header. function sayMyName() declares a function. TypeScript now knows the name, but no code has run.
  2. Read the body. The console.log line sits between the braces, so it is part of the function and is stored with it, not executed.
  3. Look for a call. A call is the name followed by parentheses. There is none, so the body never runs.
  4. Match the options. Option A needs the print to run. Option B needs an error, and there isn't one. Option D would need the program to print the function's name, which would take something like console.log("sayMyName") or a printed function reference. Option C, nothing, is what remains.
  5. Assign the letter. The answer is "C", written as let answer: string = "C";.

The solution in TypeScript

        let answer: string = "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.log() won't be executed and nothing will be printed to the console.

*/

A few details about it:

  • The annotation checks nothing at run time. : string tells the compiler the variable holds text. It is removed when the code is compiled, so the value is simply the string "C".
  • The letter is text. It needs quotes. Writing C without them would refer to a variable named C.
  • The comment is ignored. A block comment starts with /* and ends with */, and may span several lines. It doesn't affect the program.

Why not the other options

  • Printing Heisenberg!. This would be true if the file also contained sayMyName();. The definition alone never triggers the body.
  • Errors. The function is valid TypeScript. An uncalled function is not an error.
  • Printing sayMyName. A function's name is never printed just because it was declared. Even writing the name without parentheses would not print it by itself.

Edge cases and common mistakes

  • Confusing definition with execution. Seeing a console.log in the code makes many learners assume it prints. What matters is whether that line is reached, and a function body is reached only through a call.
  • Thinking the file is "empty" and so an error. A program that does nothing visible is still a valid program.
  • Adding a call changes the answer. With sayMyName(); on a new line, the output would be Heisenberg!, which is option A. With two calls, it would print twice.
  • The call needs parentheses. A line that only says sayMyName; mentions the function without calling it, and still prints nothing.

Testing your understanding

  • The function plus one line sayMyName(); prints Heisenberg! once.
  • The function plus two such lines prints it twice.
  • The function alone prints nothing.
Keep going with TypeScriptAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
Start learning free