Scala Lesson 9 – Functions | Dataplexa

Functions in Scala

In this lesson, you will learn about functions in Scala. Functions are reusable blocks of code that perform a specific task.

Functions help make programs modular, readable, and easier to maintain. Instead of repeating logic, you define it once and reuse it.


Why Functions Are Important

Functions allow you to:

  • Reuse code efficiently
  • Break large programs into smaller parts
  • Improve readability and maintainability
  • Reduce errors caused by duplication

Defining a Function

In Scala, functions are defined using the def keyword.

def greet(): Unit = {
  println("Hello, Scala!")
}

greet()

Here:

  • def starts the function definition
  • greet is the function name
  • Unit means no return value

Function with Parameters

Functions can accept parameters to work with dynamic values.

def greetUser(name: String): Unit = {
  println(s"Hello, $name!")
}

greetUser("Alice")
greetUser("Bob")

Parameters allow the same function to behave differently based on input.


Function with Return Value

Functions can return values using an explicit return type.

def add(a: Int, b: Int): Int = {
  a + b
}

val result = add(10, 20)
println(result)

Scala automatically returns the last expression in the function.


Single-Line Functions

If a function contains only one expression, it can be written in a single line.

def square(x: Int): Int = x * x

println(square(5))

This makes code concise and readable.


Type Inference in Functions

Scala can infer return types automatically. Explicit return types are recommended for clarity.

def multiply(a: Int, b: Int) = a * b

println(multiply(4, 5))

Functions with Multiple Parameters

Functions can take multiple parameters separated by commas.

def fullName(first: String, last: String): String = {
  first + " " + last
}

println(fullName("John", "Doe"))

Default Parameter Values

Scala allows default values for parameters.

def greetPerson(name: String = "Guest"): Unit = {
  println(s"Welcome, $name!")
}

greetPerson()
greetPerson("Scala Learner")

If no argument is passed, the default value is used.


Named Arguments

Scala allows arguments to be passed by name.

def calculate(a: Int, b: Int): Int = a - b

println(calculate(b = 5, a = 10))

Named arguments improve readability and reduce mistakes.


Functions vs Methods

In Scala:

  • Methods are defined inside classes or objects
  • Functions can be assigned to variables

You will explore this difference more deeply in functional programming lessons.


📝 Practice Exercises


Exercise 1

Write a function that returns the square of a number.

Exercise 2

Write a function that takes two numbers and returns their sum.

Exercise 3

Write a function with a default parameter value.


✅ Practice Answers


Answer 1

def square(n: Int): Int = n * n
square(6)

Answer 2

def sum(a: Int, b: Int): Int = a + b
sum(8, 12)

Answer 3

def greet(name: String = "User"): Unit = {
  println(s"Hello, $name")
}

greet()

What’s Next?

In the next lesson, you will learn about Collections in Scala, which allow you to store and process groups of data efficiently.