Reminder About Functions (let ((z 17)) (let ((z 3) (a ) (x (lambda (x y) (- x (+ y z))))) (let ((z 0) (a )) (x z a)))) int h, i; void B(int w) { int j, k; i = 2*w; w = w+1; void A(int x, int y) { bool i, j; B(h); parameters and local variables int main() { Return address int a, b; Saved registers h = ; a = 3; b = 2; Temporary variables A(a, b); Return value Dr. Philip Cannata 1
Calling function: factorial BasePtr = 3, printing runtime stack Calling function: factorial BasePtr =, printing runtime stack Calling function: factorial BasePtr = 7, printing runtime stack Calling function: factorial BasePtr = 9, printing runtime stack n: 0 Hmm Runtime Stack for Factorial 3 int factorial(int n) { if(n < 1) { return 1; else { return n * factorial(n - 1); int main() { int number, answer; number = 3; answer = factorial(number); print(answer); BasePtr = 9, printing runtime stack n: 0 BasePtr = 7, printing runtime stack BasePtr =, printing runtime stack return#factorial: 2 BasePtr = 3, printing runtime stack return#factorial: 2 return#factorial: 6 Dr. Philip Cannata 2
A Taxonomy of Functions 6.1 A Taxonomy of Functions first-order Functions are not values in the language. They can only be defined in a designated portion of the program, where they must be given names for use in the remainder of the program. The functions in F1WAE are of this nature, which explains the 1 in the name of the language. higher-order Functions can [be defined anywhere in a program and] return other functions as values. first-class Functions are values with all the rights of other values. In particular, they can be supplied as the value of arguments to functions, returned by functions as answers, and stored in data structures. [They can also be defined anywhere in a program.] Dr. Philip Cannata 3
Functions in Python/Jython http://www.penzilla.net/tutorials/python/functions/ Python and Functions: Python is a little different from other languages like C/C++ and Java, in that functions are not magic constructs, but values. Yes, functions in Python, are like integers in C. They are simply values and can be passed to other functions/object constructors, and so forth. We've already seen a pretty simple example of a Python function in the form of the venerable "Hello World!" program. Let's take another look at it: # hello.py def hello(): print "Hello World! return Dr. Philip Cannata 4
$ cat cs34.py x=3+4 def h (): print 'Hello' h h() print (lambda x, y : x + y)(2, 3) $ dist/bin/jython cs34.py Hello AST Functions in Python/Jython $ dist/bin/jython ast/astview.py cs34.py ('Module', ('body', ('Assign (1,0)', ('targets', ('Name (1,0)', ('id', 'x'), ('ctx', ('Store',)))), ('value', ('BinOp (1,2)', ('left', ('Num (1,2)', ('n', 3))), ('op', ('Add',)), ('right', ('Num (1,4)', ('n', 4)))))), ('FunctionDef (2,0)', ('name', 'h'), ('args', ('arguments', ('args',), ('vararg', None), ('kwarg', None), ('defaults',))), ('body', ('Print (3,3)', ('dest', None), ('values', ('Str (3,9)', ('s', 'Hello'))), ('nl', True))), ('decorator_list',)), ('Expr (4,0)', ('value', ('Name (4,0)', ('id', 'h'), ('ctx', ('Load',))))), ('Expr (,0)', ('value', ('Call (,0)', ('func', ('Name (,0)', ('id', 'h'), ('ctx', ('Load',)))), ('args',), ('keywords',), ('starargs', None), ('kwargs', None)))), ('Print (6,0)', ('dest', None), ('values', ('Call (6,6)', ('func', ('Lambda (6,7)', ('args', ('arguments', ('args', ('Name (6,)', ('id', 'x'), ('ctx', ('Param',))), ('Name (6,17)', ('id', 'y'), ('ctx', ('Param',)))), ('vararg', None), ('kwarg', None), ('defaults',))), ('body', ('BinOp (6,21)', ('left', ('Name (6,21)', ('id', 'x'), ('ctx', ('Load',)))), ('op', ('Add',)), ('right', ('Name (6,2)', ('id', 'y'), ('ctx', ('Load',)))))))), ('args', ('Num (6,28)', ('n', 2)), ('Num (6,31)', ('n', 3))), ('keywords',), ('starargs', None), ('kwargs', None))), ('nl', True)))) Dr. Philip Cannata
"let" Can Be Made Redundant In the textbook, the author says the following: Now that we have functions and function invocation as two distinct primitives, we can combine them to recover the behavior of "with" as a special case. Every time we encounter an expression of the form {with {var named body we can replace it with {{fun {var body named Example: In DrRacket: > (calc (parse '{with {x {+ {with {y {- x 3 {+ y y) ) In jython: >>> (lambda x : (lambda y : y + y) (x - 3) ) ( + ) Dr. Philip Cannata 6
"let" Can Be Made Redundant DrRacket (see Textbook, Chapter 3) v. Let in jython as in Homework #6 Welcome to DrRacket, version.0.2 [3m]. Language: plai; memory limit: 128 MB. > (calc (parse '{with {x {+ {+ x x) ) 20 > (calc (parse '{with {x {+ x x) ) > (calc (parse '{with {x {+ {with {y {- x 3 {+ y y) ) > (calc (parse '{with {x {+ x {with {x 3 x) ) 8 > (calc (parse '{with {x {+ x {with {x 3 ) ) 1 > (calc (parse '{with {x {+ x {with {y 3 x) ) > (calc (parse '{with {x {with {y x y) ) > (calc (parse '{with {x {with {x x x) ) $ dist/bin/jython Jython 2..2rc4 (trunk, Feb 27 2011, 21:3:17) >>> (let x: +: x+x)() 20 >>> (let x: : x+x)() >>> (let x: +:(let y: x-3: y+y)())() >>> (let x: : x + (let x: 3: x)())() 8 >>> (let x: : x + (let x: 3: )())() 1 >>> (let x: : x + (let y: 3: x)())() >>> (let x: : (let y: x: y)())() >>> (let x: : (let x: x: x)())() Dr. Philip Cannata 7