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;

def functionName([arguments]) : return type

For example,

def multiply(a:Int ,b:Int): Int

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 functionName ([list of parameters]) : [return type] = { function body return [expr] }

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;

def multiply(a:Int,b:Int) : Int = { var c : Int = 0 c = a*b return c; }

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.

def hello( ) : Unit = { println("Hello, World!") }

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.

object mult { def main(args:Array[String]) { println("Multiplication result of two numbers is : "+multiply(20,21)); } def multiply( a:Int, b:Int ) : Int = { var c:Int = 0 c = a * b return c } }

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;

functionName( list of parameters )

For example,

multiply(4,3)

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

[instance.]functionName( list of parameters )

For example,

test.multiply(5,6)

Consider an example of calling a function;

object multest { def main(args: Array[String]) { println( "Result is : " + multiply(12,14) ); } def multiply( a:Int, b:Int ) : Int = { var c:Int = 0 c = a * b return c } } 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 <code>multest.main(null)</code> in the scala shell and see the following output; <strong>Result is : 168</strong> Now let us see an example how to invoke a function by creating an instance. class Multiplication { def multiply(a : Int , b: Int ) :Int = { var c : Int = 0 c = a * b return c } }

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;

object multest { def main(args:Array[String]) { var mul = new Multiplication() println( "Result is : " +mul.multiply(15,16)); } }

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.

def min(x: Int, y: Int, z: Int) = { def min(i: Int, j: Int) = if (i < j) i else j min(x,min(y,z)) }

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.

## Leave a Reply