๐Ÿ
๐Ÿ Python Fundamentals (Hands-On) ยท PY-101 ยท Session 4 of 6

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.

โฑ 8 min read๐Ÿ“… Updated 2026-07-27๐Ÿ“Š Tables, charts & examples

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.

๐Ÿ“Œ Worked example
Name it once, use it everywhere. You find yourself writing the same three lines to format a price in several places. Then a bug appears in all of them, and you fix it in three spots (and miss a fourth). The fix: put those lines in a function, format_price(), and call it everywhere. Now there's one place to get it right, one place to fix, and your code reads more clearly. That's the everyday power of functions - not clever tricks, just naming reusable pieces so you don't repeat yourself.

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
PartWhat it is
def name(...)Defines a function called 'name'
parametersThe inputs it accepts
returnThe value it gives back (optional)
callingname(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.

๐ŸŽฏ
Break big problems into small functions. The real skill of programming is decomposition: splitting a hard task into small, named functions that each do one clear thing, then combining them. Each function is small enough to understand and test on its own (with larzkata!), and together they solve the big problem. Good function names make code read almost like a description of what it does.

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.

Session 4 of 6 complete

Next up: Lists and Dictionaries

Continue to Session 5 โ†’

Related lessons

More in this course

Course home → · Browse all courses →