# COMMON FUNCTIONS MISTAKES
# Each block shows the wrong way, the error it produces, and the right way
# Read through — don't just run it top to bottom

# ─────────────────────────────────────────────
# MISTAKE 1 — Calling before defining
# ─────────────────────────────────────────────

# Wrong — NameError
# greet("Bull")
# def greet(name):
#     print(f"Hello, {name}.")

# Right — define before calling
def greet(name):
    print(f"Hello, {name}.")

greet("Bull")

# ─────────────────────────────────────────────
# MISTAKE 2 — Forgetting parentheses
# ─────────────────────────────────────────────

def greet():
    print("Hello.")

greet     # does nothing — references the function object
greet()   # calls it — prints "Hello."

# ─────────────────────────────────────────────
# MISTAKE 3 — Forgetting return
# ─────────────────────────────────────────────

# Wrong — returns None
def calculate_bmi(weight, height):
    bmi = weight / height ** 2
    print(bmi)    # prints but doesn't return

result = calculate_bmi(70, 1.75)
print(result)    # None

# Right
def calculate_bmi(weight, height):
    return weight / height ** 2

result = calculate_bmi(70, 1.75)
print(result)    # 22.857...

# print inside a function is not return

# ─────────────────────────────────────────────
# MISTAKE 4 — Code after return
# ─────────────────────────────────────────────

def check(number):
    if number > 0:
        return "positive"
    print("This never runs.")    # unreachable
    return "zero or negative"

# return stops the function immediately
# everything after it in the same block is unreachable

# ─────────────────────────────────────────────
# MISTAKE 5 — Default parameters before required ones
# ─────────────────────────────────────────────

# Wrong — SyntaxError
# def greet(name="stranger", role):
#     print(f"{name} — {role}")

# Right — required first, defaults last
def greet(role, name="stranger"):
    print(f"{name} — {role}")

# ─────────────────────────────────────────────
# MISTAKE 6 — Modifying a global variable
# ─────────────────────────────────────────────

count = 0

# Wrong — UnboundLocalError
# def increment():
#     count = count + 1

# Right — use parameters and return
def increment(count):
    return count + 1

count = increment(count)
print(count)    # 1

# ─────────────────────────────────────────────
# Quick reference
# ─────────────────────────────────────────────

# define before calling — Python reads top to bottom
# parentheses required — greet() not greet
# print != return — print displays, return sends back
# return stops function — nothing after it runs
# default parameters always last
# use parameters and return — avoid modifying globals
