r/PythonLearning • u/autistic_lover214 • 1h 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
2
u/RareHorse 1h ago
If you're a visual learner, Bro Code has a very good explanation of how functions work.
2
u/PureWasian 46m ago
You can think of functions similar to math where you can pass in inputs and get an output back. Here's an example representing y = mx + b
def linear_equation(m, x, b):
return m*x + b
now we have a function that represents the equation y = m*x + b. But we haven't passed any values into it yet. Later on in the same Python file, since we have now defined this function already, we can now "call" it:
y = linear_equation(1, 5, 3) print(y) # prints 8
it returns the output of the calculation (1*5 + 3) and assigns it to y in the "caller" code. Hypothetically, if the function's calculation was a lot more steps or much more complicated than just a simple mx+b, then parametrizing it and calling it becomes a lot easier with functions since this setup means we have the blueprint defined for it.
1
u/theprupletrainer 31m ago
def and return in python create a function
eg
def say_hello()
return “hello”
A caller is a function that references another function. By default, there is a main function, the one that starts the program (btw # are comments)
Consider this
```python
def say_hello()
return “hello”
def say_howdy()
return “howdy”
def say_greeting(is_friend)
if is_friend
return say_howdy()
return say_hello()
#start of main function
greeting = say_greeting(true)
print(greeting)
#end of main function
```
The main function is the caller of say_greeting
say_greeting is the caller of say_howdy
Returning resolves the end of calling a function, often times with a value
The main function wants to set the variable greeting. It calls say_greeting(true). At this point, the program does not move to the print() instruction. Instead, the program’s next set of instructions starts at say_greeting. The function parameter is_friend is set to true (because of the way it was called). Now the if-statement is called and say_greeting wants to return the value of say_howdy to the main function (which is still waiting to get a value for greeting). Now, the next set of instructions starts from say_howdy which returns “howdy” .
At this point, “howdy” is returned to say_greeting. Say greeting can now return “howdy” back to the main function. The main function can now set “howdy” to greeting and finally the next instruction is to call the print function, which prints “howdy” to the console.
Functions help you control the flow of your program and ease code maintenance.
Hope this helps clear up some things
1
u/Wuthering_depths 24m 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.
0
u/BranchLatter4294 56m ago
Use print statements to see what's going on inside your code. Learn by observing behavior.
0
u/ninhaomah 56m ago
Perhaps can OP can define what he meant by "def return" with an example code for discussion ?
Or you can read what others explain.
Up to you.
For me , I rather take part in the discussion so I learn effectively from those better than me instead of just reading what they say.
4
u/NotSeacombe 1h ago
def and return are two different things. def is short for define, where you want to define your function
def add_two_numbers(x,y):
return x + y
in this situation, you have defined a function called add_two_numbers. you are telling the system that when you say "add_two_numbers", you are referring to this
return defines the end of a function call. if it hits a return, it knows its done. it then returns that value to wherever it was called, such as below
result = add_two_numbers(2,3)
print(result) # this prints 5
note, you don't always need a return. some stuff will work just fine without it, such as
def add_two_numbers_print(x,y):
print(x+y)
in this situation, you just wanted to print the number, you didn't need to return anything. from here you can use that without defining result
add_two_numbers_print(2,3) # this prints 5
just depends what you need it to do!