By the end of The Repeated Chunk every group had the same complaint. The program worked, but the eight lines that converted a mark to a letter grade appeared four times, and when the cutoff for a B changed, three of the four copies got updated. The bug was not in any line of code. It was in having four copies at all.

A function is the fix: name a chunk of thinking once, then use the name.

def letter_grade(mark):
    if mark >= 80:
        return "A"
    elif mark >= 70:
        return "B"
    elif mark >= 60:
        return "C"
    elif mark >= 50:
        return "D"
    else:
        return "R"

Now the cutoff lives in exactly one place. Change it there and every part of the program that grades anything changes with it — including the parts you have not written yet.

What the parts are called

PartIn the exampleWhat it does
Definitiondef letter_grade(mark):Names the chunk
ParametermarkThe information the chunk needs
Bodythe indented linesThe thinking itself
Return valuereturn "A"The answer it hands back
Callletter_grade(78)Asking for that thinking, here, now

A call is not a copy. When Python reaches letter_grade(78) it jumps into the function, runs it with mark set to 78, and comes back holding "B". The function does not print anything and does not know who asked — which is exactly what makes it reusable.

Why “returns” beats “prints”

An early instinct is to make the function print the grade. Resist it. A function that returns can be used in ways you did not anticipate:

grade = letter_grade(78)              # store it
print(f"You earned a {letter_grade(78)}")   # show it
if letter_grade(mark) == "R":         # decide with it
    print("See me — let's make a plan.")

A function that prints can only ever do the one thing. This is the first place in the course where a small design decision quietly decides how useful your code will be to somebody else — which is the whole subject of Who Is This For.

Where this goes next

Functions are the unit you will design in when the problem gets bigger than one screen — see Decomposition and Design, where a whole program becomes a handful of well-named chunks. Every program you build from here on, including The Community App, is easier to finish and far easier to hand to somebody else when the thinking has names. Practise the mechanics in Functions Practice, then read somebody else’s in Writing Functions.

Curriculum connection

A3.1

demonstrate the ability to use existing subprograms (e.g., random number generator, substring, absolute value) within computer programs;

Link to original

A3.2

write subprograms (e.g., functions, procedures) that use parameter passing and appropriate variable scope (e.g., local, global), to perform tasks within programs.

Link to original

B2.3

apply the principle of modularity to design reusable code (e.g., subprograms, classes) in computer programs;

Link to original