print() and int(). Today you start making your own.
def syntax — defining your own verbreturn vs print — the great beginner confusionTwo reasons you'll reach for functions every single day:
Wrote the same 4 lines three times? Wrap them once, call them three times. Fix a bug in one place.
build_bio(...) reads like English. Your code becomes a story of verbs, not a wall of detail.
print(), int(), input() are functions someone else named for you. Today you become that someone.
def syntaxdef say_hello():
print("Hello!")
print("Nice to see you.")
# Defining it does NOTHING visible.
# You must CALL it to run it:
say_hello()
# → Hello!
# → Nice to see you.
def keyword · the name · () for inputs · a : · then an indented body.
A parameter is a variable that gets its value from whoever calls the function. Same recipe, different ingredients.
# `name` is a PARAMETER — a placeholder for an input.
def greet(name):
print(f"Hello, {name}!")
greet("Aisha") # the ARGUMENT "Aisha" fills `name` → Hello, Aisha!
greet("Ravi") # reuse with a different argument → Hello, Ravi!
# More than one input? Separate with commas:
def add(a, b):
print(a + b)
add(2, 3) # → 5
return vs printdef double(n):
print(n * 2) # shows it, then throws it away
x = double(5) # prints 10 ...
print(x) # but x is None !
print(double(5) + 1) # TypeError — can't add to None
def double(n):
return n * 2 # hands the value back
x = double(5) # x is now 10
print(x) # → 10
print(double(5) + 1) # → 11, you can reuse it!
print shows a value to the human. return hands a value to the rest of your code so it can be reused.
return actually doesdef build_bio(name, age):
bio = f"{name}, age {age}"
return bio # send the string back
print("never runs") # return ENDS the function
text = build_bio("Aisha", 19)
print(text) # → Aisha, age 19
return:main printsThe pattern you'll use in today's lab: small functions that return, glued together by one that prints.
def ask_text(prompt):
return input(prompt) # returns a string
def make_line(name, city):
return f"{name} from {city}" # returns a string
def main():
name = ask_text("Name? ")
city = ask_text("City? ")
print(make_line(name, city)) # only main() prints
main()
return. A function with no return hands back None — your variable ends up empty.print with return. Printing shows it once; returning lets you reuse it. build_bio must return.def must appear above the call that runs it.(). greet is the recipe; greet() actually cooks it.Take your Day-5 "About Me" script and split it into four clean functions.
ask_text(prompt) → a string
ask_int(prompt) → an int
build_bio(...) → the bio string
main() wires the helpers together, gets the bio string back from build_bio, and is the only place that prints.
students/<your-username>/D11_functions.py. Full brief in lab/module_1_foundations/D11_functions/problem.md. PR before 12:00.
def and call them.
return apart from print — and know when to use each.