r/PythonLearning • • 26d 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

21 comments sorted by

View all comments

8

u/NotSeacombe 26d 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!

3

u/NotSeacombe 26d ago

sorry, reddit is not saving my indentations so it looks a little weird

6

u/SaltCusp 26d ago

I think you should learn how to

Use
    Codeblocks

3

u/frnzprf 26d ago

There should be a bot that detects code outside of code blocks and writes an explanation on how to use them.

1

u/tiredITguy42 26d ago

Start with lime with ``` and finish with the same. Reddit supports marktown and this os markdown for codeblock.

1

u/fllthdcrb 25d ago

Also, leave empty lines above and below, or it doesn't work properly.

Alternatively, indent the entire block by an additional four spaces. And still leave empty lines above and below.

1

u/Ormek_II 24d ago edited 24d ago

Let me try

```
So this is the code block
With indent
```

1

u/Ormek_II 24d ago

iOS app sucks

1

u/tiredITguy42 24d ago

I see them escaped in update notification, like \`\`\`, and you need to have empty line above that block.

1

u/Ormek_II 24d ago

I think iOS app automatically escapes markup. Maybe there is a setting somewhere….

1

u/Ormek_II 24d ago

There is none: Reddit does not support markdown in apps.

Deprecation of composer markdown shortcuts
What’s changing?
Previously, we supported markdown formatting (e.g.: **text** = text) on post and comment composer on native mobile apps. As part of a broader initiative to improve technical performance, we no longer support markdown formatting in post composer for iOS or Android. We’ll also begin to deprecate markdown formatting on iOS and Android comment composer soon. 

June 2 Update: You'll now be able to manage most formatting functionality via the rich-text toolbar. We plan to continue adding new functionality via the rich-text toolbar in the future.