Python19 min read
Python Functions
Learn how functions work, how parameters and return values behave, and how to design small reusable functions.
Emily Davis
September 12, 2025
3.2k71
Functions let you organize code into reusable blocks. Instead of rewriting the same logic in multiple places, you define it once and call it whenever needed.
Why functions matter (real reason)
Without functions:
- your code becomes repetitive
- bugs multiply because you fix one place but forget others
- projects become hard to read
With functions:
- logic is reusable
- code is easier to test
- you build programs like building blocks
A simple function
def greet():
print("Hello from Los Angeles!")
greet()
Expected output:
Hello from Los Angeles!
Parameters (input to a function)
def greet_person(name):
print(f"Hello {name}!")
greet_person("Emma")
greet_person("Jack")
Expected output:
Hello Emma!
Hello Jack!
Return values (output from a function)
def add(a, b):
return a + b
result = add(5, 3)
print(result)
Expected output:
8
Default parameters
def greet(name, city="Chicago"):
print(f"Hello {name} from {city}!")
greet("Tom")
greet("Sarah", "Miami")
Expected output:
Hello Tom from Chicago!
Hello Sarah from Miami!
Multiple return values (tuple return)
def get_user():
name = "David"
age = 30
return name, age
user_name, user_age = get_user()
print(user_name)
print(user_age)
Expected output:
David
30
Graph: function call lifecycle
flowchart LR
A[Call function] --> B[Run body]
B --> C{Return?}
C -->|Yes| D[Send value back]
C -->|No| E[Finish]
In the next lesson, you will learn dictionaries, which is one of the most practical data structures in Python and common in API work.
#Python#Beginner#Functions