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 100To 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
Easy- Create a function called
square_numberthat takes a single parameter n and returns its square (n × n). - Create a variable called
input_numthat takes input from the user (convert it to int using int()). - Call the function with
input_numas the argument, and store the returned value in a variable calledresult. - 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 100Try it yourself
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Logical Operators Part 48Loops
For LoopWhile LoopBreakContinueRecap - FactorialThe Range FunctionNested LoopRecap - Dynamic Input3Operators Part 1
Arithmetic OperatorsModulo OperatorArithmetic ShortcutsRecap - Simple MathComparison Operators9Functions
Declare a FunctionArgumentsReturnRecap - Sigma FunctionRecap - Validation FunctionDefault Values12Iterating Over Sequences
Iterating Over ElementsThe Enumerate FunctionIterating Over Strings Part 1Iterating Over Strings Part 2