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)
- 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.
Example 1:
// Scala program to illustrate the anonymous method object Main { def main(args: Array[String]) { // Creating anonymous functions // with multiple parameters Assign // anonymous functions to variables var myfc1 = (str1:String, str2:String) => str1 + str2 // An anonymous function is created // using _ wildcard instead of // variable name because str1 and // str2 variable appear only once var myfc2 = (_:String) + (_:String) // Here, the variable invoke like a function call println(myfc1("Geeks", "12Geeks")) println(myfc2("Geeks", "forGeeks")) } } |
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:
// Scala program to illustrate anonymous method object Main { def main(args: Array[String]) { // Creating anonymous functions // without parameter var myfun1 = () => {"Welcome to GeeksforGeeks...!!"} println(myfun1()) // A function which contain anonymous // function as a parameter def myfunction(fun:(String, String)=> String) = { fun("Dog", "Cat") } // Explicit type declaration of anonymous // function in another function val f1 = myfunction((str1: String, str2: String) => str1 + str2) // Shorthand declaration using wildcard val f2 = myfunction(_ + _) println(f1) println(f2) } } |
Output:
Welcome to GeeksforGeeks...!! DogCat DogCat
Recommended Posts:
- Using anonymous functions with the map method in Scala
- Scala | Functions Call-by-Name
- Scala | Functions - Basics
- Partial Functions in Scala
- Scala | Nested Functions
- Currying Functions in Scala with Examples
- Scala | Partially Applied functions
- Higher Order Functions in Scala
- Scala Tutorial â Learn Scala with Step By Step Guide
- Set in Scala | Set-2
- Set in Scala | Set-1
- Scala Map
- Scala | Either
- Scala Map get() method with example
- Scala Byte +(x: Int): Int
- Stack in Scala
- Scala Int to(end: Int) method with example
- Scala Byte /(x: Int): Int
- Scala Map max() method with example
- Scala Map last() method with example
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.

