|
Oktatás * Programozás 2 * Szkriptnyelvek * Adator. prog. Teaching * Prog. for Data Sci. Félévek Linkek * kalendárium |
Scala /
functions, methods
Functions
def double(n: Int): Int = {
2 * n
}
val x = 4
println(double(x)) // 8
If the function's body is just one expression, then you can omit the curly braces: def double(n: Int): Int = 2 * n // equivalent with the previous Methods
def hello(name: String) {
println("Hello, %s!".format(name))
}
Notice that its return type is missing and there is no "=" sign before the opening brace. This is a method, not a function. The code above is actually a shorthand for the following. Here it is explicitly stated that its return type is
def hello(name: String): Unit = {
println("Hello, %s!".format(name))
}
|
![]() Blogjaim, hobbi projektjeim * The Ubuntu Incident [ edit ] |