Start learning free

LESSON 39 OF 348 · FUNCTIONS I

Why Parameters and Arguments in Kotlin

Lesson Read the full explanation

Now that we've learned about function parameters and arguments, let's see why they are so important in our prgrammers' life:

Consider this: Three students named Andy, Mike and Sally made a mistake. And so their teacher asked each of them to write an apology on the whiteboard.

We can put the apologies in a program like this:

println("This is Andy")
println("Andy made a mistake")
println("Andy suffers the consequences")

println("This is Mike")
println("Mike made a mistake")
println("Mike suffers the consequences")

println("This is Sally")
println("Sally made a mistake")
println("Sally suffers the consequences")

That's decent. But what if we want to change the students' names?

We would have to go through the whole program and change each individual name with the new one.

And if we dealt with 10 students instead of 3 and if the apology text consisted of more than 3 lines, this would quickly get out of hand.


The story with functions:

This is where functions with parameters shine. We can write a function that takes in a student's name as argument and prints the specific aplogy for that student.

Then, we'll just call the function for each individual student:

fun printMessage(name: String) {
	println("This is " + name)
	println(name + " made a mistake")
	println(name + " suffers the consequences")
}

printMessage("Andy")
printMessage("Mike")
printMessage("Sally")

Changing the story:

Need to change the students' names? We only change the arguments in the function calls.

Need to change the aplogy text a little bit? We only change it inside the function instead of changing it in the whole program.

fun printMessage(name: String) {
	println("This is the apology of " + name)
	println(name + " made a mistake and so " + name + " suffers the consequences")
}

printMessage("Andrei")
printMessage("Paul")
printMessage("Mary")

Assignment
Follow the Coding Tutorial and let's write some functions.


Hint
Look at the examples above if you get stuck.


STEP 1 OF 5
    Ctrl + Enter to check

    KOTLIN · FUNCTIONS I

    Why Parameters And Arguments in Kotlin, explained

    The problem: repeating yourself

    Suppose three students, Andy, Mike and Sally, each have to print the same three lines about themselves. Without functions, you write nine println calls, and the only difference between the groups of three is the name:

    println("This is Andy")
    println("Andy made a mistake")
    println("Andy suffers the consequences")
    println("This is Mike")
    println("Mike made a mistake")
    println("Mike suffers the consequences")
    println("This is Sally")
    println("Sally made a mistake")
    println("Sally suffers the consequences")
    

    This works, but it breaks down quickly. Changing a name means editing three lines. Changing the wording means editing it once per student. With ten students and a longer text, you would be copying and fixing dozens of lines, and one missed edit would leave an inconsistent output.

    The idea: a function with parameters

    A parameter is a variable declared in a function's header. It stands for a value that the function doesn't know yet. An argument is the actual value the caller supplies for it in a call. In Kotlin, a parameter is written as a name, a colon and a type:

    fun printMessage(name: String) {
        println("This is " + name)
        println(name + " made a mistake")
        println(name + " suffers the consequences")
    }
    
    fun main() {
        printMessage("Andy")
        printMessage("Mike")
        printMessage("Sally")
    }
    

    The body is written once. Each call runs it with a different value in name. The three calls print the same nine lines as before.

    Now both kinds of change are easy. A new name is just a different argument in a call. A new wording is a change inside the function, and every call picks it up.

    Several parameters

    A function can take more than one parameter. List them in the header, separated by commas, each with its own type. When you call the function, the arguments are matched to the parameters by position: the first argument goes to the first parameter, the second to the second, and so on. The call must supply as many arguments as the function declares.

    The lesson's exercise

    The exercise asks for a function that introduces a person, and two calls that use it. Here is how to build it step by step.

    1. Define the function. Under the comment that marks the spot, write fun introduceYourself(name: String, language: String). It has two String parameters, in the order name, then language.
    2. Print the name. Inside the braces, use println with "My name is " joined to name by +. The text ends with a space, because + adds nothing between the parts.
    3. Print the language. On the next line, do the same with "I code in " and language.
    4. Introduce John. Inside mainFunction, below the comment for John, call introduceYourself("John", "Python"). The first argument becomes name, the second becomes language.
    5. Introduce Mary. Below the comment for Mary, call introduceYourself("Mary", "JavaScript"). Only the arguments change; no print statement is repeated.
    // Function goes here:
    fun introduceYourself(name: String, language: String) {
        println("My name is " + name)
        println("I code in " + language)
    }
    
    
    fun mainFunction() {
        // Introduce John:
        introduceYourself("John", "Python")
    
        // Introduce Mary:
        introduceYourself("Mary", "JavaScript")
    
    }
    

    Calling mainFunction() from a program's main prints:

    My name is John
    I code in Python
    My name is Mary
    I code in JavaScript
    

    Kotlin details and common mistakes

    • Types are required. Kotlin doesn't infer the type of a parameter. Writing fun introduceYourself(name, language) doesn't compile.
    • Order matters. Swapping the arguments, as in introduceYourself("Python", "John"), compiles because both are strings, and prints My name is Python. The compiler can check types but not meaning.
    • The argument count must match. Passing one argument to a function that declares two is a compile error.
    • The type must match. Passing a number where a String is expected is rejected by the compiler.
    • Parameters are read-only inside the function. You can use name but not assign a new value to it.
    • Quotes make text. "name" in quotes prints the word, while name without quotes is the variable.
    • String templates. Kotlin also lets you write "My name is $name", which gives the same text as joining with +.

    Key takeaways

    • Parameters let one function body do the work for many different values.
    • The header declares parameters with names and types; the call supplies arguments.
    • Arguments are matched to parameters by position.
    • To change what is printed for a person, change the arguments. To change how it is printed, change the function once.
    Keep going with KotlinAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free