Learn Kotlin - Functions Cheatsheet - Codecademy
Learn Kotlin - Functions Cheatsheet - Codecademy
Functions
Functions
A function is a named, reusable block of code that can
be called and executed throughout a program. fun greet() {
A function is declared with the fun keyword, a println("Hey there!")
function name, parentheses containing (optional) }
arguments, as well as an (optional) return type.
To call/invoke a function, write the name of the fun main() {
function followed by parentheses. // Function call
greet() // Prints: Hey there!
}
Function Arguments
In Kotlin, an argument is a piece of data we can pass
into a function when invoking it. fun birthday(name: String, age: Int)
To pass data into a function, the function’s header must {
include parameters that describe the name and data println("Happy birthday $name! You
type of the incoming data. If a function is declared with turn $age today!")
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!
}
/
Default Arguements
We can give arguments a default value which provides
an argument an automatic value if no value is passed fun favoriteLanguage(name, language
into the function when it’s invoked. = "Kotlin") {
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 provide additional readability. fun findMyAge(currentYear: Int,
To name an argument, write the argument name birthYear: Int) {
followed by the assignment operator ( = ) and the var myAge = currentYear
argument value. The argument’s name must have the - birthYear
same name as the parameter in the function being println("I am $myAge years old.")
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.
}
/
Return Statement
In Kotlin, in order to return a value from a function, we
must add a return statement to our function using the // Return type is declared outside
return keyword. This value is then passed to the parentheses
where the function was invoked. fun getArea(length: Int, width: Int):
If we plan to return a value from a function, we must Int {
declare the return type in the function header. var area = length * width
fun main() {
var myArea = getArea(10, 8)
println("The area is $myArea.") //
Prints: The area is 80.
}
/
Function Literals
Function literals are unnamed functions that can be
treated as expressions: we can assign them to variables,
call them, pass them as arguments, and return them fun main() {
from a function as we could with any other value. // Anonymous Function:
Two types of function literals are anonymous functions var getProduct = fun(num1: Int,
and lambda expressions.
num2: Int): Int {
return num1 * num2
}
println(getProduct(8, 3)) //
Prints: 24