Hintor
Python Basics for Programmers

Functions and return

Python for Developers/Python Basics for Programmers/

Functions and return

Python

Lesson explanation

Functions and return


🎯 Learning Objective

After this lesson, you will be able to:

  • Define small functions with clear parameters.
  • Distinguish function parameters from the arguments supplied by a caller.
  • Return strings, numbers, and booleans that other code can reuse or test.
  • Use conditional and early returns to keep function logic clear.

Short Explanation

A function packages reusable behavior behind a name. Define it with def, list the required parameters inside the parentheses, and indent the function body:

Python
def calculate_total(price, quantity):
    return price * quantity

price and quantity are parameters. The values supplied in a call such as calculate_total(12.5, 4) are arguments.

The most important distinction in this lesson is return versus print(). print() displays a value, but return sends a result back to the caller. Returned values can be stored, compared, formatted, passed to another function, or checked by automated tests.

A function can return different kinds of values, including strings, numbers, and booleans. When a condition identifies a final result, an early return can end the function immediately and avoid unnecessary nesting.

If execution reaches the end of a function without a return, Python returns None.

💡 Tip: In a function_task, Hintor calls your function with several test cases. Return the result; printed text does not replace a returned value.


Clean Code Examples

1. Return a numeric result

The caller receives the calculated value and can reuse it.

Python
def estimate_storage(file_size_mb, copies):
    return file_size_mb * copies

required_mb = estimate_storage(18, 3)
print(f"Storage: {required_mb} MB")

Output:

TEXT
Storage: 54 MB

The function calculates; the caller decides how to display the result.

2. Return a formatted string

Parameters make the helper reusable for different values.

Python
def format_environment(name, region):
    return f"{name.lower()}@{region.upper()}"

print(format_environment("Staging", "eu"))

Output:

TEXT
staging@EU

The returned string is built from both arguments.

3. Return a boolean rule

A comparison can be returned directly.

Python
def is_within_limit(used, limit):
    return used <= limit

print(is_within_limit(72, 100))

Output:

TEXT
True

The result is a boolean, not the text "True".

4. Exit early when a rule is final

An early return handles the blocking case before the normal result.

Python
def deployment_status(failed_checks, is_frozen):
    if is_frozen:
        return "frozen"

    if failed_checks > 0:
        return "blocked"

   

Output:

TEXT
ready

Once Python reaches a return, the function ends immediately.



⚠️ Common Mistakes

  1. Do not print when the task expects a returned value.
  2. Do not hardcode one visible test result; the parameters must control the answer.
  3. Do not forget that code after a reached return will not run.
  4. Do not let every branch fall through without returning when the caller expects a value.
Start Practice

Python for Developers

Functions and return

6 tasks
Lesson progress0 / 6 tasks completed

Tasks

Practice progress

Complete the returned service label

View task

Complete the function so the caller receives the formatted service label.

Requirements

  • Keep the function name, parameter, and f-string unchanged.
  • Fill only the missing keyword.

Expected output

Service: worker
Code: PythonTask 1

Complete the returned service label

Change the starter code or fill in the blanks, then run it before asking Hintor.

1
2
3
4
5

Assembled code

Preview of the code that will run

Generated code is hidden by default. Expand it only if you want to inspect what your current blank values produce.

Output / ConsolePreparing Python runtime...

0 / 6 tasks completed

Previous lessonBack to unitBack to track
return
"ready"
print(deployment_status(0, False))
GoalPython
Define reusable behaviordef function_name(parameters):
Send a result to the callerreturn value
Display a value onlyprint(value)
End a function when a rule is finalEarly return
No explicit returned valuePython returns None