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.
- Define the function. Under the comment that marks the spot, write
fun introduceYourself(name: String, language: String). It has twoStringparameters, in the ordername, thenlanguage. - Print the name. Inside the braces, use
printlnwith"My name is "joined tonameby+. The text ends with a space, because+adds nothing between the parts. - Print the language. On the next line, do the same with
"I code in "andlanguage. - Introduce John. Inside
mainFunction, below the comment for John, callintroduceYourself("John", "Python"). The first argument becomesname, the second becomeslanguage. - 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 printsMy 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
Stringis expected is rejected by the compiler. - Parameters are read-only inside the function. You can use
namebut not assign a new value to it. - Quotes make text.
"name"in quotes prints the word, whilenamewithout 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.