Pylerno
English

Defining functions

Separate a reusable calculation from displaying its result.

On this page

The concept

A function groups a named operation. def creates the function; its body runs only when called. Parameters are names in the definition, while arguments are values supplied by a caller. return sends a value back and ends the current call.

Printing is a side effect, not a replacement for returning a result. A calculation that returns a value can be reused in another calculation, saved to a file, or tested. A function without an explicit return value returns None. Notice how the area function and greeting function deliberately have different contracts.

Worked example

python
def rectangle_area(width, height):
    return width * height


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


area = rectangle_area(5, 3)
print("Area:", area)
print("Twice the area:", area * 2)
result = greet("Sara")
print("Return value without return:", result)

Trace the result

  1. rectangle_area multiplies two inputs and returns the product.
  2. The caller stores the product and uses it again to calculate twice the area.
  3. greet displays text but returns None, which becomes visible when result is printed.
text
Area: 15
Twice the area: 30
Hello Sara
Return value without return: None
Line-by-line walkthrough

Lines 1–2: def rectangle_area(width, height):

Define the function rectangle_area with input names width, height. Definition stores the operation rather than executing its body. An ordinary call executes a normal function; a coroutine runs when awaited/scheduled, and a generator advances when consumed. Decorators can provide a context-manager or wrapper interface. Indented statements below belong to this operation.

Line 2: return width * height

Evaluate width * height and return that value to the caller. This ends the current function call; subsequent statements on this execution path do not run.

Lines 5–6: def greet(name):

Define the function greet with input names name. Definition stores the operation rather than executing its body. An ordinary call executes a normal function; a coroutine runs when awaited/scheduled, and a generator advances when consumed. Decorators can provide a context-manager or wrapper interface. Indented statements below belong to this operation.

Line 6: print(f"Hello {name}")

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Line 9: area = rectangle_area(5, 3)

Evaluate rectangle_area(5, 3), then bind or update area with the result. The call finishes before its return value is bound; a failed call does not produce a successful assignment. Assignment itself does not imply a deep copy.

Line 10: print("Area:", area)

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Line 11: print("Twice the area:", area * 2)

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Line 12: result = greet("Sara")

Evaluate greet('Sara'), then bind or update result with the result. The call finishes before its return value is bound; a failed call does not produce a successful assignment. Assignment itself does not imply a deep copy.

Line 13: print("Return value without return:", result)

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Common mistake

If a calculation only prints its answer, other functions cannot obtain the answer as a return value. Keep display decisions outside the calculation.

Check your understanding

What does this program print?

python
def twice(n):
    return n * 2
print(twice(4) + 1)
Choose one answer

Search documentation

Share this page