Functions in Python#
š Data Analysis Using Python š Python Fundamentals Lesson 008
ā Previous Ā· Next ā¶ Ā· ā Section Ā· ā Hub
Important
⨠AI-generated content. This page was written with the assistance of an AI language model and is provided as a learning aid. Despite careful review, it may still contain mistakes, omissions, or out-of-date information. Whether you are new to the topic, a team lead, or a senior practitioner, treat it as a starting point rather than an authoritative reference: read it critically and independently verify anything you act on (code, commands, figures, and factual claims) against official documentation and primary sources before relying on it.
Reusable blocks of code#
As programs grow, the same operations recur ā and copying code to repeat them is the duplication the foundations warned against. Functions are Pythonās tool for reuse: a named, reusable block of code that performs a task, defined once and called wherever needed. This lesson covers defining and calling functions ā the core of writing clean, non-repetitive Python.
Defining and calling a function#
A function is defined with def, given a name, parameters, and a body, and called
by name:
def add_tax(amount, rate):
total = amount + amount * rate
return total
result = add_tax(1000, 0.08) # call it; result is 1080.0
print(add_tax(50, 0.10)) # 55.0
def add_tax(amount, rate): defines a function taking two parameters; the indented
body is what it does; return gives back a result. Calling add_tax(1000, 0.08)
runs the body with those arguments and evaluates to the returned value. The same
function serves any amount and rate ā written once, used many times.
Parameters, arguments, and return#
The pieces of a function:
Parameters ā the named inputs in the definition (
amount,rate) ā the āparameterized functionā idea, letting one function handle many inputs.Arguments ā the actual values passed in a call (
1000,0.08).Return value ā what the function gives back via
return, usable by the caller. A function withoutreturngives backNone.Default arguments ā parameters can have defaults used when an argument is omitted:
def add_tax(amount, rate=0.08): # rate defaults to 0.08 return amount + amount * rate add_tax(1000) # uses default rate: 1080.0 add_tax(1000, 0.10) # overrides: 1100.0
Defaults are the āexplicit defaultsā principle from the foundations, in code.
Why functions matter#
Functions are the primary tool for the foundationsā reuse and abstraction principles.
They eliminate duplication (write the logic once, call it many times), improve
readability (a well-named function like add_tax documents what a block of code does),
enable testing (a function can be verified in isolation), and localise change (fix the
logic in one place). A program built from well-named functions is modular, readable, and
maintainable ā the same virtues good structure gives anything, achieved in code through
functions.
The caveat#
Functions can be misused in opposite directions. Too little use ā repeating code instead of writing a function ā produces the duplication that makes programs unmaintainable (change the logic and you must find every copy). Too much or wrong abstraction ā functions that do too many things, or are split so finely that following the logic means jumping among many tiny functions ā harms readability in the other direction. The balance is the single-responsibility idea: a function should do one well-defined thing, be named for it, and be neither a sprawling catch-all nor a needless fragment. And functions should ideally be pure where practical ā depending only on their inputs and returning a result, without hidden side effects ā which makes them predictable and testable, the controlled-side-effects principle in code. The next lessons cover writing clean, well-documented Python.
Hint
See also
Source article Adapted (context, re-expressed) in our own words from: https://insightful-data-lab.com/2023/12/06/functions-in-python/ (insightful-data-lab.com).