16
April 21, 2004 1 ICE 1341 – Programming Languages (Lecture #15) In-Young Ko Programming Languages Programming Languages (ICE 1341) (ICE 1341) Lecture #15 Lecture #15 April 21, 2004 In-Young Ko iko .AT. i cu . ac.kr Information and Communications University (ICU)

Announcements

Embed Size (px)

DESCRIPTION

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. - PowerPoint PPT Presentation

Citation preview

Page 1: Announcements

April 21, 2004 1 ICE 1341 – Programming Languages (Lecture #15) In-Young Ko

Programming LanguagesProgramming Languages(ICE 1341)(ICE 1341)

Lecture #15Lecture #15 April 21, 2004

In-Young Koiko .AT. icu.ac.kr

Information and Communications University (ICU)

Page 2: Announcements

April 21, 2004 2 ICE 1341 – Programming Languages (Lecture #15) In-Young Ko

AnnouncementsAnnouncements

You can now access your grades from the class You can now access your grades from the class homepage by entering your homepage by entering your student IDstudent ID and and email addressemail address

Page 3: Announcements

April 21, 2004 3 ICE 1341 – Programming Languages (Lecture #15) In-Young Ko

Review of the Previous LecturesReview of the Previous Lectures

Control StructuresControl Structures Selection StatementsSelection Statements – if, switch – if, switch Iterative StatementsIterative Statements – do, for, while, …– do, for, while, … Unconditional BranchingUnconditional Branching – goto– goto Guarded CommandsGuarded Commands – nondeterministic if– nondeterministic if

Page 4: Announcements

April 21, 2004 4 ICE 1341 – Programming Languages (Lecture #15) In-Young Ko

Characteristics of SubprogramsCharacteristics of Subprograms

General characteristics of subprograms:General characteristics of subprograms:1. A subprogram has a 1. A subprogram has a single entry pointsingle entry point2. 2. The caller is suspendedThe caller is suspended during execution of the during execution of the

called subprogramcalled subprogram3. 3. Control always returns to the callerControl always returns to the caller when the called when the called

subprogram’s execution terminatessubprogram’s execution terminates

Caller ProgramCaller Program SubprogramSubprogram

int myFunc (int a, char *s) {int myFunc (int a, char *s) {

// // Process AbstractionProcess Abstraction

}}

……

int result = myFunc (10, “val”);int result = myFunc (10, “val”);

……

Page 5: Announcements

April 21, 2004 5 ICE 1341 – Programming Languages (Lecture #15) In-Young Ko

Basic DefinitionsBasic Definitions

Subprogram DefinitionSubprogram Definition: describes the : describes the interfaceinterface to and to and the the actionsactions of the subprogram abstraction of the subprogram abstraction

Parameter Profile (Signature)Parameter Profile (Signature): the number, order, and : the number, order, and types of its parameterstypes of its parameters

ProtocolProtocol = = Parameter Profile + Parameter Profile + Return TypeReturn Type Subprogram Declaration (Prototype)Subprogram Declaration (Prototype): provides the : provides the

protocol, but protocol, but not the bodynot the body, of the subprogram, of the subprogram

Caller ProgramCaller Program SubprogramSubprogram

int myFunc (int a, char *s) {int myFunc (int a, char *s) {

}}

……

int result = myFunc (10, “val”);int result = myFunc (10, “val”);Subprogram HeaderSubprogram Header

Subprogram CallSubprogram Call

Page 6: Announcements

April 21, 2004 6 ICE 1341 – Programming Languages (Lecture #15) In-Young Ko

ParametersParameters

Correspondence btw Actual and Formal params.Correspondence btw Actual and Formal params. Positional ParametersPositional Parameters Keyword ParametersKeyword Parameters (Ada, Fortran 95) (Ada, Fortran 95)

e.g., e.g., Sort(List => A, Length => N);Sort(List => A, Length => N); Specifying default values (C++, Ada, PHP)Specifying default values (C++, Ada, PHP)

ee.g., .g., int myFunc (int a = 9, char *s);int myFunc (int a = 9, char *s);

Caller ProgramCaller Program SubprogramSubprogram

int myFunc (int a, char *s) {int myFunc (int a, char *s) {

}}

……

int result = myFunc (10, “val”);int result = myFunc (10, “val”);

Actual ParametersActual Parameters

Formal ParametersFormal Parameters

Page 7: Announcements

April 21, 2004 7 ICE 1341 – Programming Languages (Lecture #15) In-Young Ko

Models of Parameter PassingModels of Parameter Passing

Page 8: Announcements

April 21, 2004 8 ICE 1341 – Programming Languages (Lecture #15) In-Young Ko

Parameter Passing Methods (1)Parameter Passing Methods (1)

Caller ProgramCaller Program SubprogramSubprogram

int myFunc (int a, char *s) {int myFunc (int a, char *s) {

… …

}}

……

int result = myFunc (10, “val”);int result = myFunc (10, “val”);

……

Call-by-ValueCall-by-ValueNeeds duplicated spaceNeeds duplicated space Pass-by-Reference Pass-by-Reference

(Call-by-Reference)(Call-by-Reference) Needs indirect addressingNeeds indirect addressingPass-by-Value (In Mode)Pass-by-Value (In Mode)

ValueValue

Access PathAccess Path

Page 9: Announcements

April 21, 2004 9 ICE 1341 – Programming Languages (Lecture #15) In-Young Ko

Parameter Passing Methods (2)Parameter Passing Methods (2)

Caller ProgramCaller Program SubprogramSubprogram

int myFunc (int a, char *s) {int myFunc (int a, char *s) {

… …

}}

……char *str = “val”;char *str = “val”;int result = myFunc (10, str);int result = myFunc (10, str);

……

Pass-by-Result Pass-by-Result (Out Mode)(Out Mode)

Pass-by-Reference Pass-by-Reference (Inout Mode)(Inout Mode)

Page 10: Announcements

April 21, 2004 10 ICE 1341 – Programming Languages (Lecture #15) In-Young Ko

Parameter Passing Methods (3)Parameter Passing Methods (3)

Pass-by-Value-ResultPass-by-Value-Result (Inout Mode, (Inout Mode, Pass-by-CopyPass-by-Copy))

= Pass-by-Value + Pass-by-Result= Pass-by-Value + Pass-by-Result

The following generates the similar effect as PVR:The following generates the similar effect as PVR:

int aFunc (int *a) {int aFunc (int *a) { int aVal = *a; int aVal = *a; // copy the value to a local var// copy the value to a local var

…… aVal += 10; aVal += 10; // No direct changes to *a // No direct changes to *a

…… *a = aVal;*a = aVal; // copy the result to the return // copy the result to the return

varvar }}

Page 11: Announcements

April 21, 2004 11 ICE 1341 – Programming Languages (Lecture #15) In-Young Ko

Parameter Passing Methods (4)Parameter Passing Methods (4)

Pass-by-NamePass-by-Name (Inout Mode) (Inout Mode) The actual parameter is The actual parameter is textually substitutedtextually substituted for the for the

corresponding formal parameter in all its occurrencescorresponding formal parameter in all its occurrences Lazy evaluationLazy evaluation ee.g., C macros.g., C macros

#define SQUARE (x) x * x#define SQUARE (x) x * x

……

y = SQUARE (val);y = SQUARE (val); SQUARE (val * val);SQUARE (val * val);

Page 12: Announcements

April 21, 2004 12 ICE 1341 – Programming Languages (Lecture #15) In-Young Ko

Disadvantages of Pass-by-ReferenceDisadvantages of Pass-by-Reference

Slower accessSlower access –– Indirect reference of values Indirect reference of values Actual parameter collisionsActual parameter collisions –– Allowing aliasing Allowing aliasing

e.g. e.g. void fun (int &a, int &b); void fun (int &a, int &b); // subprogram def.// subprogram def. sub1 (x, x);sub1 (x, x); // caller// caller

Array element collisionsArray element collisionse.g. e.g. ssub1 (list[i], list[j]);ub1 (list[i], list[j]); // if i = j ?// if i = j ? sub2 (list, list[i]); sub2 (list, list[i]); // a different one// a different one

Collision between formals and globalsCollision between formals and globalse.g. e.g. int *global;int *global; // a global variable// a global variable

void sub (int *param) {void sub (int *param) { // subprogram def.// subprogram def.……

}} sub (global);sub (global); // caller// caller * AW Lecture Notes

Page 13: Announcements

April 21, 2004 13 ICE 1341 – Programming Languages (Lecture #15) In-Young Ko

Stack Implementation of Stack Implementation of Parameter-PassingParameter-Passing

Call-by-ValueCall-by-Value

Return-by-ValueReturn-by-Value

Pass-by-Value-Pass-by-Value-ResultResult

Pass-by-Pass-by-ReferenceReference

Page 14: Announcements

April 21, 2004 14 ICE 1341 – Programming Languages (Lecture #15) In-Young Ko

Multidimensional Arrays as ParametersMultidimensional Arrays as Parameters

void fun (int matrix[][10]) { void fun (int matrix[][10]) { … }… } The formal parameter must include the The formal parameter must include the column size column size

to make a storage mapping functionto make a storage mapping function Cannot accept matrices with different column sizeCannot accept matrices with different column size

void fun (int *matrix, int rsize, int csize) { void fun (int *matrix, int rsize, int csize) { … }… } Mapping function: Mapping function: * (matrix + (row * csize) + col)* (matrix + (row * csize) + col) Readability is lowReadability is low

void fun (int matrix[][]) {void fun (int matrix[][]) {int rsize = matrix.length;int rsize = matrix.length;int csize = matrix[0].length;int csize = matrix[0].length;

}} // Java and C# // Java and C# Arrays are objects Arrays are objects

Page 15: Announcements

April 21, 2004 15 ICE 1341 – Programming Languages (Lecture #15) In-Young Ko

Parameters that are Subprogram Parameters that are Subprogram NamesNames

Shallow BindingShallow Binding –– the the reference reference environmentenvironment of the call statement is of the call statement is passed to the subprogrampassed to the subprogram

X = 4X = 4 Deep BindingDeep Binding –– the environment of the environment of

the definition of the passed the definition of the passed subprogramsubprogram

X = 1X = 1 Ad Hoc BindingAd Hoc Binding –– the environment the environment

of the call statement that passes of the call statement that passes the subprogram as an actual the subprogram as an actual parameterparameter

X = 3X = 3

functionfunction sub1 () { sub1 () {var x;var x;functionfunction sub2 () { sub2 () { alert(x);alert(x);};};functionfunction sub3 () { sub3 () { var x;var x; x = 3;x = 3; sub4 (sub4 (sub2sub2););};};functionfunction sub4 ( sub4 (subxsubx) {) { var x;var x; x = 4;x = 4; subx ();subx ();

};};x = 1;x = 1;sub3 ();sub3 ();

};}; // JavaScript // JavaScript

Page 16: Announcements

April 21, 2004 16 ICE 1341 – Programming Languages (Lecture #15) In-Young Ko

Design Issues for SubprogramsDesign Issues for Subprograms

1. What 1. What parameter passing methodsparameter passing methods are provided? are provided?

2. Are 2. Are parameter types checkedparameter types checked??

3. Are 3. Are local variables static or dynamiclocal variables static or dynamic??

4. Can subprogram definitions appear in other 4. Can subprogram definitions appear in other subprogram definitions (subprogram definitions (nested definitionsnested definitions)?)?

5. What is the 5. What is the referencing environmentreferencing environment of a passed of a passed subprogram?subprogram?

6. Can subprograms be 6. Can subprograms be overloadedoverloaded??

7. Are subprograms allowed to be 7. Are subprograms allowed to be genericgeneric??