diff options
author | 2022-11-27 12:13:53 -0500 | |
---|---|---|
committer | 2022-11-27 17:13:53 +0000 | |
commit | e4cf98f6ee423a32f62ee470dc7bd798288f636c (patch) | |
tree | a2cd58675d6ef553b9b1ab183bb3a3530ae2802d | |
parent | Merge pull request #2342 from python-discord/don't-close-already-closed-posts (diff) |
Update return.md (#2325)
Made the tag more brief without any substantial changes to its overall approach.
Co-authored-by: wookie184 <[email protected]>
-rw-r--r-- | bot/resources/tags/return.md | 19 |
1 files changed, 8 insertions, 11 deletions
diff --git a/bot/resources/tags/return.md b/bot/resources/tags/return.md index e37f0eebc..1d65ab1ae 100644 --- a/bot/resources/tags/return.md +++ b/bot/resources/tags/return.md @@ -1,27 +1,25 @@ **Return Statement** -When calling a function, you'll often want it to give you a value back. In order to do that, you must `return` it. The reason for this is because functions have their own scope. Any values defined within the function body are inaccessible outside of that function. - -*For more information about scope, see `!tags scope`* +A value created inside a function can't be used outside of it unless you `return` it. Consider the following function: ```py def square(n): - return n*n + return n * n ``` -If we wanted to store 5 squared in a variable called `x`, we could do that like so: +If we wanted to store 5 squared in a variable called `x`, we would do: `x = square(5)`. `x` would now equal `25`. **Common Mistakes** ```py >>> def square(n): -... n*n # calculates then throws away, returns None +... n * n # calculates then throws away, returns None ... >>> x = square(5) >>> print(x) None >>> def square(n): -... print(n*n) # calculates and prints, then throws away and returns None +... print(n * n) # calculates and prints, then throws away and returns None ... >>> x = square(5) 25 @@ -29,7 +27,6 @@ None None ``` **Things to note** -• `print()` and `return` do **not** accomplish the same thing. `print()` will only print the value, it will not be accessible outside of the function afterwards. -• A function will return `None` if it ends without reaching an explicit `return` statement. -• When you want to print a value calculated in a function, instead of printing inside the function, it is often better to return the value and print the *function call* instead. -• [Official documentation for `return`](https://docs.python.org/3/reference/simple_stmts.html#the-return-statement) +• `print()` and `return` do **not** accomplish the same thing. `print()` will show the value, and then it will be gone. +• A function will return `None` if it ends without a `return` statement. +• When you want to print a value from a function, it's best to return the value and print the *function call* instead, like `print(square(5))`. |