(Functions help you write reusable, modular code β aka less typing, more doing!)
A function is a reusable block of code that only runs when it is called. It helps break programs into smaller chunks to make them more organized and readable.
def function_name():
# Code block (indented)
...
Example:
def greet():
print("Hello, world!")
To run (or βcallβ) this function:
greet()
avg()
and greet()
def avg():
a = int(input("Enter Number 1: "))
b = int(input("Enter Number 2: "))
c = int(input("Enter Number 3: "))
average = (a + b + c) / 3
print("Average:", int(average)) # int() rounds down
def avg():
β We define a new function.