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