Write reusable functions

Package a task into a function so it can be called with different values.

A function is a named block of reusable code. Parameters receive input values. return sends a result back to the caller; print() only displays a value.

Create a greeting function

python · static example

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

message = greet("Asha")
print(message)

The function accepts one parameter and returns a formatted string.

Sample output
Hello, Asha!

Labeled sample only — nothing was run on this page.

Knowledge check

What does `return` do in a function?

Pick an answer to check.