>> Module 01 / Day 11 / Lecture

Functions — Your Own Verbs

Day 11 of a 40-day journey
Until now you've used functions like print() and int(). Today you start making your own.
Lecture
75 min · 09:00–10:15
Lab
Refactor into Functions
Module
M1 · Python Foundations
DAY 11 / 40
>> Today's roadmap

Five stops, then a lab

  1. Why functions? — name an idea, kill repetition
  2. The def syntax — defining your own verb
  3. Parameters — the inputs a function takes
  4. return vs print — the great beginner confusion
  5. Lab — refactor your Day-5 "About Me" into functions
>> 01 / 05 · Why bother

A function is a name for a job.

Two reasons you'll reach for functions every single day:

REASON 1
Kill repetition

Wrote the same 4 lines three times? Wrap them once, call them three times. Fix a bug in one place.

REASON 2
Name an idea

build_bio(...) reads like English. Your code becomes a story of verbs, not a wall of detail.

>> 02 / 05 · The shape of a function

The def syntax

def_syntax.py
def 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.
Anatomy.
def keyword · the name · () for inputs · a : · then an indented body.
>> 03 / 05 · Inputs

Parameters — feeding the function

A parameter is a variable that gets its value from whoever calls the function. Same recipe, different ingredients.

parameters.py
# `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
>> 04 / 05 · The confusion that trips everyone

return vs print

it_prints.py
def 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
it_returns.py
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!
>> A closer look at return

What return actually does

return_detail.py
def 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
Two facts about return:
1. It hands a value back to the caller.
2. It immediately stops the function — any lines after it never run.
>> 05 / 05 · Putting it together

Helpers return · main prints

The pattern you'll use in today's lab: small functions that return, glued together by one that prints.

together.py
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()
>> Common failures

Four mistakes you'll make today

  1. Forgetting return. A function with no return hands back None — your variable ends up empty.
  2. Confusing print with return. Printing shows it once; returning lets you reuse it. build_bio must return.
  3. Calling before defining. Python reads top-to-bottom — the def must appear above the call that runs it.
  4. Forgetting the (). greet is the recipe; greet() actually cooks it.
>> 11:00 – 12:00 · Lab

Today's lab — Refactor into Functions

Take your Day-5 "About Me" script and split it into four clean functions.

RETURNS

ask_text(prompt) → a string
ask_int(prompt) → an int
build_bio(...) → the bio string

PRINTS

main() wires the helpers together, gets the bio string back from build_bio, and is the only place that prints.

>> End of Day 11

What you can now do

  • ✓ Define your own functions with def and call them.
  • ✓ Pass inputs in as parameters and reuse one function many ways.
  • ✓ Tell return apart from print — and know when to use each.
01 / 11
←/→ navigate   F fullscreen   Ctrl/⌘+P save as PDF