Introduction to Scala Functions

Hello Readers,
Welcome to the second part of Scala Language Online Tutorials Series. Last post, we gone through basic introduction to Scala language. In this post we are going to look at few more interesting features of Scala Language. We will start with Scala Functions and have to look at how do we write and call Functions in Scala. In the last post we have seen how can we declare a variable or a value. Function declaration in Scala is very much similar to variables.

Below is a typical way of defining a function in Scala.

scala> def add (x: Int, y: Int) : Int = {x +y}
add: (x: Int, y: Int)Int

scala> add(2,4)
res0: Int = 6

For those coming from Java or similar background might find this confusing. So let’s have a deeper look at the function syntax. At first the ‘def’ keyword just flags the expression as a function declaration. the name of the function ‘add’ comes after that followed by list of arguments. Then there is :Int which is the function return type followed by the function body in curly braces.

That not enough yet. The one we saw above is a typical full form function declaration. There are many ways to cut it short that we will be seeing as and when we come across it.

Not only because it supports shorthand & less redundant syntax but also because of its syntactical closeness with variable declarations. In Scala both a variable and a function declaration are similar in syntax. We already discussed in last tutorial that functions are values. You can pass them to another function, return them or assignment to a variable, just like a variable.

Below is a simple variable declaration followed by a function declaration. We will identify how similar they are.

The keywords ‘var’ (or ‘value’) and ‘def’ are the only differences we see when we declare a variable and a function in Scala. Next thing is a unique identifier in case of variable declarations it is the name of the variable and in case of function it is function signature (combination of function name and parameters). In case of variables we know we can’t have another variable in same scope. Similarly in case of function you can’t have another function in the same scope with the same function name and parameters. Hence we will consider that part as a unique identifier. Then comes the Data type. In case of variables Data type tells what type of data it can hold in other words what kind of data its going to pass if it is assigned to some other variable. Similarly functions have Data types we call it as return type. That the type of data it is going to return or pass if assigned to a variable. Then after the equals symbol there comes the actual value of a variable and in case of function it is the actual value (or group of expressions that evaluates to a value) that will be returned from that function.

The fun is not over yet. In the above function we have a Single expression body. In such cases we can skip the curly braces. See below.

scala> def add (x: Int, y: Int) : Int = x + y
add: (x: Int, y: Int)Int

scala> add(2,4)
res0: Int = 6

 

Scala has a very powerful type inference mechanism. Which allows us to skip the return type. That means Scala’s compiler is smart enough to find out the actual return type of a function you write even if you do not specify explicitly. See below example, where the return type is skipped. But the compiler corruptly comes to know Int added to an Int will be Int hence the created function has automatically Int as a return type. In the next function definition I added an Int to a String and Scala compiler correctly inferred the return type to String. At the last I provided an explicit return type of Int but the body evaluates to a String. Scala compiler shouts with a ‘Type Mismatch’ exception.

scala> def add (x: Int, y: Int) = x + y
add: (x: Int, y: Int)Int //Int as a return type

scala> def add (x: Int, y: Int) = x + "any_string"
add: (x: Int, y: Int)String //String as a return type

scala> def add (x: Int, y: Int) : String= x + "any_string"
<console>:7: error: type mismatch;
    found : String
    required: Int
      def add (x: Int, y: Int) :Int = x + "as"
      ^

Have you noticed that none of the function has a explicit ‘return’ statement. Yes, that is because it is not required. Scala compiler and implicit return statement in a function and a very last expression of a function is automatically returned as an output of that function. Even if a function doesn’t return anything it still returns something. Let’s have a look at the print method in below example.

scala> def print (msg : String) = println(msg)
print: (msg: String)Unit

The above method takes a String as a parameter and simply prints it. Method is not expected to return anything. But still Scala compiler returns a Unit. The Unit object is returned when the method doesn’t return anything. That is little similar to Java’s void return type. So the thing to remember is If a method doesn’t return anything it returns Unit.

To start with, this is enough for the function. Let’s meet in the next tutorial to see some more on Scala Language Features.