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 calculatorStep 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.joelExpected output:
🚀 JOEL Runtime - Mode: Interpreted
📦 Module: calculator
x = 10
y = 5
x + y = 15
x - y = 5Understanding the Code
Header
[Interpreted]This tells JOEL to run in interpreted mode. Use [Compiled] for compiled mode.
Module Declaration
module calculatorDeclares the module name (optional but recommended).
Function Definition
fn add(a: i32, b: i32) -> i32 {
return a + b
}fn- Function keywordadd- Function name(a: i32, b: i32)- Parameters with types-> i32- Return type{ ... }- Function body
Variables
let x = 10
let y = 5Creates 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:
- Add multiplication and division functions
- Add input validation
- Create a loop to calculate multiple operations
- Add error handling
See Examples for more ideas!