![]() |
VOOZH | about |
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.
Syntax:
(z:Int, y:Int)=> z*y
Or
(_:Int)*(_:Int)
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.
Example 1:
Output:
Geeks12Geeks
GeeksforGeeks
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.
Example 2:
Output:
Welcome to GeeksforGeeks...!!
DogCat
DogCat