0% found this document useful (0 votes)
3 views

Learn Kotlin_ Functions Cheatsheet

This document is a cheatsheet for learning functions in Kotlin, detailing how to declare and invoke functions, use function arguments, and implement default and named arguments. It also covers return statements, single expression functions, and function literals, including anonymous functions and lambda expressions. The document provides code examples for each concept to illustrate their usage in Kotlin programming.

Uploaded by

hasan zahid
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Learn Kotlin_ Functions Cheatsheet

This document is a cheatsheet for learning functions in Kotlin, detailing how to declare and invoke functions, use function arguments, and implement default and named arguments. It also covers return statements, single expression functions, and function literals, including anonymous functions and lambda expressions. The document provides code examples for each concept to illustrate their usage in Kotlin programming.

Uploaded by

hasan zahid
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

12/12/23, 12:37 PM Learn Kotlin: Functions Cheatsheet | Codecademy

Cheatsheets / Learn Kotlin

Functions

Functions

A function is a named, reusable block of code that can be fun greet() {


called and executed throughout a program.
println("Hey there!")
A function is declared with the fun keyword, a function
name, parentheses containing (optional) arguments, as }
well as an (optional) return type.
To call/invoke a function, write the name of the function
fun main() {
followed by parentheses.
// Function call
greet() // Prints: Hey there!
}

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.
}

Single Expression Functions

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

Function literals are unnamed functions that can be


treated as expressions: we can assign them to variables,
fun main() {
call them, pass them as arguments, and return them 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, num2:
and lambda expressions.
Int): Int {
return num1 * num2
}
println(getProduct(8, 3)) // Prints: 24

// 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

You might also like