Mojo First Program
Writing your first Mojo program teaches you the basic building blocks that every Mojo file shares. You will learn how a Mojo file is structured, how to display output, and how to add comments that explain your code.
The Structure of Every Mojo Program
A Mojo program starts from a special entry point called main. Think of main as the front door of your house. Every visitor (the computer) enters through the front door first, regardless of how many rooms exist inside.
Mojo Program
│
▼
┌────────┐
│ main │ ← Computer starts here
└────────┘
│
▼
(your code runs in order, top to bottom)
Here is the simplest complete Mojo program:
fn main():
print("Hello, World!")
Save this as hello.mojo and run it:
magic run mojo hello.mojo
Output:
Hello, World!
Breaking Down the Code
The fn Keyword
The word fn tells Mojo you are defining a function — a named block of instructions. Mojo uses fn instead of Python's def when you want the stricter, faster version of a function. You will also see def in Mojo for Python-compatible functions, but fn is the Mojo-native way.
The main Function Name
The name main is special. Mojo looks for a function named main and runs it first. Every standalone Mojo program must have exactly one main function.
The Colon and Indentation
The colon at the end of fn main(): signals the start of a block. Everything indented by four spaces (or one tab) beneath it belongs to that block. Mojo enforces indentation the same way Python does — it is not optional formatting, it is part of the language grammar.
The print Function
The built-in print function sends text to your terminal screen. You pass text inside double quotes. Mojo calls these double-quoted pieces of text string literals.
Printing Different Types of Data
The print function accepts more than just text. It handles numbers and multiple values in one call.
fn main():
print("My name is Mojo")
print(42)
print(3.14)
print("Score:", 100)
print("Sum:", 10 + 5)
Output:
My name is Mojo 42 3.14 Score: 100 Sum: 15
When you pass multiple values separated by commas, print inserts a space between them automatically.
Printing on the Same Line
By default, print adds a newline character at the end, moving the cursor to the next line. Use the end parameter to change this behavior.
fn main():
print("Loading", end="")
print("...")
Output:
Loading...
Setting end="" replaces the default newline with an empty string, so the next print continues on the same line.
Adding Comments
Comments are notes you leave in your code for yourself and other readers. The computer ignores everything after a # symbol on the same line.
fn main():
# This line prints a greeting
print("Hello, Mojo!") # Inline comment works too
# The next line adds two numbers
print(7 + 3)
Comments serve as a map inside your code. They explain the why, not just the what. A good comment says "We add 7 + 3 because the formula requires two base offsets" rather than "We add 7 + 3."
Multi-Line Programs in Practice
Real programs do more than one thing. Mojo executes your statements from top to bottom, one line at a time.
fn main():
# Step 1: Greet the user
print("Welcome to Mojo!")
# Step 2: Show some math
print("2 to the power of 10 =", 2 ** 10)
# Step 3: Show a final message
print("Ready to learn more.")
Output:
Welcome to Mojo! 2 to the power of 10 = 1024 Ready to learn more.
Diagram: How Mojo Executes Your First Program
You run: magic run mojo hello.mojo
│
▼
┌─────────────────────┐
│ Mojo Compiler │
│ Reads hello.mojo │
│ Compiles to native │
│ machine code │
└─────────┬───────────┘
│
▼
┌─────────────────────┐
│ CPU Executes │
│ main() function │
│ line by line │
└─────────┬───────────┘
│
▼
Terminal shows output
This diagram shows that your code goes through the compiler before running. The compiler translates your readable Mojo source into machine code that your CPU understands directly. This compilation step is what makes Mojo programs much faster than Python programs.
Common Beginner Mistakes
Forgetting the Colon
Writing fn main() without the trailing colon causes a syntax error. Mojo expects the colon to mark the start of the function body.
Incorrect Indentation
Placing print at the same column as fn main (no indentation) also causes an error. The code inside main must be indented.
Mismatched Quotes
Opening a string with a double quote and closing it with a single quote, like print("hello'), causes a syntax error. Always match your quotes.
Key Takeaways
Every Mojo program needs a fn main(): entry point. The print function displays output to the terminal. Comments start with # and help readers understand the code. Mojo compiles your source before running it, which is why Mojo programs run so much faster than equivalent Python programs.
