Skip to content

Instantly share code, notes, and snippets.

@sigriston
Created April 16, 2015 16:49
Show Gist options
  • Save sigriston/199ea09d967a37f1d405 to your computer and use it in GitHub Desktop.
Save sigriston/199ea09d967a37f1d405 to your computer and use it in GitHub Desktop.
Lexical Scoping
#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;
}
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"
}
}
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"
#!/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"
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