Presentation is loading. Please wait.

Presentation is loading. Please wait.

ICE1341 Programming Languages Spring 2005 Lecture #16 Lecture #16 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.

Similar presentations


Presentation on theme: "ICE1341 Programming Languages Spring 2005 Lecture #16 Lecture #16 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University."— Presentation transcript:

1 ICE1341 Programming Languages Spring 2005 Lecture #16 Lecture #16 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University (ICU)

2 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University FORTRAN Programming – cont. FORTRAN Programming – cont. Control Statements Control Statements Input/Output Statements Input/Output Statements Subprograms Subprograms Last Lecture

3 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University This Lecture Chapter 9 – Subprograms Chapter 9 – Subprograms Parameter Passing Models and Methods Parameter Passing Models and Methods Design Issues for Subprograms Design Issues for Subprograms Overloaded Subprograms Overloaded Subprograms Coroutines Coroutines

4 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University FORTRAN Carriage Control Characters Carriage control characters (1H1, 1H0, 1H, …) are effective only at the beginning of each line Carriage control characters (1H1, 1H0, 1H, …) are effective only at the beginning of each line WRITE (6, 98) 11, 22, 33 WRITE (6, 98) 11, 22, FORMAT (1H1, I3/, 1H0, I3, 1H0, I3) GNU FORTRAN Compiler (g77) doesn’t support the carriage control characters GNU FORTRAN Compiler (g77) doesn’t support the carriage control characters

5 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Chapter 9 Subprograms

6 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Characteristics of Subprograms General characteristics of subprograms: 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 Caller Program Subprogram int myFunc (int a, char *s) { // Process Abstraction // Process Abstraction} … int result = myFunc (10, “val”); …

7 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Basic Definitions Subprogram Definition: describes the interface to and the actions of the subprogram abstraction Subprogram Definition: describes the interface to and the actions of the subprogram abstraction Parameter Profile (Signature): the number, order, and types of its parameters Parameter Profile (Signature): the number, order, and types of its parameters Protocol = Parameter Profile + Return Type Protocol = Parameter Profile + Return Type Subprogram Declaration (Prototype): provides the protocol, but not the body, of the subprogram Subprogram Declaration (Prototype): provides the protocol, but not the body, of the subprogram Caller Program Subprogram int myFunc (int a, char *s) { } … int result = myFunc (10, “val”); Subprogram Header Subprogram Call

8 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Parameters Correspondence btw Actual and Formal params. Correspondence btw Actual and Formal params. Positional Parameters Positional Parameters Keyword Parameters (Ada, Fortran 95) Keyword Parameters (Ada, Fortran 95) e.g., Sort(10, List => A, Length => N); Specifying default values (C++, Ada, PHP) Specifying default values (C++, Ada, PHP) e.g., int myFunc (char *s, int a = 9) Caller Program Subprogram int myFunc (int a, char *s) { } … int result = myFunc (10, “val”); Actual Parameters Formal Parameters Any problems?

9 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Models of Parameter Passing

10 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Parameter Passing Methods (1) Caller Program Subprogram int myFunc (int a, char *s) { …} … int result = myFunc (10, “val”); … Call-by-Value Needs duplicated space Pass-by-Reference (Call-by-Reference) Needs indirect addressing Needs indirect addressing Pass-by-Value (In Mode) Value Access Path

11 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Parameter Passing Methods (2) Caller Program Subprogram int myFunc (int a, char *s) { …} … char *str = “val”; int result = myFunc (10, str); … Pass-by-Result (Out Mode) Pass-by-Reference (Inout Mode)

12 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Parameter Passing Methods (3) Pass-by-Value-Result (Inout Mode, Pass-by-Copy) 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 aFunc (int *a) { int aVal = *a; // copy the value to a local var int aVal = *a; // copy the value to a local var… aVal += 10; // No direct changes to *a aVal += 10; // No direct changes to *a… *a = aVal; // copy the result to the return var *a = aVal; // copy the result to the return var }

13 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Parameter Passing Methods (4) Pass-by-Name (Inout Mode) Pass-by-Name (Inout Mode) The actual parameter is textually substituted for the corresponding formal parameter in all its occurrences The actual parameter is textually substituted for the corresponding formal parameter in all its occurrences Lazy evaluation Lazy evaluation e.g., C macros e.g., C macros #define SQUARE (x) x * x … y = SQUARE (val);  SQUARE (val * val);

14 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Disadvantages of Pass-by-Reference Slower access – Indirect reference of values Slower access – Indirect reference of values Actual parameter collisions – Allowing aliasing Actual parameter collisions – Allowing aliasing e.g. void fun (int &a, int &b); // subprogram def. fun (x, x);// caller fun (x, x);// caller Array element collisions Array element collisions e.g. fun (list[ i ], list[ j ]);// if i = j ? fun1 (list, list[ i ]); // a different one fun1 (list, list[ i ]); // a different one Collision between formals and globals Collision between formals and globals e.g. int *global;// a global variable void sub (int *param) {// subprogram def. void sub (int *param) {// subprogram def.… } sub (global);// caller sub (global);// caller * AW Lecture Notes

15 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Stack Implementation of Parameter-Passing Call-by-Value Return-by-Value Pass-by-Value- Result Pass-by- Reference

16 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Multidimensional Arrays as Parameters void fun (int matrix[][10]) { … } The formal parameter must include the column size to make a storage mapping function The formal parameter must include the column size to make a storage mapping function Cannot accept matrices with different column size Cannot accept matrices with different column size void fun (int *matrix, int rsize, int csize) { … } Mapping function: * (matrix + (row * csize) + col) Mapping function: * (matrix + (row * csize) + col) Readability is low Readability is low void fun (int matrix[][]) { int rsize = matrix.length; int csize = matrix[0].length; } // Java and C# Arrays are objects Arrays are objects

17 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Parameters that are Subprogram Names Shallow Binding – the reference environment of the call statement is passed to the subprogram 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 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 Ad Hoc Binding – the environment of the call statement that passes the subprogram as an actual parameter X = 3 function sub1 () { var x; function sub2 () { alert(x); alert(x);}; function sub3 () { var x; var x; x = 3; x = 3; sub4 (sub2); sub4 (sub2);}; function sub4 (subx) { var x; var x; x = 4; x = 4; subx (); subx (); }; }; x = 1; sub3 (); }; // JavaScript

18 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University 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?

19 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Overloaded Subprograms A subprogram that has the same name as another subprogram in the same referencing environment A subprogram that has the same name as another subprogram in the same referencing environment Each has a unique protocol to differentiate from others Each has a unique protocol to differentiate from others Number, order, types of parameters Number, order, types of parameters Return type Return type class MyVector extends Vector { … public boolean add(int num) { Integer obj = new Integer(num); Integer obj = new Integer(num); return add(obj); return add(obj);} public boolean add(float num) { Float obj = new Float(num); Float obj = new Float(num); return add(obj); return add(obj);}…} MyVector vec = new MyVector(); vec.add(13.41); vec.add(1341); Java

20 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Generic (Polymorphic) Subprograms Takes parameters of different types on different activations Takes parameters of different types on different activations Needs to be instantiated Needs to be instantiated Compiler generates a version for a data type Compiler generates a version for a data type Overloaded subprograms provide ad hoc polymorphism Overloaded subprograms provide ad hoc polymorphism e.g., e.g., C++ Template Functions C++ Template Functions Ada Generic Units Ada Generic Units template template Type max(Type a, Type b) { return a > b ? a : b; } int n1, n2; float f1, f2; … int n3 = max(n1, n2); float f3 = max(f1, f2); int max(int a, int b) { return a > b ? a : b; } C++

21 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University User-Defined Overloaded Operators function "*"(A, B : in Vector_Type) return Integer is return Integer is Sum : Integer := 0; begin for Index in A'range loop Sum := Sum + A(Index) * B(Index); Sum := Sum + A(Index) * B(Index); end loop; return Sum; end "*"; Which of the following is more readable? c = a * b; c = a * b; c = DotProduct(a, b); c = DotProduct(a, b); Ada

22 Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Coroutines Symmetric Unit Control Model (cf. Master- slave Control Model) Symmetric Unit Control Model (cf. Master- slave Control Model) Quasi-concurrent execution of program units Quasi-concurrent execution of program units


Download ppt "ICE1341 Programming Languages Spring 2005 Lecture #16 Lecture #16 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University."

Similar presentations


Ads by Google