Unit 3 · Programming 2 · Lesson 3.13

return vs print

They look almost the same and do something completely different. return hands a value back to your program; print just shows it on the screen. Confusing the two wrecks more Create PTs than anything else — so it gets its own lesson.

Big Idea 3 — Algorithms & ProgrammingThe #1 procedure confusion

Before this lesson — read on Runestone

Watch the difference between a procedure that returns and one that only prints. Enrolled in our Runestone course? Open it from there so your progress counts.

1

Where We Are

People→ Bits→ Programs→ Internet→ Security→ Big Data→ Impact

Still in Programs. You can write procedures (3.12). This lesson is small but critical: what your procedure does with its answer. Get it wrong and your procedures can’t be combined — which is exactly what the Create PT needs them to do.

2

The Hook

One tells you the answer. One writes it on the board and walks away. Ask a friend to double 5. If they tell you “10,” you can use it — add it, store it, double it again. If they just write “10” on a whiteboard and leave, you saw it, but your program has nothing in hand.

return is telling you the answer — the value comes back into the program. print is writing it on the board — you see it, but the program keeps nothing. That difference is everything.

3

The Idea

Two nearly identical procedures — one returns, one prints. Watch what happens when you try to use the result:

Python — return (usable)

def double(n):
    return n * 2

x = double(5)   # x is 10
print(x * 10)  # 100 — works!

Python — print (a dead end)

def double(n):
    print(n * 2)

x = double(5)   # shows 10, but x is None
print(x * 10)  # ERROR

Hover or tap a line to light its twin. Left side: return puts 10 into x, so x * 10 is 100. Right side: print shows 10 on screen but hands back nothing (None), so x is empty and x * 10 crashes. Same-looking code, opposite usefulness.

Rule of thumb: if another part of your program needs to use the value — store it, do more math, pass it on — you need return. Use print only when a value’s whole job is to be seen by a human. A procedure can return a value and let the caller decide whether to print it.
In pseudocode, RETURN also ends the procedure immediately. The moment RETURN (expr) runs, the procedure stops and hands back that value — even if it’s inside an IF. That’s an “early return,” and it’s a clean way to handle a special case first: IF (n < 0) { RETURN ("invalid") } and the rest never runs for a negative n.
4

Try It — Return or Print?

Same procedure body, two versions. Call each, then try to use the result by multiplying it by 10. One works; one is a dead end. See why.

n =
press a button to run x = double(n) then print(x * 10)
5

Vocabulary

return Programs
Send a value back to the caller so the program can use it. In pseudocode, RETURN also ends the procedure immediately.
print / DISPLAY Programs
Show a value on the screen for a human. It does not hand the value back to the program.
None Programs
What a procedure hands back when it has no return — “nothing.” Trying to use it in math causes an error.
6

Check

Five “what is displayed / what is returned” questions — the exact MCQ pair the exam loves. Read carefully whether the value is used or just shown. Then pick your answer.

0 of 5 answered
7

Impact Check

Why this tiny word decides whether code can be built on

Software is procedures feeding other procedures: one computes a price, another applies a discount to it, a third adds tax. That chain only works if each step returns its result so the next can use it. A step that merely prints breaks the chain — the value reached your eyes but not the next procedure.

On the Create PT, students lose points every year by writing a procedure that prints when it needed to return, so nothing else can build on it. Getting this right is what lets your program be more than a pile of separate scripts — it’s what makes it a system.

8

Connections

Came from 3.12 — you can write procedures; this is the choice that makes them reusable or not.
Next in 3.14 — a procedure that takes a list and returns a computed result — the Create PT shape.
Returns in 3.15 — refactoring the gradebook only works because each helper returns its statistic to the main program.
Returns on the exam — the “what is displayed vs what is returned” MCQ pair is guaranteed. Track where the value goes.
← 3.12 Writing Procedures