Skip to content
ComputerCS
Start learning

Python Programming · Lesson 2.5.1.1

Defining & Calling Functions

Wrap any block of code into a named, reusable function with def — call it a hundred times, change it in one place.

01

Learning objectives

  • Define a function with def, a name, parameters and an indented body.
  • Call a function several times with different arguments and trace the output.
  • Explain why functions reduce repetition and make testing easier.
  • Write a one-line docstring that states what a function returns.
  • State what a function without a return statement hands back (None).
02

Lesson notes

Copy-paste is the beginner's tool for repetition, and it fails the moment a fix is needed in five copied blocks. A function fixes this: name a block of code once with def, then run it anywhere by writing its name — a call. The recipe analogy is exact: the recipe is written once, the dish is cooked daily. Functions are also the unit of testing: one function, one clear input, one checkable output — which is exactly how examiners mark programming answers.

Anatomy: the header def greet(name): has the keyword def, a descriptive name, parameters in brackets, and a colon; the body is indented underneath. Crucially, defining runs nothing — Python only records the recipe. The body executes at each call, with the call's arguments filling the parameters. A call before the definition raises NameError, because Python reads the file from top to bottom and the name simply does not exist yet.

Docstrings document intent: a triple-quoted first line such as '''Return the area of a rectangle.''' states what the function does and returns. The help() function and most editors read them, and examiners award marks for them in long questions. Write them from day one — what does this return? is the first question any reader, including future you, asks of a function.

A function that finishes without hitting return hands back the special value None. This surprises beginners who print() inside a function and then wonder why the result cannot be reused: printing shows a value on screen, while returning delivers it to the caller. The full treatment comes next topic — for now, just remember that result = some_function() stores None unless the function explicitly returns something.

Board tip: a standard question asks you to define a function such as cal_grade(marks) and then show two sample calls with their output. Structure the answer: the def line with its parameter, a docstring, the body with the logic and a return, then the calls. Naming matters too — lowercase with underscores (total_price) matches the style used throughout the PECTAA textbook examples.

A main program calling a function and returning to the main flow.
Function call and return
03

Worked code examples

Define once, call many times

The def line only registers the recipe; each call runs the body with its own argument, so one definition serves any number of names.

def greet(name):
    """Return a friendly greeting for the given name."""
    return "Hello, " + name + "!"

print(greet("Ayesha"))
print(greet("Bilal"))
Output
Hello, Ayesha!
Hello, Bilal!

Functions remove duplication

The same tested code is reused for two different rooms: 15 + 16 gives 31. Fixing a bug would now need one edit, not two.

def area_of_rectangle(w, h):
    """Return the area of a w-by-h rectangle."""
    return w * h

total = area_of_rectangle(5, 3) + area_of_rectangle(4, 4)
print(total)
Output
31

No return means None

show_total prints 12 while running, but returns nothing, so Python supplies None — printing inside a function is not the same as returning.

def show_total(nums):
    print(sum(nums))

result = show_total([2, 4, 6])
print(result)
Output
12
None
04

Key terminology

function

A named, reusable block of code, defined with def and run by calling its name.

parameter

A placeholder variable in the def line that receives a value at call time.

argument

The actual value passed into a function when it is called.

docstring

A string on the first line of a function body that documents what it does.

None

The value Python returns when a function has no return statement.

05

Common misconceptions

Common mistake

Defining a function immediately runs its body.

Correct understanding

def only records the recipe. The body runs each time the function is called, and never before.

Common mistake

A function that prints a result also returns it.

Correct understanding

print only displays on screen. Without a return statement the function delivers None to the caller.

Common mistake

A function can be called before its def line as long as it is defined somewhere in the file.

Correct understanding

Python executes top to bottom; calling before definition raises NameError because the name does not exist yet.

06

Check your understanding

1. def f(x): return x * 2 — what does print(f(3) + f(4)) show?

2. A program's very first line is print(greet('Ali')) and greet is defined at the bottom of the file. What happens?

3. Which line correctly documents what a function does?

NERDLYY · FEEDBACK

Help shape Nerdlyy

Your feedback goes directly to the team. We read every submission.

No reply needed — you can send feedback without sharing contact details.