import ballerina/io;
public function main(string name,
                     int age = 18,
                     string year = "Freshman",
                     string... modules)
                  returns error? {
    if (name.length() < 5) {
        error e = error("InvalidName", message = "invalid length");
        return e;
    }    string info = string `Name: ${name}, Age: ${age}, Year: ${year}`;    if (modules.length() > 0) {
        info += ", Module(s): " + modules.toString();
    }
    io:println(info);
}# To run this sample, navigate to the directory that contains the
# `.bal` file, and execute the `ballerina run` command.

The Main Function

A public function named main is considered as an entry point to a Ballerina program. The main function is data-binding and can have zero or more parameters whose types are subtypes of anydata, including any number of required/defaultable parameters and/or a single rest parameter. The main function could also return a value whose type is a subtype of error?.

import ballerina/io;
public function main(string name,
                     int age = 18,
                     string year = "Freshman",
                     string... modules)
                  returns error? {

The main function that accepts student information and prints out a formatted string. The first parameter name is a required parameter, while the second parameter age is a defaultable parameter with the default value 18. The third parameter year is also a defaultable parameter. The rest parameter modules represents the additional arguments. The main function may return an error or ().

    if (name.length() < 5) {
        error e = error("InvalidName", message = "invalid length");
        return e;
    }

Return an error if the name is invalid.

    string info = string `Name: ${name}, Age: ${age}, Year: ${year}`;
    if (modules.length() > 0) {
        info += ", Module(s): " + modules.toString();
    }
    io:println(info);
}
# To run this sample, navigate to the directory that contains the
# `.bal` file, and execute the `ballerina run` command.
# Use the ballerina `run` command to invoke the `main` function specifying `Alice`
# as the string argument for `name`. `18` would be set as the value for
# `age` and `Freshman` would be set as the value for `year`.
$  ballerina run the_main_function.bal Alice
Name: Alice, Age: 18, Year: Freshman
# Use the ballerina `run` command to invoke the `main` function specifying `Alice`
# as the string argument for `name` and `20` as the integer value for
# `age`. Both arguments are specified as positional arguments.
$ ballerina run the_main_function.bal Alice 20
Name: Alice, Age: 20, Year: Freshman
# Use the ballerina `run` command to invoke the `main` function specifying `Alice`
# as the string argument for `name` and `Sophomore` as the string argument for
# `year`. The value for `year` is specified as a named argument.
$ ballerina run the_main_function.bal Alice -year=Sophomore
Name: Alice, Age: 18, Year: Sophomore
# Use the ballerina `run` command to invoke the `main` function specifying values for
# all parameters, including the rest parameter. All arguments are specified as
# positional arguments.
$ ballerina run the_main_function.bal Alice 20 Sophomore math physics
Name: Alice, Age: 20, Year: Sophomore, Module(s): math physics
# Use the ballerina `run` command to invoke the `main` function specifying an invalid 
# string as the argument for `name`. The `error` returned would be printed.
$ ballerina run the_main_function.bal Ali
error: InvalidName message=invalid length