Download presentation
Presentation is loading. Please wait.
1
Organization of Programming Languages
Meeting 21 March 7, 2016
2
Planning Ahead Quiz on Wednesday Multiple assignment statements
Program functions Control structures Sequence If-then-else While-do For Associated functions: range, next
3
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
4
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, …
5
Functions (3) Syntax issues: Defining the function
Using (invoking) the function
6
Defining Functions Need to specify: Name Parameters
Kind: function, procedure, operator, … Type of output Parameters Type Mode
7
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
8
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
9
Output Values (3) Side effect to global variable
Action but no state change Print Write to file
10
Simple Functions Specialize to
One or two arguments (parameters) of same type One value of same type as arguments Monadic or dyadic operators
11
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
12
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
13
Calls and Returns (2) Need to specify for return
Moving values to output parameters Deallocating storage Resetting variable access Returning control to calling program
14
Semantics of Parameter Passing
Consider five ways of parameter passing: Value Result Value-Result Reference (aka Location) Name
15
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.
16
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
17
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
18
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
19
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
20
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
21
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
22
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
23
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
24
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
25
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
26
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
27
Pass by Reference (2) Execute subprogram
28
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
29
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.
30
Pass by Name (2) Execute subprogram
31
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.