Skip to content

Instantly share code, notes, and snippets.

@myw
Last active August 29, 2015 14:26
Show Gist options
  • Select an option

  • Save myw/178165ff5002915e0ad3 to your computer and use it in GitHub Desktop.

Select an option

Save myw/178165ff5002915e0ad3 to your computer and use it in GitHub Desktop.
Examples of function scoping in python, C, and JavaScript
#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;
}
#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;
}
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();
}
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();
}
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))
@myw
Copy link
Author

myw commented Aug 12, 2015

There are no scoping changes planned for Py3 as far as I can tell. ECMAScript 6, however, is planning on adding a let keyword, which would work normally, unlike that insane var nonsense: all curly braces would create local scope for variables declared with let.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment