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.
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.
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.
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.
Two nearly identical procedures — one returns, one prints. Watch
what happens when you try to use the result:
def double(n): return n * 2 x = double(5) # x is 10 print(x * 10) # 100 — works!
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.
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.
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.
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.
RETURN
also ends the procedure immediately.return — “nothing.”
Trying to use it in math causes an error.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.
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.