Lecture 2 Names Functions 09/25 Slides adapted from Berkeley CS61a
Lecture 2 - Names & Functions 09/25 Slides adapted from Berkeley CS61a
Program Structure
Program Structure
Review-Expressions Primitive Expressions: 2 “he11o!" add numbers strings names Arithmetic Expressions: 1+2 15//3 add(3,4) Call Expressions: max(add(2,3),5*min(-1,4))
Review - Expressions Primitive Expressions: Call Expressions: 2 “hello!” add add(3, 4) max(add(2, 3), 5 * min(-1, 4)) Arithmetic Expressions: 1 + 2 15 // 3 numbers strings names
Review-Evaluating Call Expressions add (2 ,3 Operator Operand Operand 1.Evaluate a.Evaluate the operator subexpression b.Evaluate each operand subexpression 2.Apply a.Apply the value of the operator subexpression to the values of the operand subexpression
Review - Evaluating Call Expressions 1. Evaluate a. Evaluate the operator subexpression b. Evaluate each operand subexpression 2. Apply a. Apply the value of the operator subexpression to the values of the operand subexpression add ( 2 , 3 ) Operator Operand Operand
Nested Call Expression Evaluate operator Evaluate operands 3 Apply! 45 add(add(6,mu1(4,6),mu1(3,5)) add 30 15 add(6,mu1(4,6)) mu1(3,5) 24 add 6 mu1(4,6) mul 3 5 mul 4 6 Expression Tree
Nested Call Expression Evaluate operator Evaluate operands Apply! add(add(6, mul(4, 6)), mul(3, 5)) add 1 2 3 add(6, mul(4, 6)) add 6 mul(4, 6) mul 4 6 24 30 mul(3, 5) 15 mul 3 5 45 Expression Tree
Values Programs manipulate values Values represent different types of data Integers:2 44-3 Strings:“hello!”“cs61a” Floats:3.144.5-2.0 Booleans:True False
Values Programs manipulate values Values represent different types of data Floats: Integers: Strings: Booleans: 2 44 -3 3.14 4.5 -2.0 “hello!” “cs61a” True False
Expressions Values Expressions evaluate to values in one or more steps Expression: Value: he11o!' 'hello!' 7/2 3.5 add(1,max(2,3)) 4
Expressions & Values Expressions evaluate to values in one or more steps ‘hello!’ 7 / 2 3.5 add(1, max(2, 3)) 4 Expression: ‘hello!’ Value:
Names Demo Values can be assigned to names to make referring to them easier. A name can only be bound to a X single value. One way to introduce a new name in a program is with an assignment statement. ×=1+2*3-4//5 Name Expression Statements affect the program,but do not evaluate to values
Names Values can be assigned to names to make referring to them easier. A name can only be bound to a single value. Demo One way to introduce a new name in a program is with an assignment statement. x = 1 + 2 * 3 - 4 // 5 Name Expression 7 x Statements affect the program, but do not evaluate to values
Check Your Understanding >>f min >>>f= max >>g,h min, max >>> max g >>>max(f(2,g(h(1,5),3)),4) ???
Check Your Understanding >>> f = min >>> f = max >>> g, h = min, max >>> max = g >>> max(f(2, g(h(1, 5), 3)), 4) ???
Visualizing Assignment Demo Names are bound to values in an environment Global frame 1X=1 X 2y=2 Final Value 3X=y*2 Name To execute an assignment statement: Bindings 1.Evaluate the expression to the right of = 2.Bind the value of the expression to the name to the left of in the current environment
Visualizing Assignment Names are bound to values in an environment To execute an assignment statement: 1. Evaluate the expression to the right of =. 2. Bind the value of the expression to the name to the left of = in the current environment. Final Value Bindings Name Demo