Unlock a total of 15 GB storage at no cost …
by using your phone number
Functions in Python: Reuse Logic, Reduce Mistakes
Functions are the building blocks of any real software. They let you package behavior, name it, and call it again and again.
What this article covers
- defining a function with
def - calling functions
- returning values
Example
def greet(person_name):
print(f"Hello, {person_name}! Welcome to Python.")
def calculate_total(items, tax_rate):
subtotal = sum(items)
return subtotal + subtotal * tax_rate
print(calculate_total([10, 20, 30], 0.18))
Why functions matter
Instead of copying and pasting the same logic, functions let you centralize work. If something changes, you fix it once.
Real-world angle
A function can model a business rule like calculating an invoice total. That same logic can then be used in a web app, script, or automation task.
Next step
Use functions together with collections and modules to build more complex applications.
Top comments (0)