Last active
October 21, 2017 03:39
-
-
Save the-vampiire/19915e92022b5abce6cf09cbdf0f1d4a to your computer and use it in GitHub Desktop.
Error Correction
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # This works | |
| def printHelloWorld(): | |
| print('hello word') | |
| def returnPrintHelloWorld(): | |
| return print('hello world') # output: syntax error | |
| """ | |
| To answer the original question: Why do you use a return statement on the return 5 | |
| function but not on the printHelloWorld() function? | |
| The printHelloWorld() function is calling another function. It happens to be that the | |
| inner function it is calling - print() - is a built in Python function that | |
| independently performs an "action" | |
| Whereas the other function is literally returning a variable. The "action" is | |
| returning a value | |
| File "/Users/Home/Documents/Programming Work/LaunchCode/Lectures/Lecture1/test.py", | |
| line 6 | |
| return print('hello world') # output: fails with syntax error | |
| ^ | |
| SyntaxError: invalid syntax | |
| This errors because the print() function does not return a value that | |
| the outer function can then return. | |
| print() is a built in Python function and does not return any value | |
| https://docs.python.org/3/library/functions.html#print | |
| See this example below to understand that functions can return other functions | |
| but only if the inner function returns an actual value | |
| We have a function called returnAnIntValue() which returns the integer 5 | |
| Because it returns a value it can be returned by another function | |
| The reason this works is because functions EVALUATE from the inside out | |
| This might be confusing but is actual intuitive. You are calling the functions | |
| from the outside in but then returning the values from the inside out as they | |
| are evaluated so the outermost function can make use of the innermost value | |
| Here are the steps occuring during the call on line 25 print(returnAFunction1()) | |
| Function Call Steps ("forward, out to in order": | |
| 1) call print() | |
| argument: returnAFunction1() | |
| 2) call returnAFunction1() | |
| return returnAnIntValue() | |
| 3) call returnAnIntValue() | |
| return 5 | |
| Function Evaluation Steps ("reverse, in to out order"): | |
| 1) evaluate innermost function call: returnAnIntValue() | |
| returns 5 | |
| 2) evaluate second innermost function call: returnAFunction1() | |
| returns returnAnIntValue() --( evaluated in step 1) )--> returns a 5 | |
| 3) evaluate outermost function call: print() | |
| print(returnAFunction1()) --( evaluated through steps 1) and 2) )--> print(5) | |
| The same can be shown for the print call on line 53, this time evaluating to a string | |
| """ | |
| def returnAnIntValue(): | |
| return 5 | |
| def returnAFunction1(): | |
| return returnAnIntValue() | |
| print(returnAFunction1()) # output: 5 | |
| def returnAStringValue(): | |
| return 'a string' | |
| def returnAFunction2(): | |
| return returnAStringValue() | |
| print(returnAFunction2()) # output: a string | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment