Learn How To Create And Use Functions

By Nadim Bahadoor | Last updated: July 25, 2017 at 13:42 pm

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 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 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.

4. Use type inference to define function with no return type

Similar to Step 2, you can take advantage of Scala's type inference when defining a function which has no return type.


println("\nStep 4: Use type inference to define function with no return type")
def printReport {
  // lookup sales data in database and create some report
  println("Printing donut report... done!")
}
printReport

You should see the following output when you run your Scala application in IntelliJ:


Step 4: Use type inference to define function with no return type
Printing donut report... done!

NOTE:

  • Notice that we did not include the parenthesis () when calling the function printReport.  This feature becomes more useful and concise as you get to do function chaining which we will see in upcoming tutorials.

This concludes our tutorial on Learn How To Create And Use Functions and I hope you've found it useful!

 

Stay in touch via Facebook and Twitter for upcoming tutorials!

 

Don't forget to like and share this page :)

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.

Nadim Bahadoor on FacebookNadim Bahadoor on GithubNadim Bahadoor on LinkedinNadim Bahadoor on Twitter
Nadim Bahadoor
Senior Software Developer | Nephila Capital
Founder of allaboutscala.com. I have over 10 years of experience in building large scale real-time trading systems in the financial industry. Passionate about Distributed Systems, Scala, Big Data and Functional Programming. Stay in touch for upcoming tutorials!
Other allaboutscala.com tutorials you may like:

Share this article on