Start learning free

LESSON 32 OF 348 · FUNCTIONS I

Calling a Function Twice in Kotlin: 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?

fun greetCoder() {
	println("Hello!")
	println("Let's code!")
}

fun mainFunction() {
	greetCoder()
	greetCoder()
}

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

KOTLIN · FUNCTIONS I

Quiz Calling A Function in Kotlin, explained

Problem overview

This quiz shows two Kotlin functions. greetCoder prints two lines, Hello! and Let's code!. mainFunction calls greetCoder() two times in a row. You are asked what the code produces when run, and you pick one of four outputs:

  • A: Hello! and Let's code!, then both lines again (four lines);
  • B: Hello! twice, then Let's code! twice;
  • C: Hello! and Let's code! once (two lines);
  • D: the pair of lines three times (six lines).

The answer goes into the variable answer as a string holding the option letter.

Approach: trace the calls

Don't guess from the look of the code. Walk through it the way Kotlin does, and keep a running list of what has been printed.

A function definition stores the body without running it. A call jumps into the body, runs every statement from top to bottom, and then returns to the line after the call. Each call is independent, so the second call starts from the first line of the body again. The body doesn't remember that it already ran.

Step by step

  1. Set the definitions aside. The definition of greetCoder prints nothing by itself. The same is true of the header of mainFunction.
  2. Run the first call. It executes the two statements in order. The output so far is Hello! followed by Let's code!.
  3. Return to mainFunction. The first call is finished, and execution continues with the next statement, the second call.
  4. Run the second call. The body runs again from the top, so the output grows by the same two lines.
  5. Compare the full output with the options. It is the four lines Hello!, Let's code!, Hello!, Let's code!. That is option A.
  6. Store "A" in answer.

The solution in Kotlin

val answer: String = "A"

/* Explanation:
In the first 4 lines of code we only define the function, so no "println()" statement will actually get executed.

Then, inside the main function, we call the function for the first time, so all the statements inside the function body will get executed from top to bottom.
So, the first function call will print "Hello!", then it will print "Let's code!" on two separate lines, and then this function call ends.
The output so far is:
Hello!
Let's code!

Then we call the function for the second time, and again all the statements inside the function body will get executed from top to bottom.
So the second function call will again print "Hello!", then it will print "Let's code!" on two separate lines, and then the function call ends.
So the output is:
Hello!
Let's code!
Hello!
Let's code!
*/

The Kotlin details in this short solution:

  • val fits here. The answer is chosen once and never changes, so a read-only val is the right kind of variable. Assigning to it a second time wouldn't compile.
  • The type follows the name. answer: String says that answer holds text. The letter is in double quotes because it is a string literal.
  • The explanation is a block comment. Text between /* and */ is ignored by the compiler, even if it spans many lines and contains quotes or code-like text.

Seeing it run

Running the quiz's code as a standalone program, with a main that calls mainFunction, prints exactly the four lines of option A:

Hello!
Let's code!
Hello!
Let's code!

Use this only after you have traced the code yourself. The point of the quiz is to build the habit of predicting the output.

Edge cases and common mistakes

  • Picking B. This treats each call as running one statement and then handing control to the other call. A call always finishes its whole body before the next statement of the caller starts, so the lines can't interleave.
  • Picking C. This assumes a function runs only once, however many times it is called. Every call runs the body again.
  • Picking D. This counts the definition as one more run. A definition never prints anything, and the two calls produce two copies of the output.
  • Miscounting lines. Each call prints two lines, not one. Two calls therefore print four.

As a general rule, the number of times a body runs equals the number of calls that actually execute, and the output of each run is the same when the body doesn't depend on any input.

Keep going with KotlinAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
Start learning free