5

Functions

Reusable code blocks

Defining Functions

Yoruba: ise (function), padapọ (return)
English: fn, return
Defining Functions
// Define a function
ise greet(name) {
    Irosu.fo("Kaabo, " + name + "!");
}

// Call the function
greet("Ifá");     // Output: Kaabo, Ifá!
greet("Orunmila"); // Output: Kaabo, Orunmila!
// Define a function
fn greet(name) {
    Fmt.println("Hello, " + name + "!");
}

// Call the function
greet("Ifá");     // Output: Hello, Ifá!
greet("Orunmila"); // Output: Hello, Orunmila!

Return Values

Return Values
ise add(a, b) {
    padapọ a + b;
}

ayanmo result = add(5, 3);
Irosu.fo("Sum: " + result);  // Sum: 8

// Chain function calls
Irosu.fo(add(add(1, 2), add(3, 4)));  // 10
fn add(a, b) {
    return a + b;
}

let result = add(5, 3);
Fmt.println("Sum: " + result);  // Sum: 8

// Chain function calls
Fmt.println(add(add(1, 2), add(3, 4)));  // 10

Multiple Parameters

Parameters
ise calculate_area(width, height) {
    padapọ width * height;
}

ayanmo area = calculate_area(10, 5);
Irosu.fo("Area: " + area);  // Area: 50
fn calculate_area(width, height) {
    return width * height;
}

let area = calculate_area(10, 5);
Fmt.println("Area: " + area);  // Area: 50

Functions with No Return

Void Functions
ise log_message(level, msg) {
    Irosu.fo("[" + level + "] " + msg);
}

log_message("INFO", "Program started");
log_message("WARN", "Low memory");
log_message("ERROR", "Connection failed");
fn log_message(level, msg) {
    Fmt.println("[" + level + "] " + msg);
}

log_message("INFO", "Program started");
log_message("WARN", "Low memory");
log_message("ERROR", "Connection failed");

Recursive Functions

Recursion
// Factorial: n! = n * (n-1) * ... * 1
ise factorial(n) {
    ti (n <= 1) {
        padapọ 1;
    }
    padapọ n * factorial(n - 1);
}

Irosu.fo(factorial(5));  // 120 (5*4*3*2*1)
Irosu.fo(factorial(10)); // 3628800
// Factorial: n! = n * (n-1) * ... * 1
fn factorial(n) {
    if (n <= 1) {
        return 1;
    }
    return n * factorial(n - 1);
}

Fmt.println(factorial(5));  // 120
Fmt.println(factorial(10)); // 3628800

Functions as First-Class Values

First-Class Functions
// Store function in variable
ayanmo operation = ise(a, b) {
    padapọ a + b;
};

Irosu.fo(operation(3, 4));  // 7

// Pass function as argument
ise apply_twice(fn, x) {
    padapọ fn(fn(x));
}

ise double(n) {
    padapọ n * 2;
}

Irosu.fo(apply_twice(double, 5));  // 20 (5→10→20)
// Store function in variable
let operation = fn(a, b) {
    return a + b;
};

Fmt.println(operation(3, 4));  // 7

// Pass function as argument
fn apply_twice(func, x) {
    return func(func(x));
}

fn double_num(n) {
    return n * 2;
}

Fmt.println(apply_twice(double_num, 5));  // 20

Practical Example: Temperature Converter

Converter
ise celsius_to_fahrenheit(c) {
    padapọ (c * 9 / 5) + 32;
}

ise fahrenheit_to_celsius(f) {
    padapọ (f - 32) * 5 / 9;
}

Irosu.fo("0°C = " + celsius_to_fahrenheit(0) + "°F");
Irosu.fo("100°C = " + celsius_to_fahrenheit(100) + "°F");
Irosu.fo("32°F = " + fahrenheit_to_celsius(32) + "°C");
Irosu.fo("212°F = " + fahrenheit_to_celsius(212) + "°C");
fn celsius_to_fahrenheit(c) {
    return (c * 9 / 5) + 32;
}

fn fahrenheit_to_celsius(f) {
    return (f - 32) * 5 / 9;
}

Fmt.println("0C = " + celsius_to_fahrenheit(0) + "F");
Fmt.println("100C = " + celsius_to_fahrenheit(100) + "F");
Fmt.println("32F = " + fahrenheit_to_celsius(32) + "C");
Fmt.println("212F = " + fahrenheit_to_celsius(212) + "C");

What You Learned