Start learning free

LESSON 31 OF 414 · FUNCTIONS I

Quiz: Defining a Function Without Calling It in Rust

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?

fn say_my_name() {
	println!("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

RUST · FUNCTIONS I

Quiz Defining Without Calling in Rust, explained

Problem overview

This quiz shows a function definition and asks what the code produces when it runs. The options are: A, it prints Heisenberg!; B, it produces errors; C, it prints nothing; D, it prints say_my_name. The answer is recorded as a comment holding the chosen letter, and the same letter is printed with println!.

fn say_my_name() {
    println!("Heisenberg!");
}

Approach: ask what actually runs

The question is only about what the code does, so go through it the way Rust does. A function definition has two jobs: it gives the code a name and it stores the body for later. It does not execute the body. The body runs only when something calls the function by name with parentheses, as in say_my_name();.

Look at the snippet for a call. There is none. The only thing in it is the definition, so the println! inside never runs.

Checking each option

  • A is wrong. It would be correct if the code contained say_my_name(); after the definition.
  • B is wrong. The definition is valid Rust: it has the keyword fn, a name, parentheses and a body in braces. Nothing in it is a mistake. A compiler may warn that the function is never used, and that is a warning, not an error.
  • C is right. Nothing calls the function, so nothing is printed.
  • D is wrong. The function's name appears only in its header, and writing a name in the source never prints it. Only a println! that actually runs prints, and the only one here prints Heisenberg! when its function is called.

Step by step

  1. Read the code for calls. A call is the function's name followed by parentheses. Here there are none.
  2. Conclude that the body never runs, so no text appears. That is option C.
  3. Write the answer as a comment on the first line: // answer = "C".
  4. Print the same letter with println!("C"); on the next line.

The solution in Rust

// answer = "C"
println!("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 say_my_name(), without calling it afterwards, so the println!() won't be executed and nothing will be printed to the console.

*/

A few Rust details about this code:

  • Comments. A line comment starts with // and runs to the end of the line. A block comment sits between /* and */ and can span several lines. The compiler ignores both.
  • The answer is a letter, in double quotes. println!("C"); prints the letter, and the format string comes first. Write println!("C"), not println!(C).
  • The quiz's own function is not called. Your submission prints the letter and never runs say_my_name. That mirrors the point of the quiz.

Edge cases and common mistakes

  • Confusing definition with execution. This is the mistake the quiz targets. A definition is a declaration. The body runs only on a call.
  • Picking B because nothing seems to happen. A program with no output is not an error. It simply did nothing visible.
  • Picking D. A function name written in code is an identifier. It is never printed unless you pass it to a printing statement as text.
  • What would change the answer. Adding say_my_name(); below the definition makes the program print Heisenberg! once, and adding it twice prints it twice.
  • Mismatched letters. The letter in the comment and the letter printed must be the same.
Keep going with RustAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
Start learning free