Python Introduction to Functions
Table of Contents + −
In the last lesson you learned Loop Patterns. You saw the common shapes a loop can take. Loops let you repeat the same code many times in a row. But what if you want to run the same set of steps later, in a different part of your program? Copy-pasting those lines everywhere gets messy fast. That is the problem functions solve.
🤔 Why Functions?
Imagine you write a few lines that greet a user and print a welcome message. Now you need that same greeting in five different places in your program. So you copy-paste it five times.
Then one day you want to change the wording. Now you have to find all five copies and fix each one. Miss one, and your program shows the wrong message. This is the real pain. Repeated code is hard to change and easy to get wrong.
A function fixes this. You write the steps once. You give them a name. Then you run them by that name whenever you need them. Change the wording in one place, and every use updates at once.
🧩 What Is a Function?
A function is a named, reusable block of code that does one job.
Think of a coffee machine. You do not grind the beans, heat the water, and pour the cup by hand every time. You press one button labeled “Latte”. The machine does all the steps inside. You just press the button and get your coffee.
A function works the same way:
- You bundle a set of steps together once.
- You give that bundle a name, like the label on the button.
- Later you “press the button” by calling the name, and all the steps run.
So the name hides the steps. You think about what it does (“make a latte”). You do not think about how it does it.
Tip
A good function does one clear job. If you can describe it in a short sentence without saying “and”, that is usually a sign the function is focused.
☕ Calling a Function
Running a function is called calling it. You write the function’s name followed by round brackets ().
You have already been calling functions this whole time. Here is one you know well.
print("Hello, Alex!")The word print is the function’s name. The () brackets tell Python “run this now”. The text inside the brackets is what you hand to the function to work with.
Output
Hello, Alex!You did not write the code that makes text appear on the screen. Someone wrote it once and named it print. Now you just call it. That is the whole idea of a function in one line.
🧰 Built-in Functions You Already Met
Python comes with many functions ready to use. These are called built-in functions because they are built into the language. You do not have to write them or import anything.
Here are a few you have already seen.
| Function | What it does | Example |
|---|---|---|
print() | Shows text on the screen | print("Hi") |
len() | Counts how many items are in something | len("cat") |
input() | Reads what the user types | input("Name? ") |
int() | Turns text into a whole number | int("42") |
Let us see two of them work together. This code asks for a word and tells you how many letters it has.
word = input("Type a word: ")length = len(word)print("That word has", length, "letters.")If the user types python, here is what shows up.
Output
Type a word: pythonThat word has 6 letters.Notice how much each function does for you. input handles reading from the keyboard. len does the counting. You did not write either one. You just called them by name and used the result.
🛠️ Making Your Own Functions
Built-in functions are great, but they cannot do everything. Sometimes you need a step that is special to your own program. Greeting a user in your exact style is one example.
When that happens, you can make your own function. You write the steps. You give them a name. Then you call that name, just like you call print.
Here is a tiny taste of what that looks like. Do not worry about every detail yet. The next lesson covers it slowly.
def greet(): print("Welcome!") print("We are glad you are here.")
greet()greet()The def keyword starts a function. greet is the name you chose. The two indented lines are the steps inside. After that, greet() calls it, and you call it twice here.
Output
Welcome!We are glad you are here.Welcome!We are glad you are here.See what happened? You wrote the two print lines once. But by calling greet() twice, the steps ran twice. If you ever want to change the welcome message, you fix it in one place, inside the function. Every call gets the new version on its own.
That is the payoff of functions. You write them once and use them many times.
🍳 A Recipe Analogy
Another way to picture a function is a recipe card.
A recipe has a name at the top, like “Pancakes”. Below it are the steps. Once the card is written, anyone can follow it again and again. You do not rewrite the steps each morning. You just say “make the pancake recipe” and follow the card.
- The recipe name is the function name.
- The steps on the card are the code inside the function.
- Following the recipe is calling the function.
So a function is just a named recipe your program can follow whenever it wants.
⚠️ Common Mistakes
A few small things trip people up when they first meet functions.
The first is forgetting the brackets when you call a function. The name alone does not run it.
# ❌ Avoid: this does not run the function, it just refers to itprint
# ✅ Good: the brackets tell Python to run it nowprint("Hello")The next is thinking a function runs the moment you write its steps. Defining a function and calling it are two separate things. Writing def greet(): only describes the steps. Nothing happens until you write greet().
The last is trying to make one function do many unrelated jobs. A function that greets the user and does math and saves a file is hard to name and hard to fix. Keep each function focused on one job.
✅ Best Practices
Keep these simple habits in mind as you start working with functions.
- Give a function a clear name that says what it does, like
greet_userorcount_words. - Make each function do one job. One job per function keeps your code easy to read and fix.
- Reuse before you repeat. If you catch yourself copy-pasting the same lines, that is a sign to make a function instead.
- Use built-in functions when they already do what you need. There is no reason to rebuild
lenyourself.
Note
Functions are one of the most important ideas in all of programming, not just Python. Every language has them in some form. Time spent understanding them here pays off everywhere.
🧩 What You’ve Learned
✅ A function is a named, reusable block of code that does one job.
✅ Functions solve the pain of repeated code. You write the steps once, then call them by name anywhere.
✅ You call a function by writing its name followed by (), like print("Hi").
✅ Python has built-in functions ready to use, like print, len, input, and int.
✅ You can also make your own functions with def when you need a step special to your program.
Check Your Knowledge
Test what you learned. Pick an answer for each question, then click Check.
- 1
What is a function in Python?
Why: A function bundles a set of steps under one name so you can run them whenever you need them.
- 2
How do you call a function named greet?
Why: You call a function by writing its name followed by round brackets, so greet().
- 3
Why are functions useful for repeated code?
Why: Writing the steps once and calling them by name means you fix or change the code in just one place.
- 4
Which of these is a built-in function you have already used?
Why: len() is built into Python and ready to use; the others would be functions you write yourself.
🚀 What’s Next?
Now that you know what a function is and why it matters, the next step is to build your own from scratch and see exactly how the def keyword works.