import ballerina/io;
function printValue (string value) {
    io:println(value);
}
function add (int a, int b) returns (int) {
    return a + b;
}function main (string[] args) {
    printValue("This is a sample text");
    int result = add(5, 6);
    io:print(result);
}

Functions

Functions operate the same way as any other language. It is a mechanism to create a reusable unit of functionality within a program.

import ballerina/io;
function printValue (string value) {
    io:println(value);
}

This function takes a string argument but does not have a return value.

function add (int a, int b) returns (int) {
    return a + b;
}

This function takes two int values and return their sum as an int.

function main (string[] args) {
    printValue("This is a sample text");

Call a function which prints the given value to the console.

    int result = add(5, 6);
    io:print(result);
}

Directly print the value that was returned from the function to the console.

$ ballerina run functions.bal
This is a sample text
11