r/PythonLearning • • Oct 23 '26

Help Request Def return

I'm stuck in def return in python I don't understand it very well why is it used and how and where does the code return to ? The caller ? What is exactly the caller ?

Thank you so much for helping me

If you don't mind can u help me understand with examples ? Couldn't understand this with Chatgpt and yt

0 Upvotes

21 comments sorted by

View all comments

1

u/frnzprf Oct 23 '26

Translation between Python and English:

def square(x):
    sum = 0
    for count in range(x):
        sum = sum + x
    return sum
Definition of the new command "square of x":
1. Store 0 in variable "sum".
2. Repeat the following steps "x" times:
    - Store the result of "sum + x" in "sum".
3. Treat the current content of "sum" as the result and jump back to the line where this command was invoked.

Calling a function:

abc = square(17)
print(abc)

# shorter: print(square(17))

English:

1. Store the result of the command "square(17)" in the variable "abc".
2. Print the contents of variable "abc".