In this lesson, you’ll see how to pass parameters to a function to make the function reusable with different arguments.
You’ll be using Kotlin Playground to edit and run the code. To follow along, go to https://play.kotlinlang.org/, or fire up your browser and type Kotlin Playground in the search bar. The first link should take you to the Kotlin Playground.
Here, a main() function is already defined. First, you’ll create a function called greetings(). The function takes two parameters: the name of a student and their favorite programming language. Then, it prints a greeting message to the screen.
fun greetings(name: String, favLanguage: String) {
println("Hello! I am $name. My favorite language is $favLanguage.")
}
Then, call the greetings() function from the main() function and pass arguments for the name and favorite programming language.
fun main() {
greetings("Bilbo", "Kotlin")
}
When you run the code, “Hello! I am Bilbo. My favorite language is Kotlin” prints on the screen. Just as expected, Bilbo’s favorite programming language is Kotlin.
Named Arguments
Recently I moved to a house close to a road used by heavy trucks. This afternoon so many trucks were passing. I was distracted just a bit and wrote the following code:
fun main() {
greetings("Kotlin", "Bilbo")
}
Mmmh! When I ran the code, it compiled without any errors. But the result’s quite wrong. It prints “Hello! I am Kotlin. My favorite language is Bilbo.” There’s no programming language called Bilbo. Whoops! I had swapped the arguments I passed to the function call.
Kotlin’s named arguments come to the rescue. Using named arguments, you can name one or more function arguments when calling the function. I’ll update the greetings() function call to use named arguments.
fun main() {
greetings(favLanguage = "Kotlin", name = "Bilbo")
}
When I run the code again, the result is correct.
Named arguments are helpful when a function has many arguments. When you use named arguments in a function call, you can change their listed order and still get the correct result.
When you name a function argument, you must name all arguments that don’t have default values.
Default Arguments
Function parameters can have default values. For example, you can pass Kotlin as the default value of the favLanguage parameter:
fun greetings(name: String, favLanguage: String = "Kotlin") {
println("Hello! I am $name. My favourite is language $favLanguage.")
}
When calling the greetings() method, you can skip passing an argument for favLanguage.
fun main() {
greetings(name = "Bilbo")
}
When you run the code, it compiles successfully and prints “Hello! I am Bilbo. My favorite language is Kotlin.”
If you don’t want the value of favLanguage to be Kotlin, you can pass a different value when you call the greetings() function.
fun main() {
greetings(name = "Bilbo", favLanguage = "Swift")
}
This code prints “Hello! I am Bilbo. My favorite language is Swift.”
Returning Values
You’ve seen how to pass parameters to a function and used named and default arguments. Now, you’ll create a function that returns a value. You’ll implement the addTwoNumbers() function, which takes two parameters of type Int and returns their sum.
fun addTwoNumbers(num1: Int, num2: Int): Int {
val sum = num1 + num2
return sum
}
addTwoNumbers() has two parameters: num1 and num2. Its return type is Int. The function adds the two arguments and stores the result in the sum variable. Then, it returns the sum of the two numbers.
Next, call the addTwoNumbers() function in the main() function, passing 7 and 13 as arguments, and assign the value returned by the function to the sumOfTwoNumbers variable. Print the value stored in the sumOfTwoNumbers variable.
fun main() {
val sumOfTwoNumbers = addTwoNumbers(7, 13)
println("The sum is $sumOfTwoNumbers")
}
When you run the code now, it prints “The sum is 20” on the screen.
But it turns out there’s a shorter syntax for returning values. When a function body consists of a single expression, you can use = instead of return to return values from the function. Now, you’ll update the addTwoNumbers() function to use the shorter syntax.
fun addTwoNumbers(num1: Int, num2: Int): Int = num1 + num2
When you run the code, the result is the same.
You’re now ready to update the findSquareRoot() function you wrote in the previous lesson to return the square root of different numbers. You can pause this video, implement your solution, and then continue with the video.
Hopefully you implemented something like this:
import kotlin.math.sqrt
...
fun findSquareRoot(num: Float): Float {
val squareRoot = sqrt(num)
return squareRoot
}
Don’t forget to import the kotlin.math.sqrt package above the main function.
The findSquareRoot() function takes one parameter of type Float, and its return type is Float.
Now, you can call the function and pass in different arguments:
fun main() {
val squareRoot = findSquareRoot(576f)
println("Square root => $squareRoot")
}
This produces the following result:
Square root => 24.0
Finding the square root of 729:
fun main() {
val squareRoot = findSquareRoot(729f)
println("Square root => $squareRoot")
}
The result is:
Square root => 27.0
Bonus! You could use the shorter syntax with the findSquareRoot() function, like this:
fun findSquareRoot(num: Float): Float = sqrt(num)
That concludes this demo. Up next is a summary.