Senin, 10 Januari 2011

Nested Function Scala ( Fungsi Bersarang )

One of the noteworthy features of Scala is that it supports nested function.

See below:

$ cat NestFunction.scala
object NestFunction {
def outside ( x: Int ) {
def inside ( x: Int ) : Int = {
if ( x > 10 ) ( x * -1 ) else ( x + 5 );
}

println ( x + " --> " + inside ( x ) )
}

def main ( args: Array[String] ) {
outside ( 1 )
outside ( 15 )
}
}

$ scalac NestFunction.scala && scala NestFunction
1 --> 6
15 --> -15

Tidak ada komentar:

Poskan Komentar