Getting StartedYour First Program

Your First JOEL Program

Let’s write a complete JOEL program step by step.

Step 1: Create a File

Create a new file called calculator.joel:

[Interpreted]

module calculator

Step 2: Write Functions

Add a function to add two numbers:

[Interpreted]

module calculator

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

Step 3: Add Main Function

Add a main function to run the program:

[Interpreted]

module calculator

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

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

fn main() {
  let x = 10
  let y = 5
  
  print("x =", x)
  print("y =", y)
  print("x + y =", add(x, y))
  print("x - y =", subtract(x, y))
}

main()

Step 4: Run the Program

joel run calculator.joel

Expected output:

🚀 JOEL Runtime - Mode: Interpreted

📦 Module: calculator
x = 10
y = 5
x + y = 15
x - y = 5

Understanding the Code

[Interpreted]

This tells JOEL to run in interpreted mode. Use [Compiled] for compiled mode.

Module Declaration

module calculator

Declares the module name (optional but recommended).

Function Definition

fn add(a: i32, b: i32) -> i32 {
  return a + b
}
  • fn - Function keyword
  • add - Function name
  • (a: i32, b: i32) - Parameters with types
  • -> i32 - Return type
  • { ... } - Function body

Variables

let x = 10
let y = 5

Creates variables x and y with values 10 and 5.

Function Calls

add(x, y)

Calls the add function with arguments x and y.

Main Function

main()

Calls the main function to start program execution.

Next Steps

Try modifying the program:

  1. Add multiplication and division functions
  2. Add input validation
  3. Create a loop to calculate multiple operations
  4. Add error handling

See Examples for more ideas!