By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
When you need to perform the same task multiple times, copying and pasting code creates a maintenance nightmare. You end up with duplicated logic scattered throughout your script. When you find a bug, you have to fix it in every copy. Functions solve this by letting you write a block of code once, name it, and call it whenever you need it.
A function is a reusable block of code that performs a specific task. You define it with the def keyword, give it a name, and list any inputs (called parameters) in parentheses. The body of the function is indented below the definition.
Functions can have default parameter values. If a caller doesn't provide a value for that parameter, the default is used. This makes your function flexible without forcing callers to specify everything every time.
def greet(name, greeting="Hello"):
"""Return a formatted greeting."""
return f"{greeting}, {name}!"
print(greet("Alice")) # Hello, Alice!
print(greet("Bob", "Good morning"))# Good morning, Bob!In the example above, greet has two parameters: name (required) and greeting (optional, defaults to "Hello"). When you call greet("Alice"), it uses the default greeting. When you call greet("Bob", "Good morning"), it overrides the default. The function returns a formatted string each time.
Sometimes a function needs to send back more than one piece of information. Python lets you return multiple values as a tuple, and the caller can unpack them into separate variables in one line.
def min_max(numbers):
return min(numbers), max(numbers)
low, high = min_max([3, 1, 4, 1, 5, 9])
print(f"Min: {low}, Max: {high}") # Min: 1, Max: 9The function min_max returns two values: the minimum and maximum from a list. The caller unpacks them into low and high. This is cleaner than calling two separate functions or storing results in a data structure.
Variables have scope: the region of your code where they exist. A variable defined inside a function is local to that function. It only exists while the function is running, and you cannot access it from outside.
def my_func():
local = "only here"
my_func()
# print(local) # NameError — local does not exist hereIn this example, local is created inside my_func(). Once the function finishes, local ceases to exist. If you try to print it afterward, Python raises a NameError. This is actually a good thing: it prevents accidental name collisions and keeps functions independent.
Variables defined inside a function are local — they don't exist outside it. If you need a value from a function, have the function return it explicitly.
Functions can read global variables (defined at the top level of your script), but they cannot modify them unless you explicitly use the global keyword. For beginners, the safest approach is to pass data in as parameters and return results explicitly. This makes your functions predictable and testable.
Functions are the foundation of organized code. They let you break a large problem into smaller, named pieces. Each function does one thing well. When you need to fix a bug or add a feature, you know exactly where to look.
Functions also make testing easier. You can write tests for individual functions without running your entire program. As your code grows, this becomes invaluable.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.