Created
April 16, 2015 16:49
-
-
Save sigriston/199ea09d967a37f1d405 to your computer and use it in GitHub Desktop.
Lexical Scoping
This file contains 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 y = 10; | |
/* forward declaration of g - required to compile w/o warnings */ | |
int | |
g(int); | |
int | |
f(int x) | |
{ | |
int y = 2; | |
return y ^ 2 + g(x); | |
} | |
int | |
g(int x) | |
{ | |
return x * y; | |
} | |
int | |
main() | |
{ | |
printf("f(3) is: %d\n", f(3)); /* will print "f(3) is: 34" */ | |
return 0; | |
} |
This file contains 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
public class Main { | |
static int y = 10; | |
static int f(int x) { | |
int y = 2; | |
return y ^ 2 + g(x); | |
} | |
static int g(int x) { | |
return x * y; | |
} | |
public static void main(String args[]) { | |
System.out.println("f(3) is: " + f(3)); // will print "f(3) is: 34" | |
} | |
} |
This file contains 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
var y = 10; | |
function f(x) { | |
var y = 2; | |
return y ^ 2 + g(x); | |
} | |
function g(x) { | |
return x * y; | |
} | |
console.log('f(3) is: ' + f(3)); // will print "f(3) is: 34" |
This file contains 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
#!/usr/bin/env python | |
# encoding: utf-8 | |
y = 10 | |
def f(x): | |
y = 2 | |
return y ^ 2 + g(x) | |
def g(x): | |
return x * y | |
print 'f(3) is:', f(3) # will print "f(3) is: 34" |
This file contains 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
y <- 10 | |
f <- function(x) { | |
y <- 2 | |
y ^ 2 + g(x) | |
} | |
g <- function(x) { | |
x * y | |
} | |
cat('f(3) is:', f(3)) # will print "f(3) is: 34" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment