61a lecture 3
play

61A Lecture 3 Wednesday, August 31 Life Cycle of a User-Defined - PowerPoint PPT Presentation

61A Lecture 3 Wednesday, August 31 Life Cycle of a User-Defined Function What happens? Def statement: >>> def square ( x ): return mul(x, x) Call expression: square(2+2) Calling/Applying: square ( x ): return mul(x, x) 2 Life


  1. 61A Lecture 3 Wednesday, August 31

  2. Life Cycle of a User-Defined Function What happens? Def statement: >>> def square ( x ): return mul(x, x) Call expression: square(2+2) Calling/Applying: square ( x ): return mul(x, x) 2

  3. Life Cycle of a User-Defined Function What happens? Def statement: >>> def square ( x ): return mul(x, x) D e f s t a t e m e n t Call expression: square(2+2) Calling/Applying: square ( x ): return mul(x, x) 2

  4. Life Cycle of a User-Defined Function What happens? Formal parameter Def statement: >>> def square ( x ): return mul(x, x) D e f s t a t e m e n t Call expression: square(2+2) Calling/Applying: square ( x ): return mul(x, x) 2

  5. Life Cycle of a User-Defined Function What happens? Formal parameter Def statement: >>> def square ( x ): return mul(x, x) D e f s t a t e m e n t B o d y Call expression: square(2+2) Calling/Applying: square ( x ): return mul(x, x) 2

  6. Life Cycle of a User-Defined Function What happens? Formal parameter Def statement: >>> def square ( x ): return mul(x, x) D e f s t a t e m e n t B o d y ( return statement ) Call expression: square(2+2) Calling/Applying: square ( x ): return mul(x, x) 2

  7. Life Cycle of a User-Defined Function What happens? Formal parameter Def statement: R e t u r n e x p r e s s i o n >>> def square ( x ): return mul(x, x) D e f s t a t e m e n t B o d y ( return statement ) Call expression: square(2+2) Calling/Applying: square ( x ): return mul(x, x) 2

  8. Life Cycle of a User-Defined Function What happens? Formal parameter Def statement: R e t u r n Function created e x p r e s s i o n >>> def square ( x ): return mul(x, x) D e f s t a t e m e n t B o d y ( return statement ) Call expression: square(2+2) Calling/Applying: square ( x ): return mul(x, x) 2

  9. Life Cycle of a User-Defined Function What happens? Formal parameter Def statement: R e t u r n Function created e x p r e s s i o n >>> def square ( x ): Name bound return mul(x, x) D e f s t a t e m e n t B o d y ( return statement ) Call expression: square(2+2) Calling/Applying: square ( x ): return mul(x, x) 2

  10. Life Cycle of a User-Defined Function What happens? Formal parameter Def statement: R e t u r n Function created e x p r e s s i o n >>> def square ( x ): Name bound return mul(x, x) D e f s t a t e m e n t B o d y ( return statement ) Call expression: square(2+2) operand: 2+2 argument: 4 Calling/Applying: square ( x ): return mul(x, x) 2

  11. Life Cycle of a User-Defined Function What happens? Formal parameter Def statement: R e t u r n Function created e x p r e s s i o n >>> def square ( x ): Name bound return mul(x, x) D e f s t a t e m e n t B o d y ( return statement ) Call expression: square(2+2) operand: 2+2 argument: 4 operator: square function: square Calling/Applying: square ( x ): return mul(x, x) 2

  12. Life Cycle of a User-Defined Function What happens? Formal parameter Def statement: R e t u r n Function created e x p r e s s i o n >>> def square ( x ): Name bound return mul(x, x) D e f s t a t e m e n t B o d y ( return statement ) Op's evaluated Call expression: square(2+2) operand: 2+2 argument: 4 operator: square function: square Calling/Applying: square ( x ): return mul(x, x) 2

  13. Life Cycle of a User-Defined Function What happens? Formal parameter Def statement: R e t u r n Function created e x p r e s s i o n >>> def square ( x ): Name bound return mul(x, x) D e f s t a t e m e n t B o d y ( return statement ) Op's evaluated Call expression: square(2+2) operand: 2+2 argument: 4 Function called operator: square with argument(s) function: square Calling/Applying: square ( x ): return mul(x, x) 2

  14. Life Cycle of a User-Defined Function What happens? Formal parameter Def statement: R e t u r n Function created e x p r e s s i o n >>> def square ( x ): Name bound return mul(x, x) D e f s t a t e m e n t B o d y ( return statement ) Op's evaluated Call expression: square(2+2) operand: 2+2 argument: 4 Function called operator: square with argument(s) function: square Calling/Applying: square ( x ): return mul(x, x) Signature 2

  15. Life Cycle of a User-Defined Function What happens? Formal parameter Def statement: R e t u r n Function created e x p r e s s i o n >>> def square ( x ): Name bound return mul(x, x) D e f s t a t e m e n t B o d y ( return statement ) Op's evaluated Call expression: square(2+2) operand: 2+2 argument: 4 Function called operator: square with argument(s) function: square Calling/Applying: 4 square ( x ): return mul(x, x) Signature 2

  16. Life Cycle of a User-Defined Function What happens? Formal parameter Def statement: R e t u r n Function created e x p r e s s i o n >>> def square ( x ): Name bound return mul(x, x) D e f s t a t e m e n t B o d y ( return statement ) Op's evaluated Call expression: square(2+2) operand: 2+2 argument: 4 Function called operator: square with argument(s) function: square Calling/Applying: 4 square ( x ): 16 return mul(x, x) Signature 2

  17. Life Cycle of a User-Defined Function What happens? Formal parameter Def statement: R e t u r n Function created e x p r e s s i o n >>> def square ( x ): Name bound return mul(x, x) D e f s t a t e m e n t B o d y ( return statement ) Op's evaluated Call expression: square(2+2) operand: 2+2 argument: 4 Function called operator: square with argument(s) function: square Calling/Applying: 4 square ( x ): Argument 16 return mul(x, x) Signature 2

  18. Life Cycle of a User-Defined Function What happens? Formal parameter Def statement: R e t u r n Function created e x p r e s s i o n >>> def square ( x ): Name bound return mul(x, x) D e f s t a t e m e n t B o d y ( return statement ) Op's evaluated Call expression: square(2+2) operand: 2+2 argument: 4 Function called operator: square with argument(s) function: square Calling/Applying: 4 square ( x ): Argument 16 return mul(x, x) Signature Return value 2

  19. Life Cycle of a User-Defined Function What happens? Formal parameter Def statement: R e t u r n Function created e x p r e s s i o n >>> def square ( x ): Name bound return mul(x, x) D e f s t a t e m e n t B o d y ( return statement ) Op's evaluated Call expression: square(2+2) operand: 2+2 argument: 4 Function called operator: square with argument(s) function: square New frame! Calling/Applying: 4 square ( x ): Argument 16 return mul(x, x) Signature Return value 2

  20. Life Cycle of a User-Defined Function What happens? Formal parameter Def statement: R e t u r n Function created e x p r e s s i o n >>> def square ( x ): Name bound return mul(x, x) D e f s t a t e m e n t B o d y ( return statement ) Op's evaluated Call expression: square(2+2) operand: 2+2 argument: 4 Function called operator: square with argument(s) function: square New frame! Calling/Applying: 4 square ( x ): Params bound Argument 16 return mul(x, x) Signature Return value 2

  21. Life Cycle of a User-Defined Function What happens? Formal parameter Def statement: R e t u r n Function created e x p r e s s i o n >>> def square ( x ): Name bound return mul(x, x) D e f s t a t e m e n t B o d y ( return statement ) Op's evaluated Call expression: square(2+2) operand: 2+2 argument: 4 Function called operator: square with argument(s) function: square New frame! Calling/Applying: 4 square ( x ): Params bound Argument 16 return mul(x, x) Body executed Signature Return value 2

  22. Life Cycle of a User-Defined Function What happens? Formal parameter Def statement: R e t u r n Function created e x p r e s s i o n N a m e >>> def square ( x ): Name bound return mul(x, x) D e f s t a t e m e n t B o d y ( return statement ) Op's evaluated Call expression: square(2+2) operand: 2+2 argument: 4 Function called operator: square with argument(s) function: square New frame! Calling/Applying: 4 square ( x ): Params bound Argument 16 return mul(x, x) Body executed Signature Return value 2

  23. Multiple Environments in One Diagram! square(square(3)) square 9 square(3) square 3 3 Example: http://goo.gl/668fU

  24. Multiple Environments in One Diagram! 27 square(square(3)) square 9 square(3) square 3 1 2 2 1 An environment is a sequence of frames. • The global frame alone 1 • A local, then the global frame 4 Example: http://goo.gl/668fU

  25. Names Have No Meaning Without Environments Every expression is evaluated in the context of an environment. A name evaluates to the value bound to that name in the earliest frame of the current environment in which that name is found. mul(x, x) 1 2 2 1 “ m u l ” i s n o t f o u n d 1 5 Example: http://goo.gl/668fU

Recommend


More recommend