Learn How To Create And Use Functions
Overview
In this tutorial, we will learn the basics for creating and using functions in Scala.
Steps
1. How to define and use a function which has no parameters and has a return type
Let's create a simple function which will return the favorite donut as a String.
To define a function in Scala, you need to use the keyword def.
Then add the name of your function which in our case will be favoriteDonut followed by an empty pair of parenthesis ().
To specify the return type you need to use the colon : followed by the return type which in our case is a String.
Finally, you will use the = followed by {} to enclose the body of your function
println("Step 1: How to define and use a function which has no parameters and has a return type")
def favoriteDonut(): String = {
"Glazed Donut"
}
val myFavoriteDonut = favoriteDonut()
println(s"My favorite donut is $myFavoriteDonut")
You should see the following output when you run your Scala application in IntelliJ:
Step 1: How to define and use a function which has a return type
My favorite donut is Glazed Donut
NOTE:
- We did not use any return keyword in favoriteDonut() as you would in say Java or .NET. The last line within the body of the function is the one that will be returned back to the caller.
- In upcoming tutorial, we will also show that you can define functions using the val keyword!
2. How to define and use a function with no parenthesis
If you are coming from another programming language such as Java or .NET, defining functions with no parenthesis will certainly be a shocker!
println("\nStep 2: How to define and use a function with no parenthesis")
def leastFavoriteDonut = "Plain Donut"
println(s"My least favorite donut is $leastFavoriteDonut")
You should see the following output when you run your Scala application in IntelliJ:
Step 2: How to define and use a function with no parenthesis
My least favorite donut is Plain Donut
NOTE:
- In general, you should define your functions without parenthesis if you are defining a function that does not have any side effects.
- We also did not provide a return type of String to the leastFavoriteDonut function to leverage what we've learned from the tutorial on Scala Type Inference.
3. How to define and use a function with no return type
In Step 1, we defined a function which had a return type of String. But what if you had to define a function which does not have any return type?
println("\nStep 3: How to define and use a function with no return type")
def printDonutSalesReport(): Unit = {
// lookup sales data in database and create some report
println("Printing donut sales report... done!")
}
printDonutSalesReport()
You should see the following output when you run your Scala application in IntelliJ:
Step 3: How to define and use a function with no return type
Printing donut sales report... done!
NOTE:
- If you have used other mainstream languages such as Java or .NET, Unit is similar to using the void keyword in a method.
Summary
In this tutorial, we went over the following:
- How to define and use a function which has no parameters and no return type
- How to define and use a function with no side effects
- How to define and use a function with no return type
- How to use type inference when defining function with no return type
Tip
- Make sure you do not abuse the style defined in Step 2 - functions with no parenthesis.
- Limit its usage to pure and simple functions only!
Source Code
The source code is available on the allaboutscala GitHub repository.
What's Next
In the next tutorial, I will show you how to define function which takes parameters.