Anonymous Functions

In this lesson, you will be introduced to anonymous functions and learn their syntax.

We'll cover the following

Remember when we learned about literals at the beginning of this course? They are defined as fixed values appearing directly in the source code. Literals don’t need to be named, they can simply be used directly.

In the previous lessons, we needed to create multiple small functions for our summation functions to execute. But we only required the functionality of those functions. Naming them was an extra unnecessary step in that scenario.

What we need are something similar to literals. Functions that do not need to be named as their functionality is only required for a single instance.

Scala provides a solution known as anonymous functions.

Anonymous functions are function literals which don’t require a name. They can be passed as-is to a higher-order function.


Let’s look at the syntax for an anonymous function.

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy