Learn Kotlin_ Functions Cheatsheet
Learn Kotlin_ Functions Cheatsheet
Functions
Functions
Function Arguments
In Kotlin, an argument is a piece of data we can pass into fun birthday(name: String, age: Int) {
a function when invoking it.
println("Happy birthday $name! You turn
To pass data into a function, the function’s header must
include parameters that describe the name and data type $age today!")
of the incoming data. If a function is declared with }
parameters, then data must be passed when the function
is invoked. We can include as many parameters as
needed. fun main() {
birthday("Oscar", 26) // Prints: Happy
birthday Oscar! You turn 25 today!
birthday("Amarah", 30) // Prints: Happy
birthday Amarah! You turn 30 today!
}
https://www.codecademy.com/learn/learn-kotlin/modules/learn-kotlin-functions/cheatsheet 1/4
12/12/23, 12:37 PM Learn Kotlin: Functions Cheatsheet | Codecademy
Default Arguements
We can give arguments a default value which provides an fun favoriteLanguage(name, language =
argument an automatic value if no value is passed into the
"Kotlin") {
function when it’s invoked.
println("Hello, $name. Your favorite
programming language is $language")
}
fun main() {
favoriteLanguage("Manon") // Prints:
Hello, Manon. Your favorite programming
language is Kotlin
favoriteLanguage("Lee", "Java") //
Prints: Hello, Lee. Your favorite
programming language is Java
}
Named Arguments
We can name our arguments when invoking a function to fun findMyAge(currentYear: Int, birthYear:
provide additional readability.
Int) {
To name an argument, write the argument name followed
by the assignment operator ( = ) and the argument value. var myAge = currentYear - birthYear
The argument’s name must have the same name as the println("I am $myAge years old.")
parameter in the function being called.
}
By naming our arguments, we can place arguments in any
order when the function is being invoked.
fun main() {
findMyAge(currentYear = 2020, birthYear
= 1995)
// Prints: I am 25 years old.
findMyAge(birthYear = 1920, currentYear
= 2020)
// Prints: I am 100 years old.
}
https://www.codecademy.com/learn/learn-kotlin/modules/learn-kotlin-functions/cheatsheet 2/4
12/12/23, 12:37 PM Learn Kotlin: Functions Cheatsheet | Codecademy
Return Statement
In Kotlin, in order to return a value from a function, we // Return type is declared outside the
must add a return statement to our function using the
parentheses
return keyword. This value is then passed to where the
function was invoked. fun getArea(length: Int, width: Int): Int
If we plan to return a value from a function, we must {
declare the return type in the function header.
var area = length * width
// return statement
return area
}
fun main() {
var myArea = getArea(10, 8)
println("The area is $myArea.") //
Prints: The area is 80.
}
If a function contains only a single expression, we can use fun fullName(firstName: String, lastName:
a shorthand syntax to create our function.
String) = "$firstName $lastName"
Instead of placing curly brackets after the function
header to contain the function’s code block, we can use
an assignment operator = followed by the expression fun main() {
being returned.
println(fullName("Ariana", "Ortega")) //
Prints: Ariana Ortega
println(fullName("Kai", "Gittens")) //
Prints: Kai Gittens
}
https://www.codecademy.com/learn/learn-kotlin/modules/learn-kotlin-functions/cheatsheet 3/4
12/12/23, 12:37 PM Learn Kotlin: Functions Cheatsheet | Codecademy
Function Literals
// Lambda Expression
var getDifference = { num1: Int, num2:
Int -> num1 - num2 }
println(getDifference(10, 3)) // Prints:
7
}
Print Share
https://www.codecademy.com/learn/learn-kotlin/modules/learn-kotlin-functions/cheatsheet 4/4