Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 5.1 — Defining and Calling Functions
Python · Example 2 of 2

Several return values and falling off the end

rectangle returns a tuple that is unpacked into two names. first_even returns None when no even number exists.

A function is a named block of code that you define once with def and run whenever you call it by name. It can receive values (parameters), and hand a value back to the caller with return; if it never reaches a return, it gives back None.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 6 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Write a function square(n) that returns n * n. Print square(7).

02

Write a function min_max(numbers) that returns the smallest and largest items of a list as two values. Unpack the result into low and high and print both.

03

Write a function describe(name, age) that returns the text Asha is 12 years old. for describe("Asha", 12). Print the result of calling it with your own values.

Explain it without notes

01

What is the difference between a parameter and an argument?

02

What does a function return if it has no return statement? Why does that matter?

03

What happens when Python reaches a def statement, and what happens when the function is called?

04

Why prefer return over print inside a function that computes something?

05

Is Python pass-by-value or pass-by-reference?

Several return values and falling off the end
Sign in to run this example in your browser.

Expected output

area: 12 perimeter: 14
both together: (12, 14)
8
None
None