In Scala, An anonymous function is also known as a function literal. A function which does not contain a name is known as an anonymous function. An anonymous function provides a lightweight function definition. It is useful when we want to create an inline function.
(z:Int, y:Int)=> z*y Or (_:Int)*(_Int)
- In the above first syntax, => is known as a transformer. The transformer is used to transform the parameter-list of the left-hand side of the symbol into a new result using the expression present on the right-hand side.
- In the above second syntax, _ character is known as a wildcard is a shorthand way to represent a parameter who appears only once in the anonymous function.
When a function literal is instantiated in an object is known as a function value. Or in other words, when an anonymous function is assigned to a variable then we can invoke that variable like a function call. We can define multiple arguments in the anonymous function.
We are allowed to define an anonymous function without parameters. In Scala, We are allowed to pass an anonymous function as a parameter to another function.
Welcome to GeeksforGeeks...!! DogCat DogCat
- Scala | Nested Functions
- Scala | Functions - Basics
- Partial Functions in Scala
- Scala | Functions Call-by-Name
- Higher Order Functions in Scala
- Scala | Partially Applied functions
- Currying Functions in Scala with Examples
- Scala Tutorial – Learn Scala with Step By Step Guide
- Scala Map
- Set in Scala | Set-2
- Scala | Either
- Set in Scala | Set-1
- Scala Int until(end: Int) method with example
- Scala | Value Classes
- Scala | Polymorphism
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.