160 likes | 239 Views
Programming Languages (ICE 1341) Lecture #15 April 21, 2004 In-Young Ko iko .AT. i cu . ac.kr Information and Communications University (ICU). Announcements. You can now access your grades from the class homepage by entering your student ID and email address.
E N D
Programming Languages(ICE 1341)Lecture #15April 21, 2004In-Young Koiko .AT. icu.ac.krInformation and Communications University (ICU) ICE 1341 – Programming Languages (Lecture #15) In-Young Ko
Announcements • You can now access your grades from the class homepage by entering your student ID and email address ICE 1341 – Programming Languages (Lecture #15) In-Young Ko
Review of the Previous Lectures Control Structures • Selection Statements – if, switch • Iterative Statements– do, for, while, … • Unconditional Branching– goto • Guarded Commands– nondeterministic if ICE 1341 – Programming Languages (Lecture #15) In-Young Ko
Characteristics of Subprograms Caller Program Subprogram • General characteristics of subprograms: 1. A subprogram has a single entry point 2. The caller is suspended during execution of the called subprogram 3. Control always returns to the caller when the called subprogram’s execution terminates … int result = myFunc (10, “val”); … int myFunc (int a, char *s) { // Process Abstraction } ICE 1341 – Programming Languages (Lecture #15) In-Young Ko
Basic Definitions Caller Program Subprogram • Subprogram Definition: describes the interface to and the actions of the subprogram abstraction • Parameter Profile (Signature): the number, order, and types of its parameters • Protocol = Parameter Profile + Return Type • Subprogram Declaration (Prototype): provides the protocol, but not the body, of the subprogram … int result = myFunc (10, “val”); int myFunc (int a, char *s) { } Subprogram Header Subprogram Call ICE 1341 – Programming Languages (Lecture #15) In-Young Ko
Parameters Caller Program Subprogram • Correspondence btw Actual and Formal params. • Positional Parameters • Keyword Parameters (Ada, Fortran 95) e.g., Sort(List => A, Length => N); • Specifying default values (C++, Ada, PHP) e.g., int myFunc (int a = 9, char *s); … int result = myFunc (10, “val”); int myFunc (int a, char *s) { } Formal Parameters Actual Parameters ICE 1341 – Programming Languages (Lecture #15) In-Young Ko
Models of Parameter Passing ICE 1341 – Programming Languages (Lecture #15) In-Young Ko
Call-by-Value Needs duplicated space Pass-by-Reference (Call-by-Reference) Needs indirect addressing Pass-by-Value (In Mode) Parameter Passing Methods (1) Caller Program Subprogram … int result = myFunc (10, “val”); … int myFunc (int a, char *s) { … } Value Access Path ICE 1341 – Programming Languages (Lecture #15) In-Young Ko
Pass-by-Reference (Inout Mode) Pass-by-Result (Out Mode) Parameter Passing Methods (2) Caller Program Subprogram … char *str = “val”; int result = myFunc (10, str); … int myFunc (int a, char *s) { … } ICE 1341 – Programming Languages (Lecture #15) In-Young Ko
Parameter Passing Methods (3) • Pass-by-Value-Result(Inout Mode, Pass-by-Copy) = Pass-by-Value + Pass-by-Result The following generates the similar effect as PVR: int aFunc (int *a) { int aVal = *a; // copy the value to a local var … aVal += 10; // No direct changes to *a … *a = aVal;// copy the result to the return var } ICE 1341 – Programming Languages (Lecture #15) In-Young Ko
Parameter Passing Methods (4) • Pass-by-Name (Inout Mode) • The actual parameter is textually substituted for the corresponding formal parameter in all its occurrences • Lazy evaluation • e.g., C macros #define SQUARE (x) x * x … y = SQUARE (val); SQUARE (val * val); ICE 1341 – Programming Languages (Lecture #15) In-Young Ko
Disadvantages of Pass-by-Reference • Slower access – Indirect reference of values • Actual parameter collisions – Allowing aliasing e.g. void fun (int &a, int &b);// subprogram def. sub1(x, x); // caller • Array element collisions e.g. sub1(list[i], list[j]);// if i = j ? sub2(list, list[i]); // a different one • Collision between formals and globals e.g. int *global; // a global variable void sub (int *param) { // subprogram def. … } sub (global); // caller * AW Lecture Notes ICE 1341 – Programming Languages (Lecture #15) In-Young Ko
Stack Implementation of Parameter-Passing Call-by-Value Return-by-Value Pass-by-Value-Result Pass-by-Reference ICE 1341 – Programming Languages (Lecture #15) In-Young Ko
Multidimensional Arrays as Parameters void fun (int matrix[][10]) { … } • The formal parameter must include the column size to make a storage mapping function • Cannot accept matrices with different column size void fun (int *matrix, int rsize, int csize) { … } • Mapping function: * (matrix + (row * csize) + col) • Readability is low void fun (int matrix[][]) { int rsize = matrix.length; int csize = matrix[0].length; } // Java and C# • Arrays are objects ICE 1341 – Programming Languages (Lecture #15) In-Young Ko
Parameters that are Subprogram Names function sub1 () { var x; function sub2 () { alert(x); }; function sub3 () { var x; x = 3; sub4 (sub2); }; function sub4 (subx) { var x; x = 4; subx (); }; x = 1; sub3 (); }; // JavaScript • Shallow Binding – the reference environment of the call statement is passed to the subprogram X = 4 • Deep Binding – the environment of the definition of the passed subprogram X = 1 • Ad Hoc Binding – the environment of the call statement that passes the subprogram as an actual parameter X = 3 ICE 1341 – Programming Languages (Lecture #15) In-Young Ko
Design Issues for Subprograms 1. What parameter passing methods are provided? 2. Are parameter types checked? 3. Are local variables static or dynamic? 4. Can subprogram definitions appear in other subprogram definitions (nested definitions)? 5. What is the referencing environment of a passed subprogram? 6. Can subprograms be overloaded? 7. Are subprograms allowed to be generic? ICE 1341 – Programming Languages (Lecture #15) In-Young Ko