The main Entry Point
Why scripts start at the main function.
The main Entry Point is a free Mojo Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Mojo Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Every Program Needs a Start
When you run a Mojo file, the program needs to know where to begin. That starting point is a special function named main. 🚀
Mojo Looks for main
The toolchain searches your file for a function called main and calls it for you. You never call main yourself.
def main():
print("Program started")The Entry Point Idea
An entry point is simply where execution enters your code. In Mojo that door is always main, so your logic goes inside it.
Code Outside main
Top-level definitions like functions and structs can live outside main, but the work that actually runs starts from main.
fn greet():
print("Hi")
def main():
greet()main Takes No Arguments
A basic main needs no parameters. The empty parentheses after the name show it accepts nothing when Mojo calls it.
def main():
print("No args needed")One main Per Program
A program has exactly one main. Two would confuse Mojo about where to begin, so the compiler expects a single clear entry point.
def or fn for main
You can define main with def or fn. Both work as the entry point; def is the friendlier, Python-like choice when starting out.
fn main():
print("Strict style")Order Inside main
Statements inside main run top to bottom, in order. The first line executes first, then the next, building your program step by step.
def main():
print("first")
print("second")Calling Helpers from main
main is often short: it just calls other functions. This keeps your entry point a clean summary of what the program does.
def main():
setup()
run()Forgetting main
If a file has no main, running it as a program fails because Mojo cannot find where to start. Always include one for runnable files.
main Ends the Program
When the last line of main finishes, the program is done and control returns to your shell. No extra cleanup is required.
Quick Check
You wrote a Mojo file with several functions. Where does execution actually begin?
Recap
Every runnable Mojo file needs one main function. Mojo calls it to start, runs its lines in order, and ends when main finishes. 🎯
Frequently asked questions
Is the “The main Entry Point” lesson free?
Yes — the full text of “The main Entry Point” is free to read here on the web, and the Mojo Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Mojo Academy course, upgrade to CoddyKit PRO.
What will I learn in “The main Entry Point”?
Why scripts start at the main function. You practise Mojo Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Mojo Academy?
No prior experience is required. Mojo Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “The main Entry Point” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Mojo Academy lesson?
Yes. Every Mojo Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Your First .mojo File
- The main Entry Point
- Running with mojo run
- Exploring in the Mojo REPL