Scala Functions – Introduction

Introduction

Functions in any language can be defined as a group of statements that constitute a logic which is performed repeatedly during the execution of a program. Functions in scala are no different.Scala identifies both functions and methods. They are both treated a bit differently. Finally, scala does support functional programming(FP) paradigms and this blog entry tries to take you through some of the FP paradigms and get you a good brief.

They follow the following format

def functionName ([list of parameters]) : [return type] = {
  function body
  return expression
}

An example of  a function in Scala. See Below

def multiplyBy2(x:Int) :Int = {
  val y = x * 2
  return y
}

In addition to that, scala also simplifies creation of code. In functions,  it removes the need to write boilerplate code. Every programmer likes less code. Speaking of less code some of the stuff scala does away with  is

  • No need of semi-colons
  • Simple functions do not need curly braces.
  • return statement is optional
  • Function without parameters can called without parenthesis

Let’s look at the example below

def multiplyBy2(x:Int) : Int = {
  val y = x * 2
  //Return the value of y. No need to return statement
  y
}

This code can be further simplified

def multiplyBy2(x:Int) : Int = {
  //Return the value of y. No need to return statement
  x * 2
}

We can take it a step further!

def multiplyBy2(x:Int):Int = x * 2

This reduction of boilerplate code allows less code to manage. We will revisit this example when we look at anonymous functions.

Pure Functions

Beyond the syntax, Scala is a functional programming language and has different types of functions. Functions can be pure or they can have side effects. Pure functions are one of the cornerstones of functional programming. Functions are said to be Pure Functions if they follow these two rules. I quote

1. Its return value is the same for the same arguments (no variation with local static variables, non-local variables, mutable reference arguments or input streams from I/O devices).
2. Its evaluation has no side effects (no mutation of local static variables, non-local variables, mutable reference arguments or I/O streams).

That output will remain the same irrespective of how many times that function is called with that parameter. Thus pure functions are similar to mathematical functions. In mathematics, only parameters and the logic of function are used to evaluate the output.

Pure functions, can be evaluated and their output can be stored and hence there is no need of re-evaluating them for the same set of parameters. This makes the code more performant and written in very flexible ways. Hence pure functions are much like values and can also be treated as first-class values hence can be assigned to variables, passed to functions and returned from other functions. We would be looking at all of them in a bit. Writing pure functions takes a bit of getting used but once you get over the hump(not a big one!) it should make things really easy.

Side effects are caused when in addition to the output the function changes something else. This may be changing a variable value(local or non-local) also known as mutation, looking up for some other data other than the data passed to the function. If a function has a side effect then it is not possible to guarantee the same output for the same set of parameters.

Local Functions

Before we dive deep into functions let’s look at one final concept of local functions. In Scala, it is possible to define functions inside functions. Such functions are local to the functions in which they are defined. See Below

def multiplyBy2(x:Int) :Int = {
  //Local function
  def add10():Int={
    x+10
  }
  add10 * 2
}

A function add10 is local to the function multiplyBy2. add10 in this function 10 is added to value of x . The function multiplyBy2 calls add10 and then multiplies the result by 2. Thus if we call multipleyBy2(5) we get the result as (5+10) * 2 = 30.

The above function tell about a few other things as well and they are discussed in next entry for function literal.

Next up is an entry on Functional Literals. Till then have fun!

Leave a Comment