Last active
August 29, 2015 14:26
-
-
Save myw/178165ff5002915e0ad3 to your computer and use it in GitHub Desktop.
Examples of function scoping in python, C, and JavaScript
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
| #include <stdio.h> | |
| int func(int, int, int); | |
| int | |
| main() | |
| { | |
| printf("%d\n", func(3, 4, 5)); | |
| return 0; | |
| } | |
| int | |
| func(int a, int b, int c) | |
| { | |
| int d, h; | |
| d = 7; | |
| h = 3; | |
| while (h < 5) { | |
| int d = 1; | |
| h++; | |
| d++; | |
| } | |
| return d; | |
| } |
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
| #include <stdio.h> | |
| int func(int, int, int); | |
| int | |
| main() | |
| { | |
| printf("%d\n", func(3, 4, 5)); | |
| return 0; | |
| } | |
| int | |
| func(int a, int b, int c) | |
| { | |
| int d, h; | |
| d = 7; | |
| h = 3; | |
| while (h < 5) { | |
| d = 1; | |
| h++; | |
| d++; | |
| } | |
| return d; | |
| } |
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
| function func(a, b, c) { | |
| var d, h; | |
| d = 7; | |
| h = 3; | |
| while (h < 5) { | |
| var d = 1; | |
| h++; | |
| d++; | |
| } | |
| return d; | |
| } | |
| function main() { | |
| console.log(func(3, 4, 5)); | |
| } | |
| if (require.main === module) { | |
| main(); | |
| } |
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
| function func(a, b, c) { | |
| var d, h; | |
| d = 7; | |
| h = 3; | |
| while (h < 5) { | |
| d = 1; | |
| h++; | |
| d++; | |
| } | |
| return d; | |
| } | |
| function main() { | |
| console.log(func(3, 4, 5)); | |
| } | |
| if (require.main === module) { | |
| main(); | |
| } |
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
| def func(a, b, c): | |
| d = 7 | |
| h = 3 | |
| while h < 5: | |
| d = 1 | |
| h += 1 | |
| d += 1 | |
| return d | |
| if __name__ == '__main__': | |
| print(func(3, 4, 5)) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There are no scoping changes planned for Py3 as far as I can tell. ECMAScript 6, however, is planning on adding a
letkeyword, which would work normally, unlike that insanevarnonsense: all curly braces would create local scope for variables declared withlet.