Scala Tutorial Scala References

Scala - Functions



A function is a block of statements which executes only when it is called somewhere in the program. A function provides re-usability of same code for different inputs, hence saves time and resources. Scala has many built-in functions and one of the common function is main() which is used to execute code. Users can also create their own function which is also termed as user-defined functions.

Create Function

In Scala, creating a function starts with def keyword and contains the following structure:

//Defining function
def function_name([parameters]) : [return_type] = {
  statements
}

In the above definition, the method is implicitly declared abstract if the equal to sign is not used which implies that the function can not be used to return value if the equal to sign is not used.

Here, return_type could be any valid Scala data type and parameters will be a list of variables separated by comma. Note that the return_type and parameters are optional.

Call Function

After defining the function, it can be called anywhere in the program with it's name followed by parenthesis containing function's parameter(s), if it has any.

Syntax

//Calling function
function_name(parameters)

Example: A function with no parameter

In the example below, a function HelloWorld is created to print Hello World!. It requires no parameters to execute and has no return type.

object MainObject {
  def HelloWorld() {
    println("Hello, world!")
  }

  def main(args: Array[String]) {
    HelloWorld()  
  }
}

The output of the above code will be:

Hello World!

Parameter

A parameter (or also known as argument) is a variable which is used to pass information inside a function. In above example, the function does not has any parameter. But a user can create a function with single or multiple parameters. Value of a parameter can be further used by the function to achieve desired result.

Example: A function with parameters

In the example below, the function called MyFunction is created which requires two integer numbers as parameters and print sum of two numbers in desired style. Please note that, the function returns nothing.

object MainObject {
  def MyFunction(x: Int, y: Int) {
    var z = x + y
    println(s"Sum of ${x} and ${y} is: ${z}")
  }

  def main(args: Array[String]) {
    MyFunction(15, 10)  
  }
}

The output of the above code will be:

Sum of 15 and 10 is: 25

Function to Return Values

A function can be used to return values. To achieve this, user must have to define return type and equal to = sign in the declaration of the function. In the example below, Int is used as the return type.

object MainObject {
  def MyFunction(x: Int, y: Int) : Int = {
    var z = x + y
    return z
  }

  def main(args: Array[String]) { 
    var x = 15
    var y = 10   
    println(s"Sum of ${x} and ${y} is: ${MyFunction(x, y)}")
  }
}

The output of the above code will be:

Sum of 15 and 10 is: 25

Default Parameter Value

Default value can be assigned to a parameter at the time of creating function. When the function is called without parameter then it uses the default value.

Example:

In the example below, the MyFunction function is used to add two, three and four integer numbers. The function uses the default value of a given parameter when the function is called without it. The default value of a parameter is set while defining the function.

object MainObject {
  def MyFunction(p: Int, q: Int, r: Int = 0, s: Int = 0) : Int = {
    return (p+q+r+s)
  }

  def main(args: Array[String]) { 
    var a = 15
    var b = 10 
    var c = 5 
    var d = 1  
    println(MyFunction(a, b))
    println(MyFunction(a, b, c))
    println(MyFunction(a, b, c, d)) 
  }
}

The output of the above code will be:

25
30
31

Function with Named Parameter

In Scala, parameter values can be passed in function with key = value syntax also. In this way, the order of parameter does not matter.

Example:

The example below describes how to pass parameters in a key = value pair.

object MainObject {
  def MyFunction(x: Double, y: Double) : Double = {
    return x/y
  }

  def main(args: Array[String]) {  
    println(MyFunction(x = 5, y = 10))
    println(MyFunction(y = 10, x = 5))
  }
}

The output of the above code will be:

0.5
0.5



More Concepts on Scala Functions