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

Learn Kotlin - Functions Cheatsheet - Codecademy

Functions allow reusable blocks of code to be declared and called. Functions can take arguments as input and return values. Default arguments, named arguments, and single expression functions provide shortcuts. Function literals like anonymous functions and lambda expressions can be treated as expressions and passed around.

Uploaded by

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

Learn Kotlin - Functions Cheatsheet - Codecademy

Functions allow reusable blocks of code to be declared and called. Functions can take arguments as input and return values. Default arguments, named arguments, and single expression functions provide shortcuts. Function literals like anonymous functions and lambda expressions can be treated as expressions and passed around.

Uploaded by

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

Cheatsheets / Learn Kotlin

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

  // 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 a shorthand syntax to create our function. fun fullName(firstName: String,
Instead of placing curly brackets after the function lastName: String) = "$firstName
header to contain the function’s code block, we can $lastName"
use an assignment operator = followed by the
expression being returned. fun main() {
  println(fullName("Ariana",
"Ortega")) // Prints: Ariana Ortega
  println(fullName("Kai", "Gittens"))
// Prints: Kai Gittens
}

/
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

  // Lambda Expression


  var getDifference = { num1: Int,
num2: Int -> num1 - num2 }
  println(getDifference(10, 3)) //
Prints: 7
}

You might also like