61a lecture 2 announcements names assignment and user
play

61A Lecture 2 Announcements Names, Assignment, and User-Defined - PowerPoint PPT Presentation

61A Lecture 2 Announcements Names, Assignment, and User-Defined Functions (Demo) Types of Expressions 4 Types of Expressions Primitive expressions: 4 Types of Expressions Primitive expressions: 2 Number or Numeral 4 Types of


  1. Discussion Question 1 Solution (Demo) func min(...) f(2, g(h(1, 5), 3)) func max(...) 2 g(h(1, 5), 3) func min(...) h(1, 5) func max(...) 1 5 Interactive Diagram 9

  2. Discussion Question 1 Solution (Demo) func min(...) f(2, g(h(1, 5), 3)) func max(...) 2 g(h(1, 5), 3) func min(...) 5 h(1, 5) func max(...) 1 5 Interactive Diagram 9

  3. Discussion Question 1 Solution (Demo) func min(...) f(2, g(h(1, 5), 3)) func max(...) 2 g(h(1, 5), 3) func min(...) 3 5 h(1, 5) func max(...) 1 5 Interactive Diagram 9

  4. Discussion Question 1 Solution (Demo) func min(...) f(2, g(h(1, 5), 3)) func max(...) 2 3 g(h(1, 5), 3) func min(...) 3 5 h(1, 5) func max(...) 1 5 Interactive Diagram 9

  5. Discussion Question 1 Solution (Demo) func min(...) 3 f(2, g(h(1, 5), 3)) func max(...) 2 3 g(h(1, 5), 3) func min(...) 3 5 h(1, 5) func max(...) 1 5 Interactive Diagram 9

  6. Discussion Question 1 Solution (Demo) func min(...) 4 3 f(2, g(h(1, 5), 3)) func max(...) 2 3 g(h(1, 5), 3) func min(...) 3 5 h(1, 5) func max(...) 1 5 Interactive Diagram 9

  7. Discussion Question 1 Solution (Demo) 3 func min(...) 4 3 f(2, g(h(1, 5), 3)) func max(...) 2 3 g(h(1, 5), 3) func min(...) 3 5 h(1, 5) func max(...) 1 5 Interactive Diagram 9

  8. Discussion Question 1 Solution (Demo) 3 func min(...) 4 3 f(2, g(h(1, 5), 3)) 3 func max(...) 2 3 g(h(1, 5), 3) func min(...) 3 5 h(1, 5) func max(...) 1 5 Interactive Diagram 9

  9. Defining Functions

  10. Defining Functions Assignment is a simple means of abstraction: binds names to values Function definition is a more powerful means of abstraction: binds names to expressions 11

  11. Defining Functions Assignment is a simple means of abstraction: binds names to values Function definition is a more powerful means of abstraction: binds names to expressions >>> def <name> ( <formal parameters> ): return <return expression> 11

  12. Defining Functions Assignment is a simple means of abstraction: binds names to values Function definition is a more powerful means of abstraction: binds names to expressions Function signature indicates how many arguments a function takes >>> def <name> ( <formal parameters> ): return <return expression> 11

  13. Defining Functions Assignment is a simple means of abstraction: binds names to values Function definition is a more powerful means of abstraction: binds names to expressions Function signature indicates how many arguments a function takes >>> def <name> ( <formal parameters> ): return <return expression> Function body defines the computation performed when the function is applied 11

  14. Defining Functions Assignment is a simple means of abstraction: binds names to values Function definition is a more powerful means of abstraction: binds names to expressions Function signature indicates how many arguments a function takes >>> def <name> ( <formal parameters> ): return <return expression> Function body defines the computation performed when the function is applied Execution procedure for def statements: 11

  15. Defining Functions Assignment is a simple means of abstraction: binds names to values Function definition is a more powerful means of abstraction: binds names to expressions Function signature indicates how many arguments a function takes >>> def <name> ( <formal parameters> ): return <return expression> Function body defines the computation performed when the function is applied Execution procedure for def statements: 1. Create a function with signature <name> ( <formal parameters> ) 11

  16. Defining Functions Assignment is a simple means of abstraction: binds names to values Function definition is a more powerful means of abstraction: binds names to expressions Function signature indicates how many arguments a function takes >>> def <name> ( <formal parameters> ): return <return expression> Function body defines the computation performed when the function is applied Execution procedure for def statements: 1. Create a function with signature <name> ( <formal parameters> ) 2. Set the body of that function to be everything indented after the first line 11

  17. Defining Functions Assignment is a simple means of abstraction: binds names to values Function definition is a more powerful means of abstraction: binds names to expressions Function signature indicates how many arguments a function takes >>> def <name> ( <formal parameters> ): return <return expression> Function body defines the computation performed when the function is applied Execution procedure for def statements: 1. Create a function with signature <name> ( <formal parameters> ) 2. Set the body of that function to be everything indented after the first line 3. Bind <name> to that function in the current frame 11

  18. Calling User-Defined Functions Interactive Diagram 12

  19. Calling User-Defined Functions Procedure for calling/applying user-defined functions (version 1): Interactive Diagram 12

  20. Calling User-Defined Functions Procedure for calling/applying user-defined functions (version 1): 1. Add a local frame, forming a new environment Interactive Diagram 12

  21. Calling User-Defined Functions Procedure for calling/applying user-defined functions (version 1): 1. Add a local frame, forming a new environment 2. Bind the function's formal parameters to its arguments in that frame Interactive Diagram 12

  22. Calling User-Defined Functions Procedure for calling/applying user-defined functions (version 1): 1. Add a local frame, forming a new environment 2. Bind the function's formal parameters to its arguments in that frame 3. Execute the body of the function in that new environment Interactive Diagram 12

  23. Calling User-Defined Functions Procedure for calling/applying user-defined functions (version 1): 1. Add a local frame, forming a new environment 2. Bind the function's formal parameters to its arguments in that frame 3. Execute the body of the function in that new environment Interactive Diagram 12

  24. Calling User-Defined Functions Procedure for calling/applying user-defined functions (version 1): 1. Add a local frame, forming a new environment 2. Bind the function's formal parameters to its arguments in that frame 3. Execute the body of the function in that new environment Built-in function Interactive Diagram 12

  25. Calling User-Defined Functions Procedure for calling/applying user-defined functions (version 1): 1. Add a local frame, forming a new environment 2. Bind the function's formal parameters to its arguments in that frame 3. Execute the body of the function in that new environment Built-in function User-defined function Interactive Diagram 12

  26. Calling User-Defined Functions Procedure for calling/applying user-defined functions (version 1): 1. Add a local frame, forming a new environment 2. Bind the function's formal parameters to its arguments in that frame 3. Execute the body of the function in that new environment Built-in function User-defined function Local frame Interactive Diagram 12

  27. Calling User-Defined Functions Procedure for calling/applying user-defined functions (version 1): 1. Add a local frame, forming a new environment 2. Bind the function's formal parameters to its arguments in that frame 3. Execute the body of the function in that new environment Built-in function Original name of function called User-defined function Local frame Interactive Diagram 12

  28. Calling User-Defined Functions Procedure for calling/applying user-defined functions (version 1): 1. Add a local frame, forming a new environment 2. Bind the function's formal parameters to its arguments in that frame 3. Execute the body of the function in that new environment Built-in function Original name of function called User-defined function Local frame Formal parameter bound to argument Interactive Diagram 12

  29. Calling User-Defined Functions Procedure for calling/applying user-defined functions (version 1): 1. Add a local frame, forming a new environment 2. Bind the function's formal parameters to its arguments in that frame 3. Execute the body of the function in that new environment Built-in function Original name of function called User-defined function Local frame Formal parameter bound to argument Return value 
 (not a binding!) Interactive Diagram 12

  30. Calling User-Defined Functions Procedure for calling/applying user-defined functions (version 1): 1. Add a local frame, forming a new environment 2. Bind the function's formal parameters to its arguments in that frame 3. Execute the body of the function in that new environment Interactive Diagram 13

  31. Calling User-Defined Functions Procedure for calling/applying user-defined functions (version 1): 1. Add a local frame, forming a new environment 2. Bind the function's formal parameters to its arguments in that frame 3. Execute the body of the function in that new environment A function’s signature has all the information needed to create a local frame Interactive Diagram 13

  32. Calling User-Defined Functions Procedure for calling/applying user-defined functions (version 1): 1. Add a local frame, forming a new environment 2. Bind the function's formal parameters to its arguments in that frame 3. Execute the body of the function in that new environment A function’s signature has all the information needed to create a local frame Interactive Diagram 13

  33. Calling User-Defined Functions Procedure for calling/applying user-defined functions (version 1): 1. Add a local frame, forming a new environment 2. Bind the function's formal parameters to its arguments in that frame 3. Execute the body of the function in that new environment A function’s signature has all the information needed to create a local frame Interactive Diagram 13

  34. Looking Up Names In Environments 14

  35. Looking Up Names In Environments Every expression is evaluated in the context of an environment. 14

  36. Looking Up Names In Environments Every expression is evaluated in the context of an environment. So far, the current environment is either: 14

  37. Looking Up Names In Environments Every expression is evaluated in the context of an environment. So far, the current environment is either: • The global frame alone, or 14

  38. Looking Up Names In Environments Every expression is evaluated in the context of an environment. So far, the current environment is either: • The global frame alone, or • A local frame, followed by the global frame. 14

  39. Looking Up Names In Environments Every expression is evaluated in the context of an environment. So far, the current environment is either: • The global frame alone, or • A local frame, followed by the global frame. Most important two things I’ll say all day: 14

  40. Looking Up Names In Environments Every expression is evaluated in the context of an environment. So far, the current environment is either: • The global frame alone, or • A local frame, followed by the global frame. Most important two things I’ll say all day: An environment is a sequence of frames. 14

Recommend


More recommend