
Python
Functions are reusable blocks of code that perform specific tasks. Think of them like factory machines ā they take inputs, process them, and produce outputs.
Without functions, you'd repeat the same code over and over:
# Without functions ā repetitive code
print("Hello! Welcome to Python!")
print("Let's learn together!")
print("Hello! Welcome to Python!")
print("Let's learn together!")
print("Hello! Welcome to Python!")
print("Let's learn together!")Notice how we're repeating the same lines? This is inefficient and hard to maintain.
With functions, you write once and reuse many times:
# With functions ā clean and organized
def greet_user():
print("Hello! Welcome to Python!")
print("Let's learn together!")
# Call the function as many times as needed
greet_user()
greet_user()
greet_user()Much better! āØ
Write once, use many times. Saves time and reduces errors.
Group related code into logical blocks. Much easier to understand.
Test each function independently to find bugs faster.
Reuse functions in multiple programs. Share with other developers.
Make your code more readable and professional.
def function_name():
# Code to execute
# Indented 4 spaces (or 1 tab)
print("This code runs when the function is called")Key Points:
def greet_user():
print("Hello! Welcome to Python Functions!")
print("Let's learn together!")
greet_user()Output:
Hello! Welcome to Python Functions!
Let's learn together!š” Key Point: The function only runs when you call it by typing `greet_user()`
Let me break down a function into its parts:
def greet_user(): # ā Function definition line
print("Hello!") # ā Function body (indented)
print("Welcome!") # ā More function body
greet_user() # ā Function call| Part | Meaning |
|---|---|
| `def` | "Define a function" keyword |
| `greet_user` | Function name (your choice) |
| `()` | Parameters go here (empty for now) |
| `:` | Indicates start of function body |
| Indented code | The function's instructions |
| `greet_user()` | Calling the function (running it) |
When you run a function, here's what happens:
def introduce():
print("My name is Python") # Step 2: Run this
print("I am 33 years old") # Step 3: Then this
print("Starting program") # Step 1: First
introduce() # Step 2: Jump to function
print("Program complete") # Step 4: Continue hereOutput:
Starting program
My name is Python
I am 33 years old
Program completedef welcome_user():
print("=" * 40)
print("š WELCOME TO PYTHON LEARNING!")
print("=" * 40)
print("This is an exciting journey!")
print("Let's build something amazing!")
print("=" * 40)
# Use the function multiple times
welcome_user()
print("\n" + "Doing some work..." + "\n")
welcome_user()Output:
========================================
š WELCOME TO PYTHON LEARNING!
========================================
This is an exciting journey!
Let's build something amazing!
========================================
Doing some work...
========================================
š WELCOME TO PYTHON LEARNING!
========================================
This is an exciting journey!
Let's build something amazing!
========================================Notice how we call `welcome_user()` twice and get the same output both times without repeating code!
def greet():
print("Hello!")
greet # ā Missing () - function doesn't run!The function won't run without the parentheses `()`. You must write `greet()`.
def greet() # ā Missing colon!
print("Hello!")Python requires a `:` at the end of the `def` line.
def greet():
print("Hello!") # ā Not indented - syntax error!Code inside the function must be indented (4 spaces or 1 tab).
def greet(): # ā
Has ()
print("Hello!") # ā
Indented
greet() # ā
Called with ()Try creating these simple functions:
def say_goodbye():
print("Goodbye!")
print("See you later!")
say_goodbye()def print_line():
print("=" * 50)
print_line()
print("Hello World")
print_line()def show_name():
print("My name is Python")
show_name()
show_name()
show_name()| Concept | Remember |
|---|---|
| Functions | Reusable blocks of code |
| def keyword | Used to create/define functions |
| Function name | You choose this (use lowercase with underscores) |
| Parentheses | Required when defining: `()` |
| Colon | Required at end of function definition: `:` |
| Indentation | All function code must be indented |
| Calling | Use `function_name()` to run a function |
| No parameters yet | We'll add those in the next lesson |
Now that you understand WHY we use functions and HOW to create simple ones, the next step is to make functions flexible by adding parameters.
Next: Parameters and Arguments ā
Ready to practice? [Take the quiz](/python/functions/quiz) or [try a challenge](/python/functions/challenges)
Resources
Ojasa Mirai
Master AI-powered development skills through structured learning, real projects, and verified credentials. Whether you're upskilling your team or launching your career, we deliver the skills companies actually need.
Learn Deep ⢠Build Real ⢠Verify Skills ⢠Launch Forward