Same function, different results, because the argument changes what name holds each time.
5 / 14
Two parameters
def describe_pet(name, animal):
print(name + " is a " + animal + ".")
describe_pet("Biscuit", "dog")
describe_pet("Whiskers", "cat")
Biscuit is a dog.
Whiskers is a cat.
Read calls left to right, carefully: mix up the order and you get a wrong sentence.
6 / 14
Printing versus returning
So far every function has printed its result — but often you want a function to hand a value back
return does that: the caller can store the value or print it directly
Contrast: once a printing function prints, the value is gone — displayed, but unusable
Rule of thumb: print when a human needs to see something; return when another part of the program needs to use it
Code after a return inside a function never runs — returning ends the function at once
7 / 14
Return a value
def add(a, b):
return a + b
total = add(4, 3)
print(total)
print(add(10, 20))
7
30
Return means “the answer of this function is a + b.” Most real functions return.
8 / 14
Why functions matter
Functions exist for the same reason loops do: write once, use many times
But they add something loops cannot — a name for an idea
Greeting becomes one idea, done three times, instead of three repeated lines
When the greeting needs to change, you change it in one place, not three
In big programs, that single-change property is what keeps code manageable