Python Functions - Complete Guide
Functions are the building blocks of Python programs. They let you write code once and use it many times. Learning functions is essential - let's master them together!
What is a Function?
A function is a block of code that performs a specific task. You give it input, it does something, and optionally returns output.
Think of a function like a recipe:
- Input - Ingredients (parameters)
- Process - Cooking steps (function body)
- Output - Finished dish (return value)
Basic Function Syntax
def function_name():
# Code here
passSimple Example
def greet():
print("Hello, World!")
# Call the function
greet() # Output: Hello, World!Functions with Parameters
Functions can take input values called parameters:
def greet(name):
print(f"Hello, {name}!")
greet("John") # Hello, John!
greet("Jane") # Hello, Jane!Multiple Parameters
def introduce(name, age, city):
print(f"Hi, I'm {name}, {age} years old, from {city}")
introduce("John", 25, "New York")Default Parameters
Set default values for parameters:
def greet(name="Guest"):
print(f"Hello, {name}!")
greet() # Hello, Guest!
greet("John") # Hello, John!Keyword Arguments
Pass arguments by name:
def introduce(name, age, city):
print(f"{name}, {age}, {city}")
# Positional arguments
introduce("John", 25, "New York")
# Keyword arguments
introduce(age=25, city="New York", name="John")Functions with Return Values
Functions can return values:
def add(a, b):
return a + b
result = add(5, 3)
print(result) # 8Multiple Return Values
Python can return multiple values:
def get_name_and_age():
return "John", 25
name, age = get_name_and_age()
print(name) # John
print(age) # 25Variable Arguments
*args - Variable Positional Arguments
def sum_all(*args):
total = 0
for num in args:
total += num
return total
print(sum_all(1, 2, 3)) # 6
print(sum_all(1, 2, 3, 4, 5)) # 15**kwargs - Variable Keyword Arguments
def print_info(**kwargs):
for key, value in kwargs.items():
print(f"{key}: {value}")
print_info(name="John", age=25, city="NY")
# Output:
# name: John
# age: 25
# city: NYLambda Functions (Anonymous Functions)
Short functions without names:
# Regular function
def add(a, b):
return a + b
# Lambda function (same thing)
add = lambda a, b: a + b
print(add(5, 3)) # 8Lambda with map()
numbers = [1, 2, 3, 4, 5]
squared = list(map(lambda x: x ** 2, numbers))
print(squared) # [1, 4, 9, 16, 25]Lambda with filter()
numbers = [1, 2, 3, 4, 5, 6]
evens = list(filter(lambda x: x % 2 == 0, numbers))
print(evens) # [2, 4, 6]Scope and Variables
Local vs Global Variables
x = 10 # Global variable
def my_function():
x = 5 # Local variable
print(x) # 5
my_function()
print(x) # 10 (global unchanged)Using Global Variables
count = 0
def increment():
global count
count += 1
increment()
print(count) # 1Docstrings
Document your functions:
def add(a, b):
"""
Add two numbers together.
Args:
a: First number
b: Second number
Returns:
Sum of a and b
"""
return a + bRecursion
Functions that call themselves:
def factorial(n):
if n == 1:
return 1
return n * factorial(n - 1)
print(factorial(5)) # 120Higher-Order Functions
Functions that take other functions as arguments:
def apply_operation(x, y, operation):
return operation(x, y)
def add(a, b):
return a + b
def multiply(a, b):
return a * b
print(apply_operation(5, 3, add)) # 8
print(apply_operation(5, 3, multiply)) # 15Visual Explanation: Function Flow
Here's how functions work:
Function Call Flow:
┌─────────────────────────────┐
│ main code │
│ │ │
│ ├─► greet("John") │
│ │ │ │
│ │ ▼ │
│ │ ┌──────────────┐ │
│ │ │ greet() │ │
│ │ │ executes │ │
│ │ │ returns │ │
│ │ └──────┬───────┘ │
│ │ │ │
│ │ ▼ │
│ │ Returns control │
│ │ │
│ └─► Continue in main │
└─────────────────────────────┘Frequently Asked Questions (FAQ)
Q1: What's the difference between parameters and arguments?
A:
- Parameters - Variables in function definition
- Arguments - Values passed when calling function
def greet(name): # name is parameter
print(f"Hello, {name}")
greet("John") # "John" is argumentQ2: Can I change a parameter's value?
A: Yes, but it only affects the local copy:
def change_value(x):
x = 10 # Only changes local x
value = 5
change_value(value)
print(value) # Still 5Q3: What's the difference between *args and **kwargs?
A:
- *args - Variable positional arguments (tuple)
- **kwargs - Variable keyword arguments (dictionary)
Q4: When should I use lambda functions?
A: Use lambda for simple, one-line functions. For complex logic, use regular functions.
Q5: Can functions return multiple values?
A: Yes! Python returns them as a tuple:
def get_data():
return "John", 25, "NY"
name, age, city = get_data()Q6: What's a docstring?
A: A string at the start of a function that documents it. Use triple quotes:
def my_function():
"""This function does something."""
passQ7: Can I have optional parameters?
A: Yes! Use default values:
def greet(name="Guest"):
print(f"Hello, {name}")Q8: What's recursion?
A: When a function calls itself. Useful for problems that can be broken down into smaller versions of themselves.
Q9: Can I pass functions as arguments?
A: Yes! Functions are first-class objects in Python:
def apply(func, x, y):
return func(x, y)Q10: How do I know what a function does?
A: Read the docstring or use help():
help(my_function)Next Steps
Now that you understand functions, here's what to learn next:
- Next: Learn about Python Data Structures - Lists, dictionaries, tuples, and sets
- Explore Python Control Flow - If statements, loops, and more
- Understand Python Classes - Object-oriented programming
- Master Python Modules - Organize your code
Functions are essential in Python. Practice writing different types of functions to get comfortable!