← Back to course
Code Crew G6+ / Week 06 / Monday
1/6
Week 06 Β· Functions and modules

Monday

Name a job with def
// Name a job once, use it everywhere
⏱ about 30 min

Monday: Name a job with def

Rocket's morning report program has grown to ninety lines. The same three banner lines appear in five different places, and today he needs to change the banner title.

"Five places," he groans. "I will miss one. I always miss one."

Raven scrolls through the program and marks each copy with her finger. "What do you notice?" she asks. "It is the same job every time. Draw the banner."

Nova projects a small labeled box around the three lines. "Would you like a hint?" she asks. "Give that job a name. Write it once. Then call it by name wherever you need it."

Rocket types def banner(): and feels the program getting lighter.

Defining and calling a function

def starts a function definition. Then comes the name, a pair of round brackets, and a colon. The indented lines below are the function body.

Defining a function does not run it. The body runs only when you call the function by writing its name with brackets, like banner().

# banner_fn.py
def banner():
    print("*" * 20)
    print("  MISSION CONTROL")
    print("*" * 20)

banner()
print("Morning briefing")
banner()
Predict: how many lines does banner_fn.py show in total?
WHY THIS EXERCISEbanner() is called twice, giving 6 lines, plus the Morning briefing line makes 7.
********************
  MISSION CONTROL
********************
Morning briefing
********************
  MISSION CONTROL
********************

Define before you call

Python reads from the top. If you call a function on a line above its def, Python has not met it yet, and you get a NameError.

# too_early.py
banner()

def banner():
    print("*" * 20)
Traceback (most recent call last):
  File "too_early.py", line 2, in <module>
    banner()
    ^^^^^^
NameError: name 'banner' is not defined
FUNCTIONS
  • Read the question.
  • Tap your answer.
You change "MISSION CONTROL" to "LAUNCH DAY" inside banner(). How many lines of code do you edit?
What does def banner(): do on its own?
At your computer
1. Type banner_fn.py in a new file, save it and run it.
2. Change the title inside banner() to LAUNCH DAY. Run it and see both banners change.
3. Add a third call to banner() at the end and run it.
4. Move one banner() call above the def line. Run it and read the NameError. Then move it back.

Practice: functions that call functions

A function body can call another function. Big jobs are often built from small ones this way, like a report that uses a divider line.

When a call finishes, Python goes back to the line just after the call and carries on.

# report_fn.py
def line():
    print("-" * 16)

def report():
    line()
    print("Fuel: OK")
    print("Crew: OK")
    line()

report()
print("Next shift")
report()
TRACE REPORT_FN.PY
  • Read the question.
  • Tap your answer.
How many lines does one call to report() show?
How many lines does the whole program show?
You change the 16 to 10 inside line(). How many divider lines change?
How many times does line() run in the whole program?
What is the fifth line of output?
How many lines would three calls to report() show?
StatementTrue or false?
A function can call another function.?
Defining a function runs its body straight away.?
After a function finishes, Python goes back to the line after the call.?
A function must be called exactly once.?
WHY THIS EXERCISEdef names the job, each call runs it, and Python returns to the caller when the body ends.

A function turns a repeated job into one clear name. Tomorrow you will pass values into functions.