| Free tutorials for Java, Eclipse and Web programming |
Functions in Scala are similar to methods in Java. They are defined with the keyword "def" and have an optional parameter list and a return parameter after the ":" sign. The body of the functional is indicated by "=" and then curly braces {}
The return parameter is for a non-recursive function optional and will be determined by Scala automatically based on the function expressions. Also the curly braces for the method body are optional if the function statement has only one statement.
If a function has no parameters you can leave the () away for the call. Also the dot is not required for calling a function with only one parameter.
Create a scala project "de.vogella.scala.functions" and try the following code.
package de.vogella.scala.functions
object Function {
def main(args: Array[String]) {
println("Hello, world!")
val f = Function;
println(f.min(3,5))
println(f.min2(3,5))
// If you have no parameters you can leave the () away
f.sayHello
f.sayHello("Lars")
// You can leave away the point and if you only have one paramter also the brackets
f sayHello "Lars"
}
//Explicit return parameter
def min(i:Int, j:Int):Int = {
if (i>j)
return i
else
return j
}
//Without explicit return parameter
def min2(i:Int, j:Int) = {
if (i>j)
i
else
j
}
// One one line a body and no parameters
def sayHello = println("Hello")
// One parameter
def sayHello(s: String ) = println("Hello " + s)
}
For recursive functions you have to define the return type explicitly.
package de.vogella.scala.functions
object Factorial {
def main(args: Array[String]) {
val f = Factorial
println( f.factorial(5) )
println( f.factorial(6) )
println( f.factorial(7) )
println( f.factorial(8) )
}
def factorial (x:BigInt):BigInt= {
if (x==0) 1
else x* factorial (x-1)
}
}
Scala permits almost very sign to define a method. For example you can define a method with the name +.
As you can leave out the dot and the braces for methods with only one parameter you can write in Scala a + b and "+" is a call to your method on a with the parameter b.
override is a keyword in Scala. Everytime you override a function you have to indicate this via this keyword.
package de.vogella.scala.functions
class OverrideExample {
// Without override this would give an error
override def toString() = "test"
}
Scala supports the uniform access principle which states that it should not make a difference if the programmer access a variable directly or via a method.
The following demonstrates this. Create the Scala project "de.vogella.scala.uniformaccess".
From the client the usage of both versions of Square is the same.
package de.vogella.scala.uniformaccess
class Square {
private var l = 1 ;
var wide = 1;
def length = l;
def length_=(s : Int)= {
require (s > 0 )
this.l = s
}
}
package de.vogella.scala.uniformaccess
class Square {
var length = 1 ;
var wide = 1;
}
package de.vogella.scala.uniformaccess
object Main {
def main(args: Array[String]) {
var s = new Square();
println(s.length);
s.length = 2;
println(s.length);
}
}