A function is a set of statements combined together to perform a specific task. The code can be divided logically into separate functions where each function is assigned a specific task.

The function in Scala is a complete object which can be assigned to a variable whereas a method in Scala is a part of class having name, signature, bytecode and annotations. Function name can have characters like ++,+,-,– etc.

A function can be defined inside a function known as nested functions which is supported in Scala unlike in Java.

Scala Function Declarations

The syntax for declaring a function is;

For example,

Here def is the short form for define and multiply is the function name. a and b are parameters to the multiply function of Integer data type and the result returns an Integer data type.

Function Definition

The function definition takes the following form;

def is the keyword to define a function, functionName is the name of the function, list of parameters are the variables separated by comma and return type is the datatype of the return value. function body contains the logic and return keyword can be used along with an expression in case function returns value.

For example;

multiply is the name of the function with two variables a and b. We declare another variable c of integer data type, store the result of a*b in this variable and return the computed variable c.

Run the program by typing multiply(50,20) in Scala shell and you will get output like res0: Int = 1000.


A function which does not return anything can return Unit which is equivalent to void in Java. The functions which does not return anything are called procedures in scala.

Here we are just printing hello world so the return data type is Unit.

Run the program by typing hello and you will get output as Hello, World!.


Let us see a complete example of a function by combining the declaration and definition and test the function by creating the object.

The line “def multiply( a:Int, b:Int ) : Int” is the function declaration and “var c:Int = 0,c = a * b” constitute the function body. return c is the return value of the function. All the three declaration, definition and return type constitute a complete function.

Run the above function code by typing mult.main(null) and see the result as below.

Multiplication result of two numbers is : 420

Invoking a Function

The way of calling a function in Scala is;

For example,

We can also create an instance and invoke the function as;

For example,

Consider an example of calling a function;

We are creating an object multest and defining a method multiply which accepts two variables a and b of integer data type and then perform the multiplication of a and b variables storing the result of the operation in variable c. We are writing a method main and calling the function multiply(12,14) along with the parameters.

Run the above code by typing multest.main(null) in the scala shell and see the following output;

Result is : 168

Now let us see an example how to invoke a function by creating an instance.

Multiplication class is created and the multiply function is defined with variables a and b and return the variable c which stores the multiplication result of the two variables passed.

Create an object multest and call the method multiply creating instance of Multiplication class as;

mul is the instance of the Multiplication class and the method is called as mul.multiply(15,16)

Run the code in the shell by typing multest.main(null) which produces the below output.

Result is : 240


Nested Functions

A function defined inside another function is called Nested function. Scala differs from Java in this feature as nested functions are not supported in Java.

Consider an example of nested function.

The method min accepts three parameters x,y and z of type Integer and then again we define min func to find the minimum of two numbers first and then with the result of the two found and the number we find the minimum. The minimum of 3 numbers function is defined first and then minimum of two numbers is defined and these are known as nested functions.

Run the above by calling min function as min(12,34,6) and you will get result as res21: Int = 6.

Nested functions are not required to be of same name, they can have different name too.

That’s all for functions in Scala programming language, we will look into more Scala core features in coming articles.

By admin

Leave a Reply

%d bloggers like this: