Programming Fundamentals › Programming Basics
Function
A named, reusable block of code that takes inputs and returns an output.
Also known as: method, subroutine, procedure
A function is a named, reusable block of code. You give it inputs, it does some work, and it hands back a result.
def total_price(price, quantity, tax_rate=0.2):
subtotal = price * quantity
return subtotal * (1 + tax_rate)
total_price(10, 3) # 36.0
Defining the function writes the recipe. Calling it runs it. The same function can be called from many places with different arguments, which is how you avoid copying code.
What makes a good one
- One job, and a name that says it.
calculate_taxis clear.processisn’t (naming). - Inputs in, result out. Prefer returning a value over changing things behind the scenes (return values).
- Short enough to read at once. If you need to scroll, or a comment to mark sections, split it.
- Variables stay inside. Names created in a function exist only there (scope).
A common mistake
Forgetting to return:
def add(a, b):
a + b # computed, then thrown away; the caller gets None
In most languages a function without return gives back nothing (None, undefined).
Also called
Methods are functions attached to an object (user.save()). Many languages treat functions as values you can pass around (first-class functions).