Functions in Scala – Part 1 | Scala

Scala is a  a multi-paradigm language that supports both functional and object-oriented programming with a growing community and many useful features Scala worth learning, and it has been adopted by big enterprises such as Linkedin , Twitter, and many others.

As Functional programming is one of the main strength points in Scala and and understanding functions is necessary to build efficient applications in Scala, in this tutorial we will summarize the main functions blocks.

Basic syntax for the function will be
def <function name>(<input parameter>:<data type>,…..):<return data type> = {…function logic….}

For example lets create a function to get the multiplication of two numbers it will be as following

  def multiply(num1:Int, num2:Int):Int = { num1*num2  }

Now let’s call the function we just created, to call a function you need to call function name and pass function parameters (if any)
<function name>(<input parameter>:<data type>,…..)

  multiply(100,2)

Input Default values

Scala provide option for you to give a default value for input parameter, in case user didn’t provide the value, default value will be used, lets check the following function which subtract two numbers

  // function with default input value
  def subtract(x:Int, y:Int=100):Unit = {
    println(x-y)
  }
  subtract(300)

As we can see we provided only one input, and we didn’t provide another input parameter, because we provided default value for the second number, function executed as 300-100 and result was 200, in case we have default value for both parameters we need to specify parameter name when we pass it to the function and we will learn how to do that in the coming section

Named Parameters

We can provide inputs to functions using parameter names, and in this case we don’t need to provide input parameters in the same order, let’s see the next example where we provide some informations and output a statement with user name and other informations

  //Call Parameters by name
  def userInput(firstName:String, lastName:String, birthMonth:String, birthYear:Int) = {
    println(s"Hi $firstName $lastName, your birth month is $birthMonth, and your born year is $birthYear")
  }

Now if we tried to call the function with parameters order doesn’t match the order we specified when we create the function, we got an error

  userInput("Mark","John",2012,"March")

Now we can call the same function but with specifying the name for each parameter

  userInput(lastName = "Mark",birthMonth = "March", firstName = "John", birthYear = 1978 )

Input-less Functions

In case our function doesn’t need input, we can create a function with empty parentheses to indicate input-less function, example for such functions could be a function to log a certain event to a database or write event status to a log file, we can specify output data type or we can let scala compiler infer the output data type from the function logic.

def eventLog():Unit = println("STATUS: event completed successfully ")
eventLog()

In this function we print a status about a certain event.

In next part we will continue other function patterns.

Ahmed Ibrahem

Ahmed Ibrahem is working as Data Engineering Team lead, with a wide experience in data management projects and technical implementation using different technologies, and delivering end to end projects starting from business analysis to Data Warehouse modeling and implementation to BI design to customers in different industries.

guest
0 Comments
Inline Feedbacks
View all comments