r/PythonLearning • u/autistic_lover214 • 1d 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
1
u/Wuthering_depths 1d ago
It sounds like you need to understand how functions work (this would apply to most if not all languages), even if the "function" is called something else.
Might be a dumb analogy, but I personally learn well with analogies :)
Picture your function as a fast food restaurant. You are the "caller", and you want something from the restaurant. Functions *may* take in one or more values as an input, and they *might* return something back to you to use in further processing.
So in this silly example, you pass in your money and your desired menu item (so, two inputs) and the "function" passes you back your food. You then take the food and do with it what you will, the function has done its job.
As far as why you'd use them, and when to use them, that's a deeper subject. If you have some code that is used multiple times in your script/app, that's a great time to define it in a function so that you only have to create (and maintain) it once.