Download presentation
Presentation is loading. Please wait.
Published byAubrey Page Modified over 9 years ago
1
Programming Languages and Design Lecture 7 Subroutines and Control Abstraction Instructor: Li Ma Department of Computer Science Texas Southern University, Houston April, 2008
2
2 Review of Previous Lectures Introduction to programming languages Syntax specifications of programming languages Semantic specifications of programming languages Functional Programming Scheme Object-Oriented Programming C++ & Java Names, binding, and scopes Static vs. Dynamic
3
3 Today’s Lecture Parameter passing mechanisms By constant-value By result By value-result By reference By name References “Foundations of Programming Languages: Design and Implementation”, S. H. Roosta, Chapter 3 (§3.2) “Programming Language Pragmatics”, Michael Scott, Chapter 8 (§8.1 – 8.3)
4
4 Name is Abstraction Names enable programmers to refer to variables, constants, operations, and types using identifier names Names are control abstractions and data abstractions for program fragments and data structures Data abstraction: Object-oriented classes hide data representation details behind a set of operations Control abstraction: Subroutines (procedures and functions) allow programmers to focus on manageable subset of program text Subroutine interface hides implementation details
5
5 Mechanisms for Processing Control Abstraction Single entry (except FORTRAN, PL/I) Caller is suspended Control returns to caller
6
6 Subroutine Design Issues Syntax, type checking Parameter passing mechanisms Static or dynamic allocation of locals Static or dynamic scope Environment of parameter functions Overloading Generics Separate compilation
7
7 Parameters in Subroutine Parameter transmission is the major alternative method for sharing data objects among subroutines Formal parameter, or parameter Local data object within a subprogram Specification of the parameter in the invoked subprogram Actual parameter, or argument Specification of the parameter in the invoking subprogram A data object shared with the caller program
8
8 Call by Value Allocate memory for parameter Initialize parameter with the argument’s value Call procedure Nothing happens on return Used in C, C++, Pascal, Lisp, ML, etc. Accesses are usually more efficient with this method But additional storage is required for the formal parameters in the called subprogram
9
9 Call-by-Value Example int a = 1; void foo (int x) { // a and x have same value, // changes to a or x don’t // affect each other } // argument can be expression foo (a + a); // no modifications to a
10
10 Call by Result Argument must be variable Allocate memory for parameter Don’t initialize parameter Call Procedure... (within here, parameter will get a value) Copy parameter into argument variable Return from procedure Additional storage is required for the formal parameters in the called subprogram
11
11 Call-by-Result Example int a = 2; void foo (int x) { // x is not initialized, // changes to a or x don’t // affect each other } // argument must be variable foo (a); // a will be modified
12
12 Call by Value-Result Copy-In-Copy-Out, or passing by copy Combination of the first two Copy argument value on call Copy result on return Used by Ada for parameters of primitive type in in-out mode Additional storage is required for the formal parameters and time for coping values
13
13 Call-by-Value-Result Example int a = 3; void foo (int x) { // a and x have same value // changes to a or x don’t // affect each other } // argument must be variable foo (a); // a might be modified
14
14 Call by Reference A pointer to the memory location of the data object is made available to the called subprogram Evaluate argument to get the location of the variable Call procedure Nothing happens on return Used by FORTRAN before 1977, Pascal var parameter Passing process is efficient in both time and space But access to the formal parameter is slower due to one more level of indirect addressing
15
15 Call-by-Reference Example int a = 4; void foo (int x) { // a and x reference same location // changes to a and x // affect each other } // argument can be an expression foo (a); // a might be modified
16
16 Call by Name Don’t evaluate argument Create closure to evaluate argument Call procedure Evaluate argument by calling parameter closure Nothing happens on return Used by ALGOL-60, Simula-67 Similar to macro expansion (e.g, TeX)
17
17 Call-by-Name Example int a = 5; void foo (int x) { // x is a function // to get value of argument, // evaluate x() when value is needed } // argument can be an expression foo (a + a); // no modifications to a
18
18 Languages Comparison in Parameter Passing In Pascal, parameters are passed by value The key word var is needed if passing by reference Fortran passes all parameters by reference If the parameter is not an l-value (it is an expression), then the compiler will create a temporary variable to hold the value, and pass this variable by reference In Java, parameters of primitive types are passed by value; Object parameters are passed by sharing Choose by value or by reference according to the argument (implemented as value or address)
19
19 Languages Comparison in Parameter Passing (cont’) Parameters in C are always passed by value, it must pass the address of variable explicitly int v1, v2; void swap (int *a, int *b) { int t=*a; *a=*b; *b=t; } … swap (&v1, &v2); C++ introduces an explicit notion of a reference void swap (int &a, int &b) { int t=a; a=b; b=t; }
20
20 Choose Passing Parameter Method In a language that provides both value and reference parameters (Pascal, Modula), it needs rules to make decision Concern about the efficiency and safety If need to change the value of an argument – call by reference – efficient If do not want to change the value of the argument – call by value – safe Concern about the potential cost Copying the large value from actuals to formals is potentially time-consuming Accessing a parameter that is passed by reference requires an extra level of indirection
21
21 Choose Passing Parameter Method (cont’) To combine the efficiency of reference parameters and the safety of value parameters Modula-3 uses READONLY parameter mode For small parameters, pass a value For large parameters, pass an address C uses const parameter void append_to_log (const huge_record *r) { … } … append_to_log (&my_record); const applies to the record to which r points The caller passes the address of its record explicitly, the callee will not change the record’s content
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.