A function is a reusable block of code. You write it once, then call it whenever you need it.
def greet(name):
print("Hello,", name, "!")
greet("Shoaib")
greet("PyBlitz")
Why Functions Matter
- No copy-paste: fix a bug once, fixed everywhere.
- Readable code: each function does one job.
- Real projects are built from functions.
Return Values: A function can also GIVE BACK a result:
def add(a, b):
return a + b
print(add(3, 4))
Mini Challenge: Write a function that takes a number and returns True if it is even.
Follow PyBlitz: YouTube https://youtube.com/@pyblitz | Instagram https://instagram.com/pyblitz | GitHub https://github.com/pyblitz
💬 Comments