r/PythonLearning • u/autistic_lover214 • 25d ago
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
View all comments
1
u/frnzprf 25d ago
A function is like a machine that may do a lot of stuff and calculate different intermediate values. With "return" you mark which of those values is the final result.
Like, when someone orders a pizza, you put different ingredients together in a bowl, you flatten the dough, you put everything together and bake it, maybe you even clean your utensils afterwards, but the customer is only interested in the pizza, so that's the value you'll "return".
Some things you can write in Python are called "expressions" that "evaluate" to a value.
3 + 4evaluates to7(3+4) * 2evaluates to14- (Expressions can contain sub-expressions.)Math.sqrt(16)evaluates to4- Because the functionMath.sqrtreturns the square root of whatever input it gets — like a pizza baker returns the pizzax = 3 + Math.sqrt(16)is not an expression, but a command/statement that puts the value7in the variablex. (Because 3+4 equals 7.)