Introduction to functions in scala

Knoldus Blog Audio
Reading Time: 4 minutes

A function is a group of statements that perform a certain task. Instead of writing the same code again and again for different inputs, we can simply make a function and call whenever required. Each function must perform a specific task.

Difference between Functions & Methods

A scala method is a part of a class that has a name, signature, bytecode, etc. whereas a function is an object which can be stored in a variable

Function Declarations and Definitions

Syntax :

def functionName ([list_of_parameters]) : [return_type] = {
  //Function Body
}

Here the def keyword is used to declare a function. functionName is the name of the function in the lower camel case. list_of_parameters is a comma-separated list of input parameters with their data type. return_type is the data type which the function will return. The function body is enclosed by braces {}. Here we defined the code that needs to be executed.

Example :

def sum(num1: Int, num2: Int): Int = {
   num1 + num2
}

In functional programming, we focus on results rather than changing things. we called a function that performs the operation and returns a result. The goal is to make data immutable so in a functional world, we create function rather than a statement. this reduces the risk of side effects in the code. Side effects are unexpected results in a program caused by changing something. The results can corrupt the logic of a program. For example, a side effect would be a function that modifies a variable or data structure. Functions need to do one thing and that is to return a result. Functional programming is a style of programming that emphasizes writing applications using only pure functions and immutable values. In this blog, we are gonna talk about pure function.

What is pure function?

A function is called a pure function if the function’s output depends only on its input variables and it has no side effects like modifying an argument. They are those functions that don’t read any other values except those given as input and follow its internal algorithm to produce the output. The function should be side-effect free. Pure functions are used heavily in Functional Programming.

Example of pure function

These Scala String methods are also pure functions :

  • substring
  • length
  • isEmpty

A pure function can be made as shown in the following program :

In the above code, we can see the function takes two parameter and return their sum. It did not change the input values. It used + operator and return the result. The sum function has no side-effects and it is a pure function.

Benefits of pure function

  • Easy to Test

One of the major benefits of using pure functions is they are immediately testable. They will always produce the same result if you pass in the same arguments. Maintaining and refactoring code is much easier.

  • Easy to debug

Pure functions are easier to debug because the output only depends on the argument and logic. You don’t have to look outside the scope of the function.

  • Readability

Pure functions are much easier to read. All relevant inputs and dependencies are provided as parameters, so no effects are observed that alter variables outside of the set of inputs.

  • Caching

Pure functions always return the same output for the same input. So, we can cache the results.

Default Arguments in Scala

We can define a default value for an argument in a function.

Here we have defined the default value for num2 as 5. So if we provide only one argument then scala will take the default value. But make sure that any default arguments must be after all non-default arguments.

Output :

Scala Named Arguments

In scala we can pass argument in a different order with names. Let’s take an example

Output:

Scala Functions with Variable Arguments

When we don’t know how many arguments we want to pass, we can use a variable argument for this. Let’s take an example.

Output :

Nested Functions in Scala

In Scala, we can define a function inside another function. Let’s take an example

Output :

Scala Anonymous Function

An anonymous function is also known as a function literal. It is useful to create a function in one line of code. Anonymous function doesn’t contains a name. Let’s take an example.

Output :

Conclusion

In this post, we have learned about scala functions.

Referances

If you find this article interesting, please check out our other articles.

Leave a Reply