OpenViSTAI · Agents · Blockchain · Crypto
Menu

OPENVIST LEARN · FREE LESSONS

Functions, return values and scope

Give a repeated rule a name, accept arguments and return a result you can test.

Lesson 11 of 22 · About 20 minutes · Beginner · English

By the end of this lesson

Write a function with a clear input and output contract.

Before you start

  • Python 3.12 or a compatible newer Python 3 interpreter; no external packages.
  • Complete lesson 10 in this route, or be comfortable with its examples.

Three words to know

Parameter
A name in a function definition.
Argument
A value supplied in a function call.
Return value
The result sent back to the caller.

1. Separate calculation from display

A function begins with def. Its body runs when called, not merely when defined. return sends a value to the caller; print writes text to output. Returning data allows the same rule to be tested or displayed in different ways.

A reusable countPYTHON · save as lesson.py, then run with your interpreter
def count_open(statuses):
    return sum(1 for status in statuses if status == "open")

result = count_open(["open", "closed", "open"])
print(result)
What you should see · example output
2

2. Local names and default values

A name assigned inside a function is normally local to that call. Use explicit parameters instead of changing unrelated global values. Defaults such as a string or integer are straightforward; avoid a mutable list default because calls would share the same list.

A clear defaultPYTHON · save as lesson.py, then run with your interpreter
def greeting(name="learner"):
    message = f"Hello, {name}!"
    return message

print(greeting())
print(greeting("Sam"))
What you should see · example output
Hello, learner!
Hello, Sam!

3. State the contract

count_open accepts an iterable of statuses and counts exact lowercase matches. It does not validate that every value is a supported status. Decide whether a function validates input or assumes a caller already did. A short docstring can state that decision, expected results and possible errors.

YOUR TURN

A small check before you continue

Write double(number) that returns twice the argument. How can you verify it without printing inside the function?

Show a suggested answer

Define def double(number): return number * 2. Compare double(3) with 6 in a test. Keep display in the caller so the function remains reusable.

Sources and scope

Documentation and examples reviewed 2026-10-18. Provider screens, package versions and output wording can differ. Examples show expected outcomes; they are not a claim that OpenViST connected to your server or verified your setup.