Day 3 · ~13m

Python Functions: Define, Call, and Reuse Code

Learn to write a Python function that takes parameters and returns a formatted value. Define once, call many times — the first step toward automating repetitive work.

student (neutral)

I've been typing the same Excel formula into forty cells Monday morning for months. There's got to be a way to write it once and just... use it everywhere.

teacher (excited)

That's exactly what a function does. In Python, you write a function once and call it as many times as you need. One definition, unlimited uses.

student (curious)

OK but how do I actually write one? I know what def means now, but I don't know what comes after.

teacher (focused)

Let's build one together. Imagine you get a list of sales from your team — customer names and dollar amounts. Instead of manually formatting each one in Excel, you want Python to do it.

def format_sale(name, amount):
    return name + ": $" + str(amount)

Let me break this down:

  • def says "I'm creating a function"
  • format_sale is the name you give it
  • name and amount are parameters — the inputs your function expects
  • return sends the result back

Notice that amount is a number, but we use str(amount) to turn it into text before joining the strings together.

student (thinking)

Wait, why do we need str(amount)? Can't we just add the number to the string?

teacher (serious)

Nope — Python gets weird about mixing types. Try adding a number and a string without converting first and Python throws an error. str() converts the number to text, which you can then join with the + operator. A lot of people think functions are advanced — actually they're the first tool you reach for when you notice yourself doing the same thing repeatedly.

student (amused)

So str() is like a translator?

teacher (encouraging)

Perfect analogy. It's a built-in function that says "take this number and give me the text version." Once you have a function like format_sale, you call it by name with the values you want:

format_sale("Alice Chen", 1250.00)   # Returns: "Alice Chen: $1250.0"
format_sale("Bob Kumar", 340.50)     # Returns: "Bob Kumar: $340.5"

Same function, different inputs, different outputs. That's the power — you write it once, the logic is locked in, and you just swap the parameters.

student (excited)

That would actually save me so much time. I could format a whole week's sales in like... five lines of code?

teacher (proud)

Exactly. Your manager Diane gives you a CSV with names and amounts, you loop through each row (we'll learn that next week), call format_sale() for each one, and boom — perfectly formatted. This is what programming is really about — removing the repetition from your work.

student (surprised)

OK I think I actually get it. So the function doesn't care what names or amounts I give it — it just follows the formula?

teacher (serious)

That's the whole point. The function is a template. As long as you pass a name (text) and an amount (a number), it will format them the same way every single time. No thinking required from you, no typos, no mistakes. This works great for one sale. But right now your function's output is limited by how you build the string — you're locked into that exact format. Tomorrow we'll look at what Python considers the difference between text and numbers — and why it matters more than you'd think.