Skip to main content
Informatik.uz
Chapter 3. Functions and Modules

Functions That Return a Value: return

The return statement sends the result a function has calculated back to where it was called. With it we build complex programs from small functions.

Topic 9 of 111 min readUpdated October 27, 2026
All topics (9 of 11)

A function does not have to stop at printing; it can also return the result of a calculation. The return statement does this: inside def square(x): we write return x * x. When we call y = square(5), the result 25 is stored in y. The result can be used in further calculations: print(square(3) + square(4)).

As soon as return runs, the function ends and the lines after it are not executed. That makes it handy inside conditions: in def sign(x): we write if x > 0: return 1, if x < 0: return -1, and then return 0. If there is no return, the function returns None (no value).

Several values can be returned separated by commas: return a + b, a * b; we receive them as s, p = calc(3, 4). Logical functions return True or False, for example def is_even(n): return n % 2 == 0. Such a function can be used directly in a condition: if is_even(8):.

Functions can call one another. For example, max3(a, b, c) can use max2(a, b) inside. Splitting a complex task into small functions makes a program clearer and lets us test each part separately. Do not forget to write a short comment describing what the function does.

Key takeaways

  • return sends the result back to the caller and ends the function.
  • Without return a function returns None.
  • A function can return True/False and be used in a condition.
  • Functions call one another, splitting a complex task into parts.

Practice tasks

  1. Let def cube(x): return the cube of a number; check the result of print(cube(3)).
  2. Write a function bigger(a, b) that returns the larger of two numbers using if, and use it to find the largest of three numbers.
  3. Write a function is_prime(n) that returns True if n is prime and False otherwise (test divisibility by numbers from 2 to n-1).

Review questions

Test yourself: think first, then reveal the answer.

  1. 1What is the difference between `print()` and `return`?
    `print()` shows a value on the screen, while `return` gives it back to the program so the result can be stored in a variable and used later.
  2. 2Are the lines written after `return` executed?
    No, `return` ends the function immediately.

0 of 11 topics completed

Sign in to mark
Informatika 9-sinf darsligi muqovasi
In the textbookInformatics — Grade 9