def (function)
Try it yourself →Defines your own command.
Syntax
def name(p: Type): ReturnType = expression
Description
Defined with def; return type is written with :. The body is given with = (single expression) or a { } block.
Example
def square(x: Int): Int = x * x def addUp(a: Int, b: Int): Int = a + b println(square(5)) println(addUp(8, 9))
Expected output
25 17
When to use?
Preventing code repetition, breaking the program into parts.