🧠 Python Chapter 8 – Functions

(Functions help you write reusable, modular code β€” aka less typing, more doing!)


πŸ”Ή What is a Function?

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.


πŸ“Œ Basic Function Syntax:

def function_name():
    # Code block (indented)
    ...

Example:

def greet():
    print("Hello, world!")

To run (or β€œcall”) this function:

greet()

✨ Real Example – 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

Explanation: