Technical skill practice
How to Create a Function in Python: Parameters and return
Create a function in Python with def, a name, parameters in parentheses and an indented body. Use return to give a value to the caller. Printing a value displays it but does not automatically return that same value, so keep calculation and presentation distinct.
Start with a small, explicit task
Write a pure conversion from nonnegative whole minutes to seconds. Check two inputs and reject a negative value. Then print one returned result outside the function. The example’s input contract is deliberately narrow; it does not attempt to infer units or parse arbitrary user text.
Run the complete example
This original example was checked with Python 3.12.14. Save it as python_create_function.py and run it with Python 3.12. It uses only the standard library. The assertions check the stated results.
Expected output
180 seconds
calculation returned: 180
def minutes_to_seconds(minutes):
if minutes < 0:
raise ValueError("minutes must be nonnegative")
return minutes * 60
result = minutes_to_seconds(3)
assert result == 180
assert minutes_to_seconds(0) == 0
try:
minutes_to_seconds(-1)
except ValueError:
rejected = True
else:
raise AssertionError("expected negative input rejection")
assert rejected
def show_seconds(seconds):
print(f"{seconds} seconds")
printed_return = show_seconds(result)
assert printed_return is None
print("calculation returned:", result)
Explain the result
minutes is the parameter inside the function, while 3 is the argument supplied by the first call. return ends the calculation and makes 180 available to result. A second call works independently with zero. The negative call takes a deliberate exception path instead of producing an invalid conversion under the stated nonnegative rule.
show_seconds prints a label but reaches the end without an explicit return, so its return value is None. The displayed text and the returned value are different things. The conversion function can therefore be reused in a calculation or assertion without forcing console output on every caller.
Avoid these mistakes
Defining a function does not execute its body; call it with the required arguments. Keep indentation consistent so the return belongs to the function. This example assumes whole numeric minutes as stated; validate types or parse strings at the boundary when your actual caller has a wider input contract.
Questions, answered
Why is my function result None?
If the function reaches its end without returning a value, it returns None, as show_seconds demonstrates.
Does def run the calculation immediately?
No. The calculation runs when minutes_to_seconds is called with an argument.
Sources and examples
ResumizeAI team. Examples are fictional teaching material, not customer results. Replace sample details with your own facts.
Your experience. A complete resume.
Build in Resumize with ATS-friendly templates, guided editing and a live preview. Start free, then upgrade when you need more.
Free account required. Free-plan PDF exports include a watermark.