r/PythonLearning • u/autistic_lover214 • 24d 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 24d ago
If you're a visual learner, Bro Code has a very good explanation of how functions work.
2
u/PureWasian 24d ago edited 24d 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 + bnow 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 8it 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.
2
u/theprupletrainer 24d 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
2
u/Wuthering_depths 24d 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.
1
u/frnzprf 24d 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.)
1
u/fllthdcrb 23d ago
Math.sqrt(16)Just a nitpick. You have to import the
mathmodule before you can reference it. The name is all lowercase. If you want to refer to it asMath, a rebinding is necessary. Easy enough, but very slightly more work than using it as is.
1
u/frnzprf 24d ago
Translation between Python and English:
def square(x):
sum = 0
for count in range(x):
sum = sum + x
return sumDefinition 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".0
u/BranchLatter4294 24d ago
Use print statements to see what's going on inside your code. Learn by observing behavior.
0
u/ninhaomah 24d 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.
8
u/NotSeacombe 24d 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!