Skip to content

Basic Examples

These examples demonstrate the fundamental features of Raven programming language.

Hello World

The classic first program:

fun main() -> void {
    print("Hello, World!");
}

main();

Variables and Types

fun main() -> void {
    let name: string = "Alice";
    let age: int = 25;
    let height: float = 5.9;
    let isActive: bool = true;

    print(format("Name: {}, Age: {}, Height: {}, Active: {}", 
                 name, age, height, isActive));
}

main();

Arrays

fun main() -> void {
    let numbers: int[] = [1, 2, 3, 4, 5];

    // Print array
    print(numbers);

    // Add element
    numbers.push(6);
    print(numbers);

    // Remove last element
    let last: int = numbers.pop();
    print(format("Removed: {}", last));
    print(numbers);

    // Get slice
    let slice: int[] = numbers.slice(1, 3);
    print(slice);
}

main();

Control Flow

If Statements

fun main() -> void {
    let age: int = 18;

    if (age < 18) {
        print("Too young");
    } elseif (age < 65) {
        print("Working age");
    } else {
        print("Retirement age");
    }
}

main();

While Loops

fun main() -> void {
    let i: int = 0;

    while (i < 5) {
        print(format("Count: {}", i));
        i = i + 1;
    }
}

main();

For Loops

fun main() -> void {
    let numbers: int[] = [10, 20, 30, 40, 50];

    for (let i: int = 0; i < len(numbers); i = i + 1) {
        print(format("Index {}: {}", i, numbers[i]));
    }
}

main();

Functions

Simple Functions

fun greet(name: string) -> void {
    print(format("Hello, {}!", name));
}

fun add(a: int, b: int) -> int {
    return a + b;
}

fun main() -> void {
    greet("Raven");
    let result: int = add(5, 3);
    print(format("5 + 3 = {}", result));
}

main();

Recursive Functions

fun factorial(n: int) -> int {
    if (n <= 1) {
        return 1;
    }
    return n * factorial(n - 1);
}

fun main() -> void {
    let result: int = factorial(5);
    print(format("5! = {}", result));
}

main();

Structs

struct Person {
    name: string,
    age: int,
    isActive: bool
}

fun main() -> void {
    let person: Person = Person { 
        name: "Alice", 
        age: 25, 
        isActive: true 
    };

    print(format("Name: {}", person.name));
    print(format("Age: {}", person.age));
    print(format("Active: {}", person.isActive));

    // Modify fields
    person.age = 26;
    print(format("New age: {}", person.age));
}

main();

Struct Methods (OOP)

struct Person {
    name: string,
    age: int
}

impl Person {
    fun greet(self) -> string {
        return format("Hello, I'm {} and I'm {} years old", self.name, self.age);
    }

    fun have_birthday(self) -> void {
        self.age = self.age + 1;
    }
}

fun main() -> void {
    let p: Person = Person { name: "Alice", age: 30 };
    print(p.greet());
    p.have_birthday();
    print(p.greet());
}

main();

Enums

enum HttpStatus {
    OK,
    NotFound,
    InternalError,
    BadRequest
}

fun main() -> void {
    let status: HttpStatus = HttpStatus::OK;

    print(format("Status: {}", status));

    // string to enum conversion
    let jsonStatus: string = "NotFound";
    let parsedStatus: HttpStatus = enum_from_string("HttpStatus", jsonStatus);
    print(format("Parsed: {}", parsedStatus));
}

main();

File Operations

fun main() -> void {
    let filename: string = "test.txt";
    let content: string = "Hello from Raven!";

    // Write file
    write_file(filename, content);
    print("File written");

    // Check if file exists
    if (file_exists(filename)) {
        print("File exists");

        // Read file
        let data: string = read_file(filename);
        print(format("Content: {}", data));
    } else {
        print("File not found");
    }
}

main();

User Input

fun main() -> void {
    let name: string = input("Enter your name: ");
    let ageStr: string = input("Enter your age: ");

    print(format("Hello, {}!", name));
    print(format("You are {} years old", ageStr));
}

main();

Error Handling

fun divide(a: float, b: float) -> float {
    if (b == 0.0) {
        print("Error: Division by zero");
        return 0.0;
    }
    return a / b;
}

fun main() -> void {
    let result1: float = divide(10.0, 2.0);
    print(format("10 / 2 = {}", result1));

    let result2: float = divide(10.0, 0.0);
    print(format("10 / 0 = {}", result2));
}

main();

Complete Example: Calculator

fun add(a: float, b: float) -> float {
    return a + b;
}

fun subtract(a: float, b: float) -> float {
    return a - b;
}

fun multiply(a: float, b: float) -> float {
    return a * b;
}

fun divide(a: float, b: float) -> float {
    if (b == 0.0) {
        print("Error: Division by zero");
        return 0.0;
    }
    return a / b;
}

fun main() -> void {
    print("Simple Calculator");
    print("1. Add");
    print("2. Subtract");
    print("3. Multiply");
    print("4. Divide");

    let choice: string = input("Enter choice (1-4): ");
    let aStr: string = input("Enter first number: ");
    let bStr: string = input("Enter second number: ");

    // Note: In a real implementation, you'd convert strings to numbers
    print(format("Choice: {}, A: {}, B: {}", choice, aStr, bStr));
}

main();

Next: Check out more examples in our GitHub repository