Methods

Methods

1
def add(x: Int, y: Int): Int = x + y
2
println(add(1, 2)) // 3
3
def addThenMultiply(x: Int, y: Int)(multiplier: Int): Int = (x + y)*multiplier
4
println(addThenMultiply(1, 2)(3)) // 9
5
def getSquareString(input: Double): String = {
6
val square = input*input
7
square.toString
8
}
9
println(getSquareString(2.5)) // 6.25
Copied!
The last expression in the body is the method’s return value, or you can explicitly use return keyword to return from method anywhere.
1
def getSquareString(input: Double): String = {
2
val square = input*input
3
return square.toString
4
}
Copied!
Last modified 1yr ago
Copy link