githubEdit

Functions

Learn what functions are, how to define them, and how return values work.

Functions group reusable logic into a named block.

Defining a Function

def greet(name: str) -> None:
    """Print a friendly greeting."""
    print(f"Hello, {name}!")

Calling a Function

greet("Paul")

Return Values

def absolute_value(n: int) -> int:
    if n >= 0:
        return n
    return -n

print(absolute_value(-4))  # 4

If you omit return, Python returns None.

Scope (Local vs Global)

Types of Functions

  • Built-in functions (e.g., print, len)

  • User-defined functions (your own)

Next | Previous

Last updated