Menu
Coddy logo textTech

Return

Part of the Fundamentals section of Coddy's Python journey — lesson 45 of 77.

The return statement in a function is used to specify the value or values that the function should produce as its output.

For example, the following function will output 100:

def function_name():
	return 100

To pass the value to a variable, write:

number = function_name()

Now the number variable will hold 100 because this is what the function returned.

challenge icon

Challenge

Easy
  1. Create a function called square_number that takes a single parameter n and returns its square (n × n).
  2. Create a variable called input_num that takes input from the user (convert it to int using int()).
  3. Call the function with input_num as the argument, and store the returned value in a variable called result.
  4. Finally, print result.

Cheat sheet

The return statement specifies the output of a function:

def function_name():
    return 100

# To use the returned value:
number = function_name()
# number now holds 100

Try it yourself

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals