Command Palette

Search for a command to run...

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

return versus print

greet gives back a value. show_greeting only prints, so what it returns is None.

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?

return versus print
Sign in to run this example in your browser.

Expected output

greet returned: Hello, Asha!
Hello, Ravi!
show_greeting returned: None
Docstring: Return a greeting for the given name.