What Is a Function?
A function is a reusable block of code that performs a specific task. You write it once, and then call it whenever you need it.
def greet():
print("Hello from a function!")
You call it like this:
greet()
Functions with Parameters
Parameters allow you to send data into a function.
def greet(name):
print("Hello,", name)
greet("Kaloyan")
Functions That Return Values
def add(a, b):
return a + b
result = add(5, 3)
print(result) # 8
return sends a value back to wherever the function was called.
Default Values
def welcome(name="Guest"):
print("Welcome,", name)
welcome()
welcome("Kaloyan")
What Is a Procedure?
A procedure is just a function that does not return anything.
def log(message):
print("[LOG]", message)
Why Functions Matter
- Cleaner code
- No repetition
- Easier debugging
- Reusable components
Example — Calculator
def add(a, b):
return a + b
def subtract(a, b):
return a - b
print(add(10, 5))
print(subtract(10, 5))
What’s Next?
Next you’ll learn how to build simple webpages using HTML.