Scala Programming. Functions
Brief introduction to Scala functions
In this Scala programming notebook, we will review functions in Scala.
The Scala programming language is a JVM based language worthwhile exploring. In this post I give a brief review of functions in Scala as I continue my exploration of the language.
Functions, in general, are at the core of every programming language when it comes to code organization and implementin the DRY principle. Scala supports two types of functions namely functions and methods. The difference is that a method operates on an object a function does not.
We define a function as follows
def myFunc(x: Integer) = if(x >=0) x else -x
When defining a function we must
- specify the types of all parameters
- if the function is not recursive i.e. does not call itself, we do not have to specify the return type
- if the body of the function requires more than one expression,then we should use a block i.e.
{}
. The last expression of the block becomes the value that the function returns.
As an aside, it is possible to omit the return type of the function. Indeed, the Scala compiler can determine the return type from the type of the expression to the right of the =
symbol. However, this may not be always the case. The following example demonstrates that
def myAbs(x: Double) = if(x >=0) x else -x
val x = -10
myAbs(x)
x: Int = -10
res1_1: Double = 10.0
However, with a recursive function, we must specify the return type
def fuc(x: Int): Int = if(x <= 0) 1 else x*fuc(x-1)
Just like C++, Scala also supports default arguments i.e. the default arguments for functions that are used when we do not specify explicit values
def showMe(x: Int=5) = println("You want to show " + x)
showMe()
> You want to show 5
showMe(6)
> You want to show 6
One of the features that I really like in Python is the named argument(s). This feature is very handy for understanding arguments at call sites i.e. I don't need to do the trip to the (unavailable) documentation but more important is really helpful in mitigating errors. Scala also supports the idea of named arguments
def speak(arg1: String, arg2: String, arg3: String=" the end") = println(arg1 + arg2 + arg3)
speak(arg2=" is ", arg1="This ")
> This is the end
As you can see, the named arguments need not be in the same order as the parameters. Furthermore, we can mix unnamed and named arguments, provided the unnamed ones come first. This is similar to Python.
Often it is useful to have a function that can take a variable
number of arguments think of printf
. Usually we call these as varargs functions Scala supports this idea too
def sum(args: Int*): Int = {
var result = 0
for(arg <- args) result += arg
result
}
val s = sum(1, 4, 9, 16, 25)
> s:Int = 55
The actual type received by the function is of type Seq
. However, we can not do the following
val s = sum(1 to 5)
> cmd10.sc:1: type mismatch; found : scala.collection.immutable.Range.Inclusive
required: Int
val s = sum(1 to 5)
^Compilation Failed
Compilation Failed
That's because if the sum
function is called with one argument, that must be a single integer. Here is how we can fix this
val s = sum(1 to 5:_*)
> s:Int = 15
- Cay Horstmann
Scala for the impatient
, Addison-Wesley.