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 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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Multiple Environments in One Diagram! square(square(3)) square 9 square(3) square 3 3 Example: http://goo.gl/668fU
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
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