Functions

Functions are the building blocks of JOEL programs.

Function Declaration

Basic Function

fn greet() {
  print("Hello")
}

Function with Parameters

fn greet(name: str) {
  print("Hello,", name)
}

Function with Return Type

fn add(a: i32, b: i32) -> i32 {
  return a + b
}

Function with Type Annotations

fn calculate(x: i32, y: f64) -> f64 {
  return x as f64 + y
}

Function Calls

greet()                    # No arguments
greet("JOEL")             # One argument
let result = add(5, 3)    # With return value

Multiple Parameters

fn create_user(name: str, age: i32, active: bool) {
  print("User:", name, age, active)
}

create_user("Alice", 25, true)

Return Values

Explicit Return

fn multiply(a: i32, b: i32) -> i32 {
  return a * b
}

Implicit Return

fn add(a: i32, b: i32) -> i32 {
  a + b  # Last expression is returned
}

No Return Value

fn print_hello() {
  print("Hello")
  # Returns None implicitly
}

Higher-Order Functions

Functions as values (coming soon):

fn apply(func: Function, x: i32) -> i32 {
  return func(x)
}

Recursion

fn factorial(n: i32) -> i32 {
  if n <= 1 {
    return 1
  }
  return n * factorial(n - 1)
}

Default Parameters

# Coming soon
fn greet(name: str = "World") {
  print("Hello,", name)
}

Variadic Functions

# Coming soon
fn sum(...numbers: list[i32]) -> i32 {
  # Sum all numbers
}

Function Overloading

# Coming soon - multiple functions with same name, different signatures
fn add(a: i32, b: i32) -> i32 { a + b }
fn add(a: f64, b: f64) -> f64 { a + b }

Closures

# Coming soon
let add = |x, y| x + y
let result = add(5, 3)

Examples

Calculator Functions

fn add(a: i32, b: i32) -> i32 {
  return a + b
}

fn subtract(a: i32, b: i32) -> i32 {
  return a - b
}

fn multiply(a: i32, b: i32) -> i32 {
  return a * b
}

fn divide(a: i32, b: i32) -> f64 {
  if b == 0 {
    return 0.0
  }
  return a as f64 / b as f64
}

String Utilities

fn capitalize(text: str) -> str {
  # Implementation coming soon
  return text
}

fn reverse(text: str) -> str {
  # Implementation coming soon
  return text
}

Best Practices

  1. Use descriptive names: calculate_total not calc
  2. Keep functions small: Single responsibility
  3. Use type annotations: Especially in compiled mode
  4. Document complex functions: Add comments for clarity

Next Steps