Organization of Programming Languages Meeting 21 March 7, 2016
Planning Ahead Quiz on Wednesday Multiple assignment statements Program functions Control structures Sequence If-then-else While-do For Associated functions: range, next
Functions, Methods, etc. Segmenting programs is an old technique producing program parts known by many names: Function Subroutine Procedure Method Operator Subprogram See Sebesta, Chapter 9, for his exposition of the topic
Functions (2) All segmentation strategies involve: Input values Local variables Output values We look in detail at the mechanisms provided by various programming languages using the semantic categories from before: Env, Ide, Loc, Store, V*, M, …
Functions (3) Syntax issues: Defining the function Using (invoking) the function
Defining Functions Need to specify: Name Parameters Kind: function, procedure, operator, … Type of output Parameters Type Mode
Output Values Produced in several ways Appear as parameter values: foofun(x,y) takes x and computes y Modes of y: result, reference Can handle arbitrary type as output
Output Values (2) Return in function name Follows structure in mathematics log(x) Easy to work with in expressions Implemented with RETURN statement Assignment to function name
Output Values (3) Side effect to global variable Action but no state change Print Write to file
Simple Functions Specialize to One or two arguments (parameters) of same type One value of same type as arguments Monadic or dyadic operators
Simple Functions (2) Implementation strategies Mix operators and functions Typical, mimics mathematics: 1+log(4) Operators only Use symbols for functions Monadic functions only Use list structure for several arguments
Subprogram Calls and Returns Need to specify for calls: Parameter passing mechanism (mode) Allocation of storage for local variables and binding to names Making global variables visible Saving execution status of calling program unit Transfer of control to subprogram code
Calls and Returns (2) Need to specify for return Moving values to output parameters Deallocating storage Resetting variable access Returning control to calling program
Semantics of Parameter Passing Consider five ways of parameter passing: Value Result Value-Result Reference (aka Location) Name
Semantics (2) In each case, we’ll assume a parameter list specified in the header of the procedure as (x1, x2, …). These are the formal parameters. We’ll assume a call foofun(a1,a2, …). The a’s are the actual parameters. Here’s what happens in each scenario.
Pass by Value Bind local parameters to locations: Element of Env becomes x1 -> loc 1 x2 -> loc 2 If x1 or x2 or … were bound to locations before invocation, those bindings are saved as part of the transfer of control
Pass by Value (2) Bind values of actual parameters to these locations. Assume a1 -> loc 50 -> v1 Then bind loc 1 to v1, changing the element of the Store Execute subprogram
Pass by Value (3) Break the bindings of the local parameters to locations. Either the element of Env shows x1 -> unbound or x1 -> location saved on entry
Pass by Result Bind local parameters to locations: Element of Env becomes x1 -> loc 1 x2 -> loc 2 If x1 or x2 or … were bound to locations before invocation, those bindings are saved as part of the transfer of control
Pass by Result (2) Execute subprogram Copy values of local parameters to locations of actual parameters x1 -> loc 1 -> v1 a1 -> loc 50 Bind loc 50 -> v1
Pass by Result (3) Break the bindings of the local parameters to locations. Either the element of Env shows x1 -> unbound or x1 -> location saved on entry
Pass by Value-Result Bind local parameters to locations: Element of Env becomes x1 -> loc 1 x2 -> loc 2 If x1 or x2 or … were bound to locations before invocation, those bindings are saved as part of the transfer of control
Pass by Value-Result (2) Bind values of actual parameters to these locations. Assume a1 -> loc 50 -> v1 Then bind loc 1 to v1, changing the element of the Store Execute subprogram
Pass by Value-Result (3) Copy values of local parameters to locations of actual parameters x1 -> loc 1 -> v1 a1 -> loc 50 Bind loc 50 -> v1
Pass by Value-Result (4) Break the bindings of the local parameters to locations. Either the element of Env shows x1 -> unbound or x1 -> location saved on entry
Pass by Reference Bind local parameters to locations of actual parameters: If a1 -> loc 50, a2 -> loc 51, … then Element of Env becomes x1 -> loc 50 x2 -> loc 51, etc If x1 or x2 or … were bound to locations before invocation, those bindings are saved as part of the transfer of control
Pass by Reference (2) Execute subprogram
Pass by Reference (3) Break the bindings of the local parameters to locations. Either the element of Env shows x1 -> unbound or x1 -> location saved on entry
Pass by Name Bind local parameters to rexp of actual parameters If a1 is given as string1 then x1 is assumed to be string1 etc If x1 or x2 or … were bound to locations before invocation, those bindings are saved as part of the transfer of control.
Pass by Name (2) Execute subprogram
Pass by Name (3) Break the bindings of the local parameters to strings (of code) Either the element of Env shows x1 -> unbound or x1 -> location saved on entry