Start learning free

LESSON 40 OF 348 · FUNCTIONS I

Creating a Function with Parameters in Kotlin: printMessages

Practice Read the full explanation

We've written this code:

fun mainFunction() {
	println("My name is Andy")
	println("I code in Python")
	println("I have a dog")

	println("My name is Mike")
	println("I code in Java")
	println("I have a cat")

	println("My name is Sarah")
	println("I code in JavaScript")
	println("I have a parrot")
}

Write the same code in a better way by creating and calling a function with parameters printMessages()

STEP 1 OF 2
    Ctrl + Enter to check

    KOTLIN · FUNCTIONS I

    Exercise Create Function With Parameters in Kotlin, explained

    Problem overview

    The starting code prints three short introductions by writing nine println calls one after another:

    fun mainFunction() {
        println("My name is Andy")
        println("I code in Python")
        println("I have a dog")
        println("My name is Mike")
        println("I code in Java")
        println("I have a cat")
        println("My name is Sarah")
        println("I code in JavaScript")
        println("I have a parrot")
    }
    

    The task is to produce exactly the same output in a better way: write a function named printMessages that takes the varying parts as parameters, and call it three times from mainFunction.

    Approach: find what changes

    Compare the three groups of lines. Each group has the same shape:

    • My name is followed by a name;
    • I code in followed by a language;
    • I have a followed by an animal.

    The fixed text stays the same and three values change: the name, the language and the animal. Each changing value becomes a parameter, and the fixed text moves into the function body, where it is written once.

    Step by step

    1. Define the function above mainFunction. Write fun printMessages(name: String, language: String, animal: String). In Kotlin each parameter is a name, a colon and a type, and the parameters are separated by commas.
    2. Print the three sentences. In the body, use three println calls. In each, put the fixed text in double quotes and insert the matching parameter with a string template: $name, $language and $animal. Inside double quotes Kotlin replaces $name with the value of name, so no + is needed.
    3. Call the function three times. In mainFunction, call printMessages("Andy", "Python", "dog"), then printMessages("Mike", "Java", "cat"), then printMessages("Sarah", "JavaScript", "parrot"). Arguments are matched to parameters by position, so the first is the name, the second the language and the third the animal.

    The solution in Kotlin

    // Define and implement the function printMessages here:
    fun printMessages(name: String, language: String, animal: String) {
        println("My name is $name")
        println("I code in $language")
        println("I have a $animal")
    }
    
    
    fun mainFunction() {
        // Call printMessages 3 times here:
        printMessages("Andy", "Python", "dog")
        printMessages("Mike", "Java", "cat")
        printMessages("Sarah", "JavaScript", "parrot")
    }
    

    Running mainFunction() prints the same nine lines the original code printed, in the same order.

    String templates

    A $ followed by a variable name inside a string literal inserts that variable's value. The name ends at the first character that can't be part of a name, so in "I have a $animal" the template runs to the closing quote. When text must follow a name directly, wrap the name in braces: "${animal}s" gives dogs for "dog". The template is the same as joining pieces with +, but it is shorter and easier to read.

    Parameters and arguments

    The names in the header are parameters. The values in a call are arguments. Each call runs the body again with new values in name, language and animal. Those variables are read-only and exist only while that call runs.

    Edge cases and common mistakes

    • Wrong argument order. All three parameters are String, so the compiler can't notice a swapped order. printMessages("Python", "Andy", "dog") compiles and prints My name is Python.
    • Missing arguments. A call with two arguments for three parameters is a compile error.
    • Missing types. Kotlin requires a type on every parameter.
    • Extra or missing spaces. The text must match exactly, including the single space before each inserted value, such as the space after My name is.
    • Quoting the variable. Without the $, "My name is name" prints the word name.
    • Printing in the calls. The function does the printing. The calls pass only arguments.

    Why this is better

    The new version states the shape of an introduction once. To add a fourth person, you add one line with three arguments. To reword a sentence, you change one line in the function, and all introductions follow. The original version would need one edit per person.

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