import ballerina/io;

public function main() {
    // Defines an anonymous function.
    function (string, string) returns string anonFunction =
            function (string x, string y) returns string {
                return x + y;
            };
    io:println("Output: ", anonFunction("Hello ", "World.!!!"));

    // Defines an anonymous function with `var`.
    var anonFunction2 = function (string x, string y, string... z) returns string {
                            string value = x + y;
                            foreach var item in z {
                                value += item;
                            }
                            return value;
                        };
    io:println("Output: ", anonFunction2("Ballerina ", "is ", "an ", "open ",
     "source ", "programming ", "language."));

    // If an anonymous function contains only the return statement in the body,
    // you can use the `arrow function expression` instead of the anonymous function.
    // The types of the input parameters are inferred from the left-hand side.
    // The return of the arrow function expression is determined by the
    // evaluation of the expression on the right-hand side of the `=>` symbol.
    function (string, string) returns string arrowExpr = (x, y) => x + y;
    io:println("Output: ", arrowExpr("Hello ", "World.!!!"));
}

Anonymous Functions

In Ballerina, anonymous functions are a syntactic shortcut for defining inline functions. The only difference between an anonymous function and a normal function definition is that an anonymous function does not have a name.

import ballerina/io;
public function main() {
    function (string, string) returns string anonFunction =
            function (string x, string y) returns string {
                return x + y;
            };
    io:println("Output: ", anonFunction("Hello ", "World.!!!"));

Defines an anonymous function.

    var anonFunction2 = function (string x, string y, string... z) returns string {
                            string value = x + y;
                            foreach var item in z {
                                value += item;
                            }
                            return value;
                        };
    io:println("Output: ", anonFunction2("Ballerina ", "is ", "an ", "open ",
     "source ", "programming ", "language."));

Defines an anonymous function with var.

    function (string, string) returns string arrowExpr = (x, y) => x + y;
    io:println("Output: ", arrowExpr("Hello ", "World.!!!"));
}

If an anonymous function contains only the return statement in the body, you can use the arrow function expression instead of the anonymous function. The types of the input parameters are inferred from the left-hand side. The return of the arrow function expression is determined by the evaluation of the expression on the right-hand side of the => symbol.

# To run this sample, navigate to the directory that contains the
# `.bal` file, and execute the `ballerina run` command below.
ballerina run anonymous_functions.bal
Output: Hello World.!!!
Output: Ballerina is an open source programming language.
Output: Hello World.!!!