Python Python Basics Functions and Looping Create a Function

Samith Vijayan
PRO
Samith Vijayan
Pro Student 5,951 Points

Why is this code wrong ? def square(number): return number * number result = return square (3)

squaring.py
def square(number):
     return  number * number
     result = return
 square (3)

2 Answers

So when you return something from a function, that allows you to use whatever you returned later on. In other words, "return" isn't used like a variable that stores something, it performs an action (which is why you got an error when you tried to store return inside a variable).

People most often will store whatever action they want to perform in a variable, and then return that variable. So if you stored number * number inside a variable, you could then return that variable whenever you run a function.

Try:

def square(number):
    result = number * number
    return  result
square(3)
KRIS NIKOLAISEN
PRO
KRIS NIKOLAISEN
Pro Student 49,951 Points

To store the return value of a function in a variable, do it like you would any other variable assignment. Place the variable on the left hand side, use the assignment operator, then the function call on the right hand side.

def square(number):
     return  number * number

result = square(3)