Writing functions
Part of the free LogicWiz Python course, Module 3: Functions. The examples and practice run in the app.
Aarav pasted the same three bill lines in five places in Canteen Counter. Then Shankar bhaiya added a ₹10 packing charge, and Aarav had to fix five places. He missed one. A function means you write it once and fix it once.
You'll learn to
- Define a function with `def` and call it
- Tell `print` and `return` apart, and know where `None` comes from
- Use default and keyword arguments
- Return several values at once and unpack them
Learn
A function is a named recipe
def gives a block of code a name. The names in brackets are parameters: blanks that get filled each time you call it. The body is indented.
def bill(price, qty):
return price * qty
Defining a function does not run it. It only runs when you call it: bill(20, 3). The values you pass (20, 3) are the arguments. Your board book calls them actual parameters, and price, qty the formal parameters.
Call it as many times as you like
def bill(price, qty):
total = price * qty
return total
a = bill(20, 3)
b = bill(15, 2)
print(a, b, a + b)
Step through it. Python jumps into bill, fills price and qty, and return sends the answer back to the line that called it.
print vs return
| print(x) | return x |
|---|---|
| Shows x on the screen | Hands x back to the caller |
| The caller gets nothing back (None) | The caller can store it, add it, print it |
| The function carries on | The function stops right there |
| For the person reading the screen | For the rest of your program |
The difference, side by side
def show_total(p, q):
print(p * q)
def get_total(p, q):
return p * q
x = show_total(20, 3)
y = get_total(20, 3)
print("x is", x)
print("y is", y)
show_total prints 60 but gives back nothing, so x is None. get_total prints nothing but gives back 60.
A function with no return (or a bare return) gives back None. If you later do x * 2 you get TypeError: unsupported operand type(s) for *: 'NoneType' and 'int'. When you see NoneType in an error, look for a missing return.
Defaults and keywords
A default fills a parameter when the caller leaves it out: def bill(price, qty=1, gst=5).
A keyword argument names the parameter at the call: bill(40, gst=12). Order doesn't matter for keywords.
Two rules:
- In the header, parameters with defaults come after those without.
- In a call, positional arguments come before keyword ones.
One function, four ways to call it
def bill(price, qty=1, gst=5):
return round(price * qty * (1 + gst / 100), 2)
print(bill(40))
print(bill(40, 3))
print(bill(40, gst=12))
print(bill(qty=2, price=50))
Returning several values
def split_bill(total, people):
share = total // people
left = total % people
return share, left
s, l = split_bill(250, 3)
print("Each pays", s, "and", l, "is left over")
print(split_bill(250, 3))
return share, left sends back a tuple (83, 1). s, l = … unpacks it into two names.
Board papers love two things here. Find the error: def f(a=1, b): is a SyntaxError (non-default argument follows default argument). Predict the output: a function that prints and returns, called inside print(). Read each call slowly: what is printed inside, and what comes back.
In one line
def makes a function, a call runs it; return hands a value back (no return means None); defaults go last in the header, keywords go last in the call.