Functions

Functions in Zard define reusable blocks of code. They can receive parameters and return values.

Zard supports functions returning the following types:


Basic Function Syntax


function returnType functionName(parameters) {
    # code
}
Example:

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

Calling Functions

Functions can be invoked using the call keyword.

call sum(2, 3);
Functions returning values can also be used in expressions.

int result = sum(5, 4);
print(result);

Void Functions

Functions that do not return values use the void type.

function void hello() {
    print("Hello Zard!");
}

call hello();

Functions with Lists

Functions can receive complex types like lists. Example: Bubble Sort implementation.

function void bubbleSort(List arr) {

    int n = arr.size();

    for (int i = 0; i < n - 1; i++) {

        for (int j = 0; j < n - i - 1; j++) {

            int a = arr.get(j);
            int b = arr.get(j + 1);

            if (a > b) {

                arr.remove(j);
                arr.remove(j);

                List temp;

                for (int k = 0; k < j; k++) {
                    temp.add(arr.get(k));
                }

                temp.add(b);
                temp.add(a);

                for (int k = j; k < arr.size(); k++) {
                    temp.add(arr.get(k));
                }

                arr.clear();

                for (int k = 0; k < temp.size(); k++) {
                    arr.add(temp.get(k));
                }

            }

        }

    }

}
Usage:

List numeros = (23,13,9,40,80);

call bubbleSort(numeros);

print(numeros);

Mathematical Functions

Zard functions can be used to implement mathematical utilities.

function double sum(double a, double b) {
    return a + b;
}

function double sub(double a, double b) {
    return a - b;
}

function double mul(double a, double b) {
    return a * b;
}

function double div(double a, double b) {

    if (b == 0) {
        print("Erro: divisao por zero!");
        return 0.0;
    }

    return a / b;
}
Example:

double r = sum(2.5, 4.2);

print(r);

Loops Inside Functions

Functions can contain loops and complex logic. Example: Power function.

function double pow(double base, int exp) {

    double result = 1.0;
    int i = 0;

    while (i < exp) {
        result = result * base;
        i++;
    }

    return result;
}

Newton Method Example

Square root approximation:

function double sqrt(double x) {

    double guess = x / 2.0;
    int i = 0;

    while (i < 20) {
        guess = (guess + x / guess) / 2.0;
        i++;
    }

    return guess;
}

Trigonometric Approximations

Example implementations using Taylor series.

function double sin(double x) {

    double term = x;
    double sum = term;

    int n = 1;

    while (n < 10) {

        term = -term * x * x / ((2 * n) * (2 * n + 1));
        sum = sum + term;
        n++;

    }

    return sum;
}

function double cos(double x) {

    double term = 1.0;
    double sum = term;

    int n = 1;

    while (n < 10) {

        term = -term * x * x / ((2 * n - 1) * (2 * n));
        sum = sum + term;
        n++;

    }

    return sum;
}

Recursion

Functions can call themselves recursively. Example: Factorial. Iterative version:

function int fact(int n) {

    int result = 1;
    int i = 1;

    while (i <= n) {
        result = result * i;
        i++;
    }

    return result;

}
Recursive version:

function int factorial(int n) {

    if (n == 0) {
        return 1;
    }

    return n * factorial(n - 1);

}

Summary

Functions are one of the core building blocks of Zard. They allow: