import ballerina/io;

// This function definition has two parameters of type `int`. 
// `returns` clause specifies type of return value.
function add(int x, int y) returns int {

    int sum = x + y;
    // `return` statement returns a value.
    return sum;

}

public function main() {
    io:println(add(5, 11));
}

Functions

A function accepts zero or more arguments and returns a single value. Function parameters are declared as in C. You are not allowed to assign to parameters in Ballerina.

import ballerina/io;
function add(int x, int y) returns int {

This function definition has two parameters of type int. returns clause specifies type of return value.

    int sum = x + y;
    return sum;

return statement returns a value.

}
public function main() {
    io:println(add(5, 11));
}
bal run functions.bal
16