Methods

Methods

def add(x: Int, y: Int): Int = x + y
println(add(1, 2)) // 3
def addThenMultiply(x: Int, y: Int)(multiplier: Int): Int = (x + y)*multiplier 
println(addThenMultiply(1, 2)(3)) // 9
def getSquareString(input: Double): String = {
val square = input*input
square.toString
}
println(getSquareString(2.5)) // 6.25

The last expression in the body is the method’s return value, or you can explicitly use return keyword to return from method anywhere.

def getSquareString(input: Double): String = {
val square = input*input
return square.toString
}

Last updated