Functions & Returns
Define your own functions with parameters and return values.
OK so yesterday we wrote a function that just returns True. Cute. But how do I make a function that actually takes input? Like, if I wanted a greeting that works for any name, not just a hardcoded one.
You give your function parameters — names that go inside the parentheses. When someone calls your function, they pass in values that fill those names. Think of it like a template with a blank you fill in each time.
def greet(name):
return "Hello, " + name + "!"
result = greet("Alice")
print(result) # Hello, Alice!
Here, name is a parameter. When you call greet("Alice"), Python sets name = "Alice" inside the function, runs the body, and hands back the result. Call it again with greet("Bob") and you get "Hello, Bob!".
That's like mail merge! I do this in Google Docs all the time — "Dear {FirstName}" and it fills in from a list. OK, can a function take more than one input?
Absolutely. Just separate the parameters with commas:
def add(a, b):
return a + b
print(add(3, 5)) # 8
print(add(10, 20)) # 30
The order matters. The first value you pass goes to the first parameter, second to second, and so on. If your function expects two arguments and you only give it one, Python raises an error — it won't guess.
Makes sense. So what happens if I forget the return? Like, I write the calculation but don't send it back?
This is actually one of the most common bugs beginners hit. The function runs its code, gets the answer... and then throws it away. Python gives back None — its way of saying "nothing."
def multiply(a, b):
a * b # calculates 20, but doesn't return it!
result = multiply(4, 5)
print(result) # None — not 20!
The math happens. Python just discards the answer because you didn't say return. It's like doing a calculation on a sticky note and then tossing the note in the trash instead of handing it to your boss.
Ugh, I can already see myself making that mistake. OK, what if my function needs to do a few steps before it returns? Like building up a message from multiple pieces?
A function body can be as many lines as you need. return just has to come wherever you want the function to stop and hand back a value:
def describe(name, age):
greeting = "My name is " + name
info = "I am " + str(age) + " years old"
return greeting + ". " + info
print(describe("Zuzu", 3))
# My name is Zuzu. I am 3 years old
Notice str(age) — we had to convert the number 3 to the string "3" before joining it with +. Python won't let you add a number directly to a string. It's being strict on purpose. Tomorrow we'll learn f-strings, which make this whole process much cleaner.
So just to make sure I've got it: def creates the function, the stuff in parentheses are the inputs, and return sends back the output. That's it?
That's the anatomy of every function you'll ever write:
def function_name(parameters):— declares what the function is called and what it needs- Body — the indented lines that do the actual work
return value— sends the result back to whoever called it
Instead of copying and pasting the same formula across fifty cells in your spreadsheet, you write it once inside a function and call it whenever you need it. Let's write one now.
Practice your skills
Sign up to write and run code in this lesson.
Functions & Returns
Define your own functions with parameters and return values.
OK so yesterday we wrote a function that just returns True. Cute. But how do I make a function that actually takes input? Like, if I wanted a greeting that works for any name, not just a hardcoded one.
You give your function parameters — names that go inside the parentheses. When someone calls your function, they pass in values that fill those names. Think of it like a template with a blank you fill in each time.
def greet(name):
return "Hello, " + name + "!"
result = greet("Alice")
print(result) # Hello, Alice!
Here, name is a parameter. When you call greet("Alice"), Python sets name = "Alice" inside the function, runs the body, and hands back the result. Call it again with greet("Bob") and you get "Hello, Bob!".
That's like mail merge! I do this in Google Docs all the time — "Dear {FirstName}" and it fills in from a list. OK, can a function take more than one input?
Absolutely. Just separate the parameters with commas:
def add(a, b):
return a + b
print(add(3, 5)) # 8
print(add(10, 20)) # 30
The order matters. The first value you pass goes to the first parameter, second to second, and so on. If your function expects two arguments and you only give it one, Python raises an error — it won't guess.
Makes sense. So what happens if I forget the return? Like, I write the calculation but don't send it back?
This is actually one of the most common bugs beginners hit. The function runs its code, gets the answer... and then throws it away. Python gives back None — its way of saying "nothing."
def multiply(a, b):
a * b # calculates 20, but doesn't return it!
result = multiply(4, 5)
print(result) # None — not 20!
The math happens. Python just discards the answer because you didn't say return. It's like doing a calculation on a sticky note and then tossing the note in the trash instead of handing it to your boss.
Ugh, I can already see myself making that mistake. OK, what if my function needs to do a few steps before it returns? Like building up a message from multiple pieces?
A function body can be as many lines as you need. return just has to come wherever you want the function to stop and hand back a value:
def describe(name, age):
greeting = "My name is " + name
info = "I am " + str(age) + " years old"
return greeting + ". " + info
print(describe("Zuzu", 3))
# My name is Zuzu. I am 3 years old
Notice str(age) — we had to convert the number 3 to the string "3" before joining it with +. Python won't let you add a number directly to a string. It's being strict on purpose. Tomorrow we'll learn f-strings, which make this whole process much cleaner.
So just to make sure I've got it: def creates the function, the stuff in parentheses are the inputs, and return sends back the output. That's it?
That's the anatomy of every function you'll ever write:
def function_name(parameters):— declares what the function is called and what it needs- Body — the indented lines that do the actual work
return value— sends the result back to whoever called it
Instead of copying and pasting the same formula across fifty cells in your spreadsheet, you write it once inside a function and call it whenever you need it. Let's write one now.
Practice your skills
Sign up to write and run code in this lesson.