Scala Tutorial - Learn How To Create Higher Order Function - Callback Function Parameter

By Nadim Bahadoor | Last updated: March 16, 2018 at 11:34 am

Overview

In this tutorial, we will learn how to create Higher Order Function which is a function that takes another function as its parameter.

 

This tutorial is a continuation of the previous Higher Order Function tutorial and we will showcase how to define a function which has a callback or Option callback parameter.

Steps

1. How to define a function with a callback parameter

Let's say that you have a function named printReport() which needs to be passed in a function for sending email once the report is completed.

 

You can define the printReport() function to have a call-by-name parameter of type unit that will essentially be the email callback function.


println("Step 1: How to define a function with a callback parameter")
def printReport(sendEmailCallback: () => Unit) {
  println("Printing report ... started")
  // look up some data in database and create a report
  println("Printing report ... finished")
  sendEmailCallback()
}

 

2. How to call a function which has a callback parameter

When calling the printReport() function from Step 1, you will need to pass in a callback function for sending email.


println("\nStep 2: How to call a function which has a callback parameter")
printReport(() =>
println("Sending email ... finished")
)

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


Step 2: How to call a function which has a callback parameter
Printing report ... started
Printing report ... finished
Sending email ... finished

 

3. How to call a function without providing its callback parameter

What if you wanted to print the report but not send any email?


println("\nStep 3: How to call a function without providing its callback parameter")
// printReport() // You get compile time error

NOTE:

  • Calling printReport() function without passing a callback function will give you a compile time error.

You can instead pass in an empty anonymous function as shown below, although the syntax does not look that elegant:


printReport(() => {}) // Not that elegant.

 

4. How to define a function with an Option callback

Let's make use of what you've learned from the Option Tutorial and define another printReport() function which would have an Option callback parameter.


println("\nStep 4: How to define a function Function with an Option callback")
def printReportWithOptionCallback(sendEmailCallback: Option[() => Unit] = None) {
  println("Printing report ... started")
  // look up some data in database and create a report
  println("Printing report ... finished")
  sendEmailCallback.map(callback => callback())
}

NOTE:

  • We are using the map() function to filter out any None callback function.

5. How to call a function without providing its callback parameter

With the printReportWithOptionCallback() function from Step 4 above, you can now more elegantly call the print report function without having to specify any callback function.


println("\nStep 5: How to call a function without providing its callback parameter")
printReportWithOptionCallback() // more elegant

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


Step 5: How to call a function without providing its callback parameter
Printing report ... started
Printing report ... finished

 

6. How to call a function with Option callback parameter

If you've followed the tutorial on Option, the syntax for calling a function with an Option parameter should be familiar to you.

 

With regards to the callback function parameter, you now need to wrap it within Some()


println("\nStep 6: How to call a function with Option callback parameter")
printReportWithOptionCallback(Some(() =>
  println("Sending email wrapped in Some() ... finished")
))

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


Step 6: How to call a function with Option callback parameter
Printing report ... started
Printing report ... finished
Sending email wrapped in Some() ... finished

This concludes our tutorial on Learn How To Create Higher Order Function - Callback Function Parameter 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 a function with a callback parameter
  • How to call a function which has a callback parameter
  • How to call a function without providing its callback parameter
  • How to define a function with an Option callback parameter
  • How to call a function without providing its callback parameter
  • How to call a function with Option callback parameter

Tip

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 can be stored as values.

Nadim Bahadoor on FacebookNadim Bahadoor on GithubNadim Bahadoor on LinkedinNadim Bahadoor on Twitter
Nadim Bahadoor
Technology and Finance Consultant with over 14 years of hands-on experience building large scale systems in the Financial (Electronic Trading Platforms), Risk, Insurance and Life Science sectors. I am self-driven and passionate about Finance, Distributed Systems, Functional Programming, Big Data, Semantic Data (Graph) and Machine Learning.
Other allaboutscala.com tutorials you may like: