In this lesson, you will learn how functions work and how to write and call functions in Scala.
A function in Scala is a collection of statements that perform some given operation. Scala permits nested functions which means that a function can be defined inside another function. Moreover, function names in Scala can contain characters like +, &, -, / etc.
The following is covered here:
1. Function Declaration and Definition
Function declaration specifies the name of the function and the return type. The syntax for declaring a function in Scala is given below:
def functionName ([parameters list]) : [return type]
Function definition specifies everything about the function. It includes the function declaration and the function body.
def functionName ([parameters list]) : [return type] = { statements .... return [expr] }
Note that the “expr” in the return statement is optional. This means that a function may or may not return a value. Also note that the parameter list is optional as well.
The code below is an example of a function that takes two parameters which are numbers and returns the product of the two numbers:
def getProduct (num1: Int, num2: Int): Int = { var product:Int = 0 product = num1 * num2 return product }
Unit Functions – These are also called Units. They are functions that does not return anything similar to void functions in Java.
2. Calling a Function
Function call in Scala is similar to function call in Java. You simply specify the function name and the arguments to the function. Let’s write a program that includes a function that calculates the max of three numbers. They we call this function using three values.
object ScalaTutorial { def findMax (num1: Int, num2: Int, num3: Int): Int = { var max:Int = num1 if (num2 > num1) { max = num2 } else if (num3 > num1) { max = num3 } return max } def main(args: Array[String]) { println("The max of 5, 12, 9 is " + findMax(5, 12, 9) ) } }
Here we write a function called findMax(). This function using Conditional Statement to find the max of three numbers. Then in the main function, we call the findMax() function with three values and the max of the three numbers is returned.
Note that you can also use the max() function available in the scala.max package to achieve the same thing.
3. Nested Functions
In Scala, you can actually define a function inside another function. This is called a nested function. The syntax for nested function is given below:
def function1() //Outer function def function2(){ //nested function body } }
The program below is an example of nested functions to find the factorial of numbers.
object NestedFunctionDemo { def factorial(i: Int): Int = { def fact(i: Int, accumulator: Int): Int = { //Nested function if (i <= 1) accumulator else fact(i - 1, i * accumulator) } fact(i, 1) } def main(args: Array[String]) { println( factorial(1) ) println( factorial(2) ) println( factorial(3) ) } }
In the code above, we have a function fact() nested inside a parent function factorial(). The fact() takes a number and some total (accumulator) and multiplies accumulator by the given number minus 1. However, if the given number is <= 1. Total is returned, meaning that the factorial has been found. The the factorial() function takes a number and finds the factorial of the number by repeatedly calling the fact() function.