Lesson 3

Welcome, learners! In this unit, we're diving into *function return values* in Scala programming. Function return values, often simply referred to as "returns," are results given back by a function after performing its assigned task. Let's dive in!

Let's create a function that returns a result. Suppose we want our function to perform a calculation — for instance, adding two numbers in a calculator program. Here's how we can write a function that adds two numbers and returns the sum:

Scala`1def addNumbers(num1: Int, num2: Int): Int = 2 // This function adds num1 and num2 and returns the sum. 3 num1 + num2 4 5@main def run: Unit = 6 // Call addNumbers with 5 and 7 as arguments and print the returned value 7 println(addNumbers(5, 7)) // Output: 12`

This function definition starts with the `def`

keyword followed by the function name (`addNumbers`

), a parameter list in parentheses (`num1: Int, num2: Int`

), and the return type (`: Int`

). The function body follows the `=`

symbol. The value of the last expression in the function (in this case, `num1 + num2`

) is the returned result.

Returns are not always simple numbers as in the example above. A function can return any type of data from the range Scala supports, including `Int`

, `String`

, `Double`

, `Boolean`

, `Char`

, and `Long`

. Let's explore this further using the calculation of a circle's area as an example:

Scala`1def circleArea(radius: Double) = 2 // This function calculates the circle's area and returns it. 3 3.14 * radius * radius 4 5@main def run: Unit = 6 println(circleArea(5)) // Output: 78.5`

In this example, the function `circleArea`

takes a `Double`

parameter named `radius`

and returns a `Double`

representing the area of the circle. The function uses the standard formula for the area of a circle (πr²) with 3.14 as the approximation for π. The return type in this case is not explicitly specified; Scala can infer that the result is a `Double`

based on the expression. However, it can be a good practice to specify the return type explicitly:

Scala`1def circleArea(radius: Double): Double = 2 3.14 * radius * radius 3 4@main def run: Unit = 5 println(circleArea(5)) // Output: 78.5`

Here, the return type is explicitly specified after the colon `:`

. The body of the function calculates the area and returns it as the result.

It's possible that a function has multiple return statements. When a return statement is reached, the function stops running, and all subsequent code within it is ignored. This ensures that the function exits as soon as it fulfills a condition that leads to a return. If we want to return early from a function, we should use an explicit `return`

keyword:

Scala`1def passOrFail(score: Int): String = 2 if score > 75 then 3 return "Pass" // If this condition is met, the function immediately exits with "Pass" 4 5 "Fail" // If the condition above is not met, the function returns "Fail" 6 7@main def run: Unit = 8 // Call passOrFail with 80 as argument and print the returned value 9 println(passOrFail(80)) // Output: Pass`

Some functions have no return value and just perform side effects. In Scala, these are referred to as `Unit`

functions, where `Unit`

signifies "no value". Here's an example of a `Unit`

function that prints the multiplication table for a specific number:

Scala`1def printMultiplicationTable(num: Int): Unit = 2 for i <- 1 to 10 do 3 // Multiply num with numbers from 1 to 10, print results 4 println(s"$num x $i = ${num * i}") 5 6@main def run: Unit = 7 // Call printMultiplicationTable with 5 as argument 8 printMultiplicationTable(5)`

This function has no return value. Its purpose is to print the multiplication table. If a function has a `Unit`

return type, you can define it explicitly, but it's often optional since Scala will infer it.

If you need to perform an early return in a `Unit`

function, you can use the `return`

keyword, similar to functions with other return types. For example:

Scala`1def printPositiveOrNegative(num: Int): Unit = 2 if num < 0 then 3 println("Number is negative") // Early return with a side effect 4 return 5 6 println("Number is positive") // This will execute if the number is not negative 7 8@main def run: Unit = 9 // Call printPositiveOrNegative with -3 and 5 as arguments 10 printPositiveOrNegative(-3) // Output: Number is negative 11 printPositiveOrNegative(5) // Output: Number is positive`

Well done! We've learned all about function return values in Scala, the `return`

keyword, how to combine it with different data types, and even how to create `Unit`

functions. Now, it's time for some practical, hands-on practice to reinforce these concepts. Happy coding!