← VCE Software Development U3O1

Program structure with a main() function

Defines a function called main that holds the main steps of your program. You call main() once at the end of the file, after the other functions are defined, to start the program.

Understanding Program structure with a main() function in VCE Software Development U3O1

What it does: greet is defined before main, so main can call it. Functions above main are ready to use. The call main() sits at the bottom with no indent. It runs the function once and starts the program. A def line must end with a colon. Without the colon you get a syntax error. The main function holds the main steps, such as asking for input, calling other functions and printing results. Defining main() only saves the steps. You must call main() to run them. The second option never calls it, so nothing prints. Indentation shows which lines are inside the function. Lines that are not indented are outside main.

Common mistakes: The first line of a function definition must end with a colon. It should be def main():. Python is case-sensitive, so Print is not the same as print. The step inside main() should be print('Start').

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 72–96) and word search (Modules 72–113).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Where should you call main() in a simple Python program?
At the end of the file, after the functions are definedAt the very top, before any functions are defined
Matches
def main(): print("Start")
↓
main() is never called, so the program does nothing. Add main() at the end of the file with no indent.
Applying
Meanings
Term
def main():
Meaning
Defines a function called main that holds the main steps of your program. You call main() once at the end of the file, after the other functions are defined, to start the program.
Practice
def greet(name):
Testing
Gaps
main():
Check Answer
Spots
1def main()
2 name = input('Name? ')

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Sand Cat' badge — Precision Prowler, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Where should you call main() in a simple Python program?
A. At the very top, before any functions are defined
B. At the end of the file, after the functions are defined ✓
Python reads from top to bottom. If main() is called before the functions it uses are defined, you get a NameError. Call main() last.
2. Which line correctly defines a main function?
A. def main()
B. def main(): ✓
A def line must end with a colon. Without the colon you get a syntax error.
3. What does main() hold in a well-structured program?
A. The main steps of the program ✓
B. Only the comments for the program
The main function holds the main steps, such as asking for input, calling other functions and printing results.
4. Which code actually starts the program?
A. def main(): print("Hi") main() ✓
B. def main(): print("Hi")
Defining main() only saves the steps. You must call main() to run them. The second option never calls it, so nothing prints.
5. True or false: you should call main() once at the end of the file.
A. True ✓
B. False
You call main() once to start the program. Calling it again would run all the steps again.

Match the Pairs

def main(): → Defines the main function that holds the main steps of the program
main() → Calls the main function once at the end of the file to start the program
Helper functions are defined above main, so they already exist when main runs → def greet(): print("Hi!") def main(): greet()
The program starts by calling main() once at the very end of the file, with no indent → def main(): print("Start") main()
def main(): print("Start") → main() is never called, so the program does nothing. Add main() at the end of the file with no indent.
main() def greet(): print("Hi!") def main(): greet() → main() is called before it is defined, which causes a NameError. Move main() to the end of the file, after all the functions.

Key Term

def main():
Defines a function called main that holds the main steps of your program. You call main() once at the end of the file, after the other functions are defined, to start the program.

Fill in the Blanks

1. def main(): print("Let's go!") main()
Hint: Type the keyword that starts a function definition
2. def main(): print("Start") main()
Hint: Type the name of the function that the last line calls to start the program
3. def main(): print("Game on") main()
Hint: Type the symbol that ends a function definition line
4. def main(): print("Hello") main()
Hint: Type the built-in function that shows text on the screen from inside main
5. def main(): print("Welcome") main()
Hint: Type the name of the function that holds the main steps of the program

Spot the Error

Question 1
A def main()
B name = input('Name? ')
C print('Hi', name)
D main()
Line A — The first line of a function definition must end with a colon. It should be def main():
Question 2
A def main():
B Print('Start')
C print('End')
D main()
Line B — Python is case-sensitive, so Print is not the same as print. The step inside main() should be print('Start')
Question 3
A import random
B define main():
C n = random.randint(1, 6)
D print(n)
E main()
Line B — Python uses the keyword def to create a function, not define. It should be def main():
Question 4
A def main():
B x = 5
C print(y)
D main()
Line C — The variable y was never created inside main(). The program made x, so it should be print(x)
Question 5
A def hello():
B print('Hello')
C def main():
D hello
E main()
Line D — Without brackets, hello only names the function and does not run it. A function call needs brackets: hello()

Student with a class code? Join your class →