Skip to content
Snippets Groups Projects

Print vs. return example

  • Clone with SSH
  • Clone with HTTPS
  • Embed
  • Share
    The snippet can be accessed without any authentication.
    Authored by uhrig
    Edited
    print_vs_return_example.md 4.28 KiB
    # Print vs. return example This example demonstrates the different use case of the `print` function vs. the `return` statement - a difficulty that many programming beginners face. You should already know about functions. This is not a tutorial. ## Introduction Have a quick look at the following code snippet. Don't spend too much time on it, just get a rough overview: ```python number = 2 deviation = 0.0001 root = 0 a = 0 b = number while abs(number - root ** 2) > deviation: root = (a + b) / 2 if root ** 2 < number: a = root else: b = root print(root) ``` You should be able to identify - a few variables `number`, `deviation`, `root`, `a` and `b`, - a [`while` loop statement](https://docs.python.org/3/reference/compound_stmts.html#the-while-statement), - an [`if` conditional statement](https://docs.python.org/3/reference/compound_stmts.html#the-if-statement) - and, finally, a call to the [built-in `print` function](https://docs.python.org/3/library/functions.html#print). ## Abstraction of "some code" All lines combined, the code is using the [bisection method](https://en.wikipedia.org/wiki/Bisection_method) to numerically calculate an approximation of the square root of 2. However, (luckily) that's almost completely irrelevant for this example. You don't need to understand anything about the calculation algorithm. What you need to take away from this is, that "some code" is being executed to calculate the square root of 2. This works fine and prints: ``` 1.4141845703125 ``` However, at some point you might want your code to also calculate the square root of 3. You recognize that you can just re-use most of the code and only need to change the `number` variable. So now you have this: ```python number = 2 deviation = 0.0001 root = 0 a = 0 b = number while abs(number - root ** 2) > deviation: root = (a + b) / 2 if root ** 2 < number: a = root else: b = root print(root) number = 3 deviation = 0.0001 root = 0 a = 0 b = number while abs(number - root ** 2) > deviation: root = (a + b) / 2 if root ** 2 < number: a = root else: b = root print(root) ``` It works as expected and prints: ``` 1.4141845703125 1.7320404052734375 ``` But the code doesn't look very clean anymore. It's a lot to read and it is repeating itself, violating the [DRY coding principle](https://en.wikipedia.org/wiki/Don%27t_repeat_yourself). So what we're going to do is to abstract "some code" into a function. And because "that code" is calculating the square root, we're going to name this function `sqrt`: ```python def sqrt(number, deviation=0.0001): root = 0 a = 0 b = number while abs(number - root ** 2) > deviation: root = (a + b) / 2 if root ** 2 < number: a = root else: b = root print(root) sqrt(2) sqrt(3) ``` This works exactly like the code before and it looks much cleaner. ## Using the `return` statement As a next step, you might want to print 5 times the square root of 2 or the square root of 3 plus 10. It seems ridiculous to have separate functions for this like: ```python def five_times_sqrt(number, deviation=0.0001): ... print(5 * root) def sqrt_plus_ten(number, deviation=0.0001): ... print(root + 10) five_times_sqrt(2) sqrt_plus_ten(3) ``` That's why we're going to remove the `print` call from our `sqrt` function: That function is now doing _only_ the calculation of `root`. The printing will happen "outside", later. To get the calculated value from "inside" the function to "outside" where the `print` will be, we have to use the `return` statement. Because `print` has been removed "inside" the function, we will add it on the "outside" and pass the returned value of our function. The code is now finished and looks like this: ```python def sqrt(number, deviation=0.0001): root = 0 a = 0 b = number while abs(number - root ** 2) > deviation: root = (a + b) / 2 if root ** 2 < number: a = root else: b = root return root print(5 * sqrt(2)) print(sqrt(3) + 10) ``` ## Conclusion Use the built-in `print` function to print a value - actually making it show up in the terminal. Use the `return` statement to return a value from a function - for example to pass it into the `print` function.
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Finish editing this message first!
    Please register or to comment