Functions: Reusable Building Blocks
Functions let you name a chunk of code and reuse it. They're how you avoid repetition, organise a program into pieces, and build up from small parts to big things.
As programs grow, you need a way to organise them and avoid writing the same code repeatedly. Functions are the answer: a function is a named, reusable chunk of code that can take inputs and give back a result. They're how you break a big problem into manageable pieces and build up from small, tested parts - a skill that pays off enormously.
Defining and calling functions
You define a function once, then call it whenever you need it. Parameters are inputs; return gives back a result.
def greet(name): # 'name' is a parameter (an input)
return 'Hello, ' + name # return gives back a value
message = greet('Ada') # call it; message is now 'Hello, Ada'
print(greet('Bob')) # reuse it with a different input| Part | What it is |
|---|---|
| def name(...) | Defines a function called 'name' |
| parameters | The inputs it accepts |
| return | The value it gives back (optional) |
| calling | name(args) - actually run it |
Define once, call many times. A function that returns a value can be used wherever you'd use that value - stored, printed, passed to another function.
Why functions matter
Functions aren't just about avoiding repetition - they let you think in named pieces, which is how you tackle anything complex.
Build with functions
- def defines a reusable function; parameters are its inputs.
- return gives back a result you can use elsewhere.
- Call a function by name to run it - as often as you like.
- Decompose - break big problems into small, named functions.
๐ฏ Key takeaways
- A function is a named, reusable chunk of code that can take inputs (parameters) and return a result.
- Define once with def, then call it by name as many times as you need.
- return gives back a value you can store, print or pass on; without it a function just does something.
- Functions let you decompose big problems into small, testable, named pieces - the core skill.
Frequently asked
What's the difference between a parameter and an argument?
They're two sides of the same coin. A parameter is the name in the function definition that stands for an input - in def greet(name):, 'name' is a parameter. An argument is the actual value you pass in when you call the function - in greet('Ada'), 'Ada' is the argument. So parameters are the placeholders you define; arguments are the real values you supply each time you call. Beginners often use the words interchangeably and it rarely causes confusion, but the distinction is: parameter = the slot, argument = what you put in it. When you call greet('Ada'), the argument 'Ada' gets stored in the parameter name for that run.
What's the difference between return and print in a function?
print shows a value on the screen; return gives a value back to the code that called the function. They're often confused but do very different jobs. A function that prints displays something but hands nothing back, so you can't store or reuse the result. A function that returns hands a value back, which you can save in a variable, use in a calculation, or pass to another function - the screen shows nothing unless you print it. As a rule, functions that compute a result should return it (so it's reusable), and you print at the point where you actually want to show something. Getting this distinction clear early prevents a lot of beginner confusion.
Related lessons
Repeating with Loops
Computers are brilliant at doing the same thing many times. Loops let you repeat actions wโฆ
Python Fundamentals (Hands-On)Lists and Dictionaries
Real programs juggle lots of data. Lists (ordered collections) and dictionaries (labelled โฆ
Learn to Code (from Zero)How Programs Think
Almost all programs are built from a few simple building blocks - values, decisions and reโฆ