Courses/Python Mastery/Module 7: Functions
Module 7 · Lesson 125 minBeginner

Defining Functions, Parameters, Return

Lesson goal
Write reusable blocks — the single biggest upgrade in your coding.

Defining Functions, Parameters, Return

Functions are the single biggest upgrade in your coding life. Before them: one long script, top to bottom. After them: named, reusable, testable blocks — the difference between a pile of code and a *program*.

The problem functions solve

code
# The same greeting logic, three times...
name1 = "Aarav"
print(f"Hello, {name1}! Welcome back.")
name2 = "Diya"
print(f"Hello, {name2}! Welcome back.")
name3 = "Kabir"
print(f"Hello, {name3}! Welcome back.")

Change the greeting format? Edit three places. Miss one? Bug. Functions collapse repetition into one definition:

code
def greet(name):
    print(f"Hello, {name}! Welcome back.")

greet("Aarav")
greet("Diya")
greet("Kabir")

Anatomy of a function

code
def greet(name):              # def + name + parameters + colon
    """Say hello to someone."""     # docstring (optional but pro)
    message = f"Hello, {name}!"     # the body — indented block
    return message                  # send the result back

result = greet("Aarav")       # calling it
print(result)                 # Hello, Aarav!

Four parts:

  • `def` — the keyword announcing a function definition
  • `greet` — its name (snake_case, verb-like: greet, calculate, save)
  • `(name)` — parameters: the inputs it needs
  • `return` — the output it sends back
  • return — the whole point

    return sends a value back to whoever called the function — and ends the function immediately:

    code
    def add(a, b):
        return a + b
    
    total = add(5, 3)       # add runs, returns 8, total holds 8
    print(total)            # 8
    print(add(10, 20) * 2)  # returned values work anywhere: 60

    The crucial distinction — printing vs returning:

    code
    def bad_add(a, b):
        print(a + b)        # DISPLAYS 8, but returns...
    
    x = bad_add(5, 3)
    print(x)                # None! — printing is not returning

    A function without a return (or with bare return) gives back None. If your function's result "disappears," check: did you return it, or just print it? This is the #1 beginner function bug.

    Multiple parameters — order matters

    code
    def introduce(name, age, city):
        print(f"{name}, {age}, from {city}")
    
    introduce("Aarav", 16, "Delhi")        # positional: order decides
    introduce(city="Mumbai", name="Diya", age=15)   # keyword: names decide

    Positional arguments match by order. Keyword arguments match by name — order stops mattering, and readability jumps.

    The docstring habit

    code
    def calculate_tip(bill, percent):
        """Return the tip amount for a bill at a given percentage."""
        return bill * percent / 100

    One line: what it returns, from what inputs. help(calculate_tip) now shows it — your future self will thank present-you.

    Functions compute — they don't ask

    A subtle design principle: keep input/output OUT of calculation functions:

    code
    # ❌ mixed concerns — can't reuse without a human typing
    def get_bmi():
        w = float(input("Weight: "))
        h = float(input("Height: "))
        print(w / h ** 2)
    
    # ✅ pure function — testable, reusable anywhere
    def calculate_bmi(weight, height):
        return weight / height ** 2
    
    # the UI wraps around it
    print(calculate_bmi(70, 1.75))

    The pure version works from user input, files, APIs, or tests — because it only *computes*.

    Common Errors & Fixes

  • Function runs but result is None — missing return. Printing ≠ returning.
  • `NameError: name 'greet' is not defined` — you called it before defining it, or misspelled the call. Definitions must run before calls.
  • `TypeError: greet() missing 1 required positional argument` — called with too few inputs. Count your parameters.
  • Nothing happens when I define it — correct! def only *creates* the function. Nothing runs until you call it.

  • ✅ Checkpoint

  • What's the difference between print and return inside a function? *(Display vs send back — print returns None)*
  • introduce("Aarav", 16, "Delhi") — what are the three values called? *(Positional arguments)*
  • Where does a function stop executing? *(At return — or at the end if no return)*
  • Why keep input() out of calculation functions? *(So they're reusable from anywhere — input, files, APIs)*
  • Next: default and keyword arguments — functions that adapt.