🐍 Python 2026 › Lesson 11: Functions
Lesson 11 of 30

Functions

Defining reusable blocks of code, parameters, return values, and scope.

Defining a Function

Functions let you encapsulate logic and reuse it. Use the def keyword:

def greet(name):
    print(f"Hello, {name}!")

greet("Alice")
greet("Bob")

Return Values

def add(a, b):
    return a + b

result = add(3, 4)
print(result)  # 7

Default Parameters

def power(base, exp=2):
    return base ** exp

print(power(5))     # 25 (exp defaults to 2)
print(power(2, 10))  # 1024

*args and **kwargs

def total(*numbers):
    return sum(numbers)

print(total(1, 2, 3, 4))  # 10

def profile(**info):
    for k, v in info.items():
        print(f"  {k}: {v}")

profile(name="Eve", age=28, city="KL")

Variable Scope

x = "global"

def show_scope():
    x = "local"
    print(x)   # local

show_scope()
print(x)       # global