Oktatás * Programozás 2 * Szkriptnyelvek * levelezősök Félévek Linkek * kalendárium |
Scala /
functions, methods
Functionsdef 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 Methodsdef 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 ] |