Start learning free

LESSON 33 OF 348 · FUNCTIONS I

Execution Flow in Kotlin: Tracing Function Calls 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 welcomeUser() {
	println("Hey coder!")
}
fun mainFunction() {
	println("Welcome back!")
	welcomeUser()
}

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 Indentation 1 in Kotlin, explained

Problem overview

This quiz shows two functions. welcomeUser prints Hey coder!. mainFunction prints Welcome back! and then calls welcomeUser(). The question is what the code prints, with these options:

  • A: Hey coder! followed by Welcome back!;
  • B: only Welcome back!;
  • C: errors;
  • D: Welcome back! followed by Hey coder!.

You answer by assigning the option letter, as a string, to the variable answer.

Approach: follow the flow of execution

The execution flow is the order in which statements actually run. Two rules decide it:

  • Inside a function body, statements run from top to bottom, one after the other.
  • When a statement is a call to another function, the flow jumps into that function, runs its whole body and comes back to the line after the call.

Where a function is written in the file does not decide when it runs. In the quiz, welcomeUser is defined first, but defining it doesn't print anything. Its body runs when it is called, and this happens in the middle of mainFunction.

Step by step

  1. Find the starting point. The lesson treats mainFunction as the main program, so its statements are the ones that run.
  2. Run the first statement. It is println("Welcome back!"), so the output starts with Welcome back!.
  3. Run the second statement. It is the call welcomeUser(). The flow goes into the function, runs println("Hey coder!") and returns.
  4. Read off the output. It is Welcome back! and then Hey coder! on a new line each. That is option D.
  5. Check for errors. Both functions are well-formed, and mainFunction calls a function that exists with the right name and parentheses. The code compiles, so option C is out.

The solution in Kotlin

val answer: String = "D"

/* Explanation:

We first only define the welcomeUser() function, so the println("Hey coder!") won't actually be executed for now.

The mainFunction() is our "main program" so all the statements inside there will be executed in the order they are written.

First one is the "println("Welcome back!")", which will be executed and the output so far is:
Welcome back!

Then, the "welcomeUser()" function call will be executed.
So the code inside the function body (println("Hey coder!")) will get executed now and the output is:
Welcome back!
Hey coder!
*/

The Kotlin details in this short solution:

  • A read-only variable. val answer: String = "D" declares a variable that can't be reassigned. The option letter is a string, so it is written in double quotes.
  • Quotes inside a comment. The explanation contains lines such as println("Welcome back!"). Inside a /* ... */ comment, those quotes are ordinary text and don't have to be balanced or escaped.
  • Function order in the file. If welcomeUser were defined below mainFunction, the output would be the same. The output depends on the statements in mainFunction, not on where each function's text appears.

Seeing it run

Put the two functions in a file with a main that calls mainFunction(), and the program prints the following:

Welcome back!
Hey coder!

Try to predict the output of a program before running it. If your prediction and the result disagree, the difference tells you which rule of the flow you have misapplied.

Edge cases and common mistakes

  • Choosing A because of the file order. The tempting reasoning is that welcomeUser appears first, so its message comes first. Writing a function first only defines it. The flow follows the calls in mainFunction.
  • Choosing B because the function seems not to run. It is called, so it does run. A function that is defined and never called would be the case where nothing is printed.
  • Choosing C because there is no main. The quiz shows the code in the lesson's own setting, where mainFunction is the main program. Judge what the code does, not how it would be launched elsewhere.
  • Forgetting that a call returns. After welcomeUser() finishes, the flow continues in mainFunction. Here the call is the last statement, so the program then ends. If another statement followed it, that statement would print after Hey coder!.
Keep going with KotlinAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
Start learning free