Function without parameters
Simple Scala function Example
def hello_world() = {
println ("Hello world!")
}
Hello world!
Function with parameters
Example of a scalar function that supports parameters
def sum_data(x: Int = 2, y: Int = 3): Int = {
x + y
}
sum_data(2,3)sum_data: (x: Int, y: Int)Int res: Int = 5
Function with variable number of parameters
Example of a Scala function that supports a variable number of parameters
def show_parameters( args:String* ) = {
var i : Int = 0;
for( arg println("Arg value[" + i + "] = " + arg );
i = i + 1;
}
}
show_parameters("first","second")show_parameters: (args: String*)Unit Arg value[0] = first Arg value[1] = second
Anonymous function
Example of anonymous Scala function
var next = (x: Int) => x + 1 next(3)
Next: int = > int = Res: INT = 4
” width=”20″ height=”20″>
” width=”20″ height=”20″>





0 Comments