Skip to content
Skip to the lesson
← RoadmapDay 4 of 90Foundationsstarts with 5 min · 2h 15m in all

Functions, arguments, returns, and scope

By the end of today you can say exactly which names a function can see, why a function that prints is different from one that returns, and why splitting code into functions is a design decision rather than tidiness.

YesterdayOn Day 3 you wrote logic that decides what happens next. Today you give that logic a name and a boundary.

TomorrowTomorrow you work with the collections that functions usually receive and return.

01

Why this matters

A function is the first boundary you ever draw in software. Every later boundary is the same idea at a larger scale: modules on Day 9, APIs on Day 25, services on Day 65, and tools an AI can call on Day 77.

Functions
A named boundary with inputs and an output, and the first boundary you ever draw.
Scope
Which names a piece of code can see, and the order Python searches for them.
Returning versus printing
A returned value is usable by the caller; a printed one is gone.
Why we split code
Naming, reuse, testability, and limiting how much a reader holds at once.
02

See it first

5 min

One small thing, worked through here before you go anywhere. Guess the answer before you open it.

Somebody wrote a helper to total up a basket. It works, they checked, the number appears on screen.

def basket_total(prices):
    print(sum(prices))

total = basket_total([10, 20])
print(total * 2)

The first print shows 30. What does the second line do?

Decide before you open it. Being wrong here is the point.

Show me what happens

It crashes. TypeError: unsupported operand type(s) for *: 'NoneType' and 'int'. basket_total shows you a number but never hands one back. A function with no return statement returns None, so total is None, and None times two is nothing at all. Printing and returning look the same when you run the file yourself, because both end with a number in front of you. They are opposites. Printing throws the value at a screen and forgets it. Returning gives it to whoever asked, so it can be added, stored, tested, or sent somewhere. Only one of those composes into a program.

Where you meet this at work

This is why a function that logs instead of returning cannot be unit tested: there is no value to assert on. It is also the first boundary you draw in software, and the same rule scales all the way up. An API endpoint that prints to the server log instead of responding has made exactly this mistake at a larger size.

03

Learn it

70 min

Copy this into Claude or ChatGPT. It quizzes you before it explains anything, which is deliberate. The resources under it are how you check what it told you.

Today's Master Prompt

Free · sign in

A prompt written for this day alone: your level, the exact scope, what to leave out, and an instruction to quiz you before it explains anything. Paste it into Claude or ChatGPT and it teaches you today's material.

Sign in to continueNo card, now or later.

Check it against something that is not a model

An assistant can be fluent and wrong, and on a topic you met today you will not catch it. These cover the same ground and were made by people who do this for a living, so they are what you hold the explanation up against. They are other people's work and we only link to them, so judge them for yourself.

4 hand-picked resources

Free · sign in

Videos, official docs and articles covering the same ground, each opened and annotated by hand. They are what you check the assistant against on a day you cannot yet catch it being wrong.

Sign in to continueNo card, now or later.
04

Build it

40 min

Take the free-shipping function you wrote on Day 3 and split it into three smaller functions, each doing one thing and returning a value rather than printing. Then write a function with a mutable default argument, call it three times, and explain to yourself why the output surprised you.

05

Recall it

20 min

Answer out loud, reveal, then mark honestly whether you had it. That score is the only thing on this page you do not get to choose.

4 recall questions

Free · sign in

Questions you answer from memory, then grade yourself against the real answer. The score is carried into the mastery rating below it, so an honest miss cannot quietly become a tick.

Sign in to continueNo card, now or later.
06

Rate it

Finishing a day and understanding it are two different things, and this page counts them separately.

Mastery tracking

Free · sign in

Rate yourself against five named criteria per concept. Completion and mastery are tracked separately, and anything you rate shakily comes back automatically on a spaced schedule.

Sign in to continueNo card, now or later.
07

Recap

  • 01A call creates a local namespace, and returning discards it
  • 02Returning gives the caller a value; printing gives them nothing usable
  • 03Default arguments are created once, at definition time
  • 04A function is the first boundary you draw, and every later one works the same way

Your progress

Free · sign in

Mark days complete, pick up where you left off across devices, and watch completion and mastery diverge. Free, and the account exists only so ninety days of work cannot vanish with a cleared browser.

Sign in to continueNo card, now or later.