Start learning free

LESSON 34 OF 414 · FUNCTIONS I

Why Use Functions in Scala: Write Once, Call Many Times

Lesson Read the full explanation

Now that we've learned about functions, let's see why they are so important in our programmers' life:

Consider this: A student named Andy made a mistake and so his teacher told him to write this text 3 times on the whiteboard:

This is Andy
Andy made a mistake
Andy suffers the consequences

We would put this in a simple program like this:

println("This is Andy")
println("Andy made a mistake")
println("Andy suffers the consequences")

println("This is Andy")
println("Andy made a mistake")
println("Andy suffers the consequences")

println("This is Andy")
println("Andy made a mistake")
println("Andy suffers the consequences")

That's manageable. But what if the text consisted of more than 3 lines? And what if the teacher asked Andy to print it for 10 times or 100 times?

You can imagine that our code would get super long while executing 3 different instructions overall.


The story with functions:

This is where functions shine. We can write a function that prints the desired text and then just call the function for as many times as the teacher asks to:

def printMessage(): Unit = {
	println("This is Andy")
	println("Andy made a mistake")
	println("Andy suffers the consequences")
}

printMessage()
printMessage()
printMessage()

Changing the story:

Need to print the text 3 more times? We call the function 3 more times instead of adding 9 lines to our program.

Need to change the student's name to Mike? We only change it inside the function instead of changing it in the whole program.

def printMessage(): Unit = {
	println("This is Mike")
	println("Mike made a mistake")
	println("Mike suffers the consequences")
}

printMessage()
printMessage()
printMessage()
printMessage()
printMessage()

Note:

If you still don't like this code, especially the fact that we have 5 duplicate lines of code: printMessage(), you are correct! We will learn how we can make this even better with loops!


Assignment
Follow the Coding Tutorial and let's write some functions.


Hint
Look at the examples above if you get stuck.


STEP 1 OF 5
    Ctrl + Enter to check

    SCALA · FUNCTIONS I

    Why Functions in Scala, explained

    The problem: repeated code

    Suppose a student named Andy has to write three lines on the whiteboard three times. As a Scala program, that is nine println statements:

    println("This is Andy")
    println("Andy made a mistake")
    println("Andy suffers the consequences")
    println("This is Andy")
    println("Andy made a mistake")
    println("Andy suffers the consequences")
    println("This is Andy")
    println("Andy made a mistake")
    println("Andy suffers the consequences")
    

    This works, but it doesn't scale. If the teacher asks for 10 or 100 copies, you paste the same lines 10 or 100 times. If the text changes, you must find and edit every copy, and missing one leaves a bug. The program says the same thing over and over.

    The fix: put the lines in a function

    A function gives a group of statements a name. You write the statements once and run them by calling the name:

    def printMessage(): Unit = {
        println("This is Andy")
        println("Andy made a mistake")
        println("Andy suffers the consequences")
    }
    
    printMessage()
    printMessage()
    printMessage()
    

    The output is the same nine lines, but the program is much shorter. Each call to printMessage() runs the three println statements again.

    What functions buy you

    • More repetitions are cheap. Printing the text three more times means adding three short calls, not nine more lines.
    • Changes happen in one place. To change the student's name to Mike, edit the three lines inside printMessage. Every call picks up the change at once.
    • Names explain intent. printMessage() says what is happening without the reader going through the three lines.
    • Smaller pieces. A long program becomes a set of small functions that each do one job.

    The calls themselves are still repeated. Loops, which come later, remove that repetition too, but you still need the function to name the work.

    Defining versus calling

    The syntax is the same as in the previous lessons. A function is defined with def, a name, empty parentheses, the result type Unit and a body in braces. The definition alone prints nothing. The body runs once per call, and a call is the name followed by parentheses. Leaving the parentheses out is a compile error in Scala 3: method printMessages must be called with () argument.

    The lesson's exercise

    The starter code has mainFunction with a line that prints Sorry, I didn't hear you and two comments that mark where the introduction should happen. The task is to introduce yourself twice without writing the lines twice.

    1. Define introduceYourself below the first comment. Use def, empty parentheses, the result type Unit and = {. The body will hold the whole introduction, so it is written once.
    2. Print the first line inside it: println("My name is Andy").
    3. Print the second line next: println("I love coding"). It stays inside the braces.
    4. Call it first in mainFunction, under the first comment, with introduceYourself(). Both print lines run.
    5. Call it again, under the second comment, after the "Sorry" line. Both lines are printed a second time, with no repeated code.

    The solution:

    // Function goes here:
    def introduceYourself(): Unit = {
        println("My name is Andy")
        println("I love coding")
    }
    
    def mainFunction(): Unit = {
        // Introduce yourself the first time:
        introduceYourself()
        println("Sorry, I didn't hear you")
        // Introduce yourself again:
        introduceYourself()
    }
    

    Running mainFunction() prints:

    My name is Andy
    I love coding
    Sorry, I didn't hear you
    My name is Andy
    I love coding
    

    If the introduction ever changes, only the body of introduceYourself needs an edit, and both calls follow.

    Common mistakes

    • Defining the function but not calling it. Nothing from the function appears in the output.
    • Calling it only once. The expected output has the introduction twice.
    • Leaving a println outside the braces. The line then belongs to the surrounding code instead of the function.
    • Calling the function at the wrong point. The calls must sit on either side of the "Sorry" line, because the output follows the order of the statements.
    • Dropping the parentheses in the call. introduceYourself alone doesn't compile.

    Key takeaways

    • A function lets you write a group of statements once and run it as often as you need.
    • Changing the behaviour means editing one body, not every copy.
    • Defining a function runs nothing; each call name() runs the whole body.
    Keep going with ScalaAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free