← Dictionary
Code·basic

Function

A reusable block of code that does a job when you call it.

A function bundles up logic so you can reuse it.

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

print(greet("Stella"))   # Hello, Stella!
print(greet("Mosaic"))   # Hello, Mosaic!

def greet(name) defines a function that takes a parameter called name. The indented body is what runs when you call the function with greet("Stella").

Functions are how you stop writing the same code over and over. They're also how big programs stay readable — a well-named function tells you what it does without you reading the whole body.

Related