Presentation is loading. Please wait.

Presentation is loading. Please wait.

Reminder About Functions

Similar presentations


Presentation on theme: "Reminder About Functions"— Presentation transcript:

1 Reminder About Functions
(let ((z 17)) (let ((z 3) (a 5) (x (lambda (x y) (- x (+ y z))))) (let ((z 50) (a 5)) (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); int main() { int a, b; h = 5; a = 3; b = 2; A(a, b); parameters and local variables Return address Saved registers Temporary variables Return value

2 Hmm Runtime Stack for Factorial 3
Calling function: factorial BasePtr = 3, printing runtime stack null: null n: 3 answer: null number: 3 BasePtr = 5, printing runtime stack n: 2 BasePtr = 7, printing runtime stack n: 1 BasePtr = 9, printing runtime stack n: 0 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); Exiting function: factorial BasePtr = 9, printing runtime stack null: null n: 0 return#factorial: 1 n: 1 n: 2 n: 3 answer: null number: 3 BasePtr = 7, printing runtime stack BasePtr = 5, printing runtime stack return#factorial: 2 BasePtr = 3, printing runtime stack return#factorial: 6

3 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.]

4 Functions in Python/Jython
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

5 Functions in Python/Jython
$ cat cs345.py x=3+4 def h ():    print 'Hello' h h() print (lambda x, y : x + y)(2, 3) $ dist/bin/jython cs345.py MyPrint: MyPrint: Hello 5 $ dist/bin/jython ast/astview.py cs345.py MyPrint: MyPrint: ('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 (5,0)',    ('value',     ('Call (5,0)',      ('func', ('Name (5,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,14)', ('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,25)', ('id', 'y'), ('ctx', ('Load',)))))))),      ('args', ('Num (6,28)', ('n', 2)), ('Num (6,31)', ('n', 3))),      ('keywords',),      ('starargs', None),      ('kwargs', None))),    ('nl', True)))) AST

6 "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 {+ 5 5}} {with {y {- x 3}} {+ y y}}}) ) 14 In jython: >>> (lambda x : (lambda y : y + y) (x - 3) ) (5 + 5) 14

7 "let" Can Be Made Redundant
DrRacket (see Textbook, Chapter 3) v. Let in jython as in Homework #6 Welcome to DrRacket, version [3m]. Language: plai; memory limit: 128 MB. > (calc (parse '{with {x {+ 5 5}} {+ x x}}) ) 20 > (calc (parse '{with {x 5} {+ x x}}) ) 10 > (calc (parse '{with {x {+ 5 5}} {with {y {- x 3}} {+ y y}}}) ) 14 > (calc (parse '{with {x 5} {+ x {with {x 3} x}}}) ) 8 > (calc (parse '{with {x 5} {+ x {with {x 3} 10}}}) ) 15 > (calc (parse '{with {x 5} {+ x {with {y 3} x}}}) ) 10 > (calc (parse '{with {x 5} {with {y x} y}}) ) 5 > (calc (parse '{with {x 5} {with {x x} x}}) ) 5 $ dist/bin/jython Jython 2.5.2rc4 (trunk, Feb , 21:35:17) >>> (let x: 5+5: x+x)() 20 >>> (let x: 5: x+x)() 10 >>> (let x: 5+5:(let y: x-3: y+y)())() 14 >>> (let x: 5: x + (let x: 3: x)())() 8 >>> (let x: 5: x + (let x: 3: 10)())() 15 >>> (let x: 5: x + (let y: 3: x)())() 10 >>> (let x: 5: (let y: x: y)())() 5 >>> (let x: 5: (let x: x: x)())() 5


Download ppt "Reminder About Functions"

Similar presentations


Ads by Google