What Is a Function?
As your programs grow, you'll notice yourself writing the same code in multiple places. Maybe you're calculating a total in three different spots, or printing a formatted message over and over. This repetition creates problems — if you need to change something, you have to find and update every copy.
A function solves this by wrapping code in a reusable package with a name. Define the code once, then call it by name whenever you need it. Functions are one of the most important tools for organizing programs.
You've Already Used Functions
Every time you've written print("Hello") or len(my_list), you've called a function. Someone else wrote the code inside print() and len() — you just use them by name without knowing the details.
message = "Hello, world!"
print(message) # print is a function
length = len(message) # len is a function
user_input = input("Name: ") # input is a function
These are built-in functions that Python provides. But you can also create your own custom functions for tasks specific to your program.
Why Functions Matter
Reduce repetition. Write code once, use it many times. If you need to change how something works, you change it in one place.
Improve readability. A well-named function like calculate_tax() tells readers what's happening without them needing to understand every line inside.
Manage complexity. Breaking a large program into smaller functions makes each piece easier to understand and test.
Enable collaboration. Different people can work on different functions, then combine them into a complete program.
The Recipe Analogy
Think of a function like a recipe. A recipe has a name ("Chocolate Chip Cookies"), a list of ingredients (inputs), and step-by-step instructions. When you want cookies, you don't reinvent the process — you follow the recipe.
Similarly, a function has a name, accepts inputs, and contains instructions. When you need that task done, you "call" the function by name.
What's Coming Next
In the following lessons, you'll learn how to define your own functions, pass information into them, and get results back. You'll see how functions help you write cleaner, more maintainable code.
The ability to create functions transforms you from someone who writes scripts into someone who builds organized, professional programs. It's a fundamental skill that every programmer uses daily.