What a function is
A function is a named container for a few lines of code that perform one task. Splitting a program into functions keeps each piece small and gives every piece a name that says what it does. Once a function exists, you can run it as many times as you like without copying its code.
Strictly speaking, a def inside a class or object is called a method in Scala. The lessons call them functions, and for the simple cases here the difference doesn't matter.
The shape of a definition
def functionName(): ReturnType = {
// function body
}
A definition has two parts.
The header
defis the keyword that starts a function definition.functionNameis the name you use to call it. Scala names are case-sensitive, and the usual style iscamelCase.- The parentheses hold the parameters. They are empty here because the function takes no input; later lessons fill them in.
ReturnType, written after a colon, is the type of the value the function gives back.- The
=sign connects the header to the body. It is required: without it, Scala sees only a declaration with no body.
The body
The body is the code that runs when the function is called, wrapped in curly braces. Indent it so readers can see where it belongs. Scala doesn't require the indentation, but it makes the code far easier to read.
The Unit return type
def sayHello(): Unit = {
println("Hello World")
}
This function prints a message and gives nothing useful back, so its return type is Unit. Unit is Scala's "no meaningful value" type, similar to void in other languages. It has exactly one value, written (). If you store the result of calling such a function, you get that value:
val r = sayHello()
println(r)
This prints Hello World and then ().
Defining is not running
A definition only tells Scala that the function exists. Its body doesn't run until something calls it. If you define sayHello and do nothing else, nothing is printed.
To call a function, write its name followed by parentheses:
def sayHello(): Unit = {
println("Hello World")
}
sayHello()
This prints Hello World. The parentheses are part of the call. A method defined with () must be called with () in Scala 3: writing just sayHello is rejected with method sayHello must be called with () argument.
Every call runs the whole body from the top. Calling sayHello() twice prints the message twice.
The lesson's exercise
The starter code has an empty function named mainFunction, and you add a function above it.
- Define
sayHelloabovemainFunction. Usedef, empty parentheses, the result typeUnitand= {, since the function prints instead of returning a value. - Print the message inside the braces.
println("Hello everybody!")goes in the body, because the body is what runs on each call. - Call it from
mainFunction. WritingsayHello()there is what makes the message appear.
The solution:
def sayHello(): Unit = {
println("Hello everybody!")
}
def mainFunction(): Unit = {
sayHello()
}
Running mainFunction() prints Hello everybody! once. Note that mainFunction is a function like any other: its body calls sayHello(), and a third function could call mainFunction() in the same way.
Common mistakes
- Forgetting the
=.def sayHello(): Unitalone declares a method without a body. - Defining but never calling. The program then prints nothing.
- Leaving out the parentheses in the call. Use
sayHello(), notsayHello. - Misspelling the name or changing its case.
sayhello()isn't the same assayHello(). - Putting the
printlnoutside the braces. It then belongs to the surrounding code, not to the function.
Key takeaways
- A function is defined with
def, a name, parentheses, a return type,=and a body in braces. Unitmeans the function returns no useful value.- The body runs only when the function is called, and it runs in full on every call.
