CSE 3302 Programming LanguagesProcedures vs. FunctionsSyntaxProcedure CallEnvironmentActivation Record for Nested BlocksSlide 7Activation Record for ProceduresSlide 9Slide 10Slide 11Slide 12Parameter Passing MechanismsExamplePass by ValueExample: Pass By ValueAre these Pass-by-Value?Pass-by-Value: PointersSlide 19Pass-by-Value: ArraysSlide 21Pass-by-Value: Java ObjectsSlide 23Pass by ReferenceExample: Pass By ReferencePass-by-Reference: How to minic it in C?It is really pass-by-valuePass-by-Reference: C++ Constant ReferencePass-by-Reference: C++ Reference-to-PointerSlide 30Pass-by-Reference: C++ Reference-to-ArrayPass by Value-ResultExample: Pass By Value-ResultUnspecified IssuesPass by NameExample: Pass By NamePass-by-Name: Side EffectsSome VariantsComparisonsCSE 3302 Programming LanguagesChengkai Li, Weimin HeSpring 2008Control IIProcedures and EnvironmentsLecture 10 – Control II, Spring 20081CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008Procedures vs. Functions•Function:•no side effect•return a value•Function call: expression•Procedure:•side effect, executed for it•no return value•Procedure call: statement•No clear distinction made in most languages–C/C++: void–Ada/FORTRAN/Pascal: procedure/functionLecture 10 – Control II, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 20082Syntax•Terminology:–body–specification interface •name•type of return value•parameters (names and types)Lecture 10 – Control II, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 20083int f(int y) {int x;x=y+1;return x;}int f(int y); //declarationint f(int y){ //definitionint x;x=y+1;return x;}Procedure Call•Caller: Callee:… int f(int y){f(a); int x; … if (y==0) return 0; x=y+1; return x; }•Control transferred from caller to callee, at procedure call •Transferred back to caller when execution reaches the end of body•Can return earlyLecture 10 – Control II, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 20084EnvironmentCSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 20085stackstatic(global) areaheap(unallocated)manually-allocated spacesunder the control of programmerautomatically-allocated spaces(local variables, procedures (chapter 8)under the control of runtime systemboth for dynamic binding•Environment: binding from names to their attributesLecture 10 – Control II, Spring 2008Activation Recordfor Nested BlocksLecture 10 – Control II, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 20086•Activation record: memory allocated for the local objects of a block–Entering a block: activation record allocated–Exit from inner block to surrounding block: activation record released•int x; //global{ int x,y; x = y*10; { int i;i = x/2; } }xxyActivation Recordfor Nested BlocksLecture 10 – Control II, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 20087int x; //global{ int x,y; x = y*10; { int i;i = x/2; } }xxyiX: Nonlocal variable,in the surrounding activation recordActivation Recordfor ProceduresLecture 10 – Control II, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 20088int x; //globalvoid B(void) { int i; i = x/2;}void A(void) { int x,y; x = y*10; B();}main() { A(); return 0;}xxyActivation Recordfor ProceduresLecture 10 – Control II, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 20089int x; //globalvoid B(void) { int i; i = x/2;}void A(void) { int x,y; x = y*10; B();}main() { A(); return 0;}xxyix: global variable indefining environmentNeed to retain information in calling environmentActivation Recordfor ProceduresLecture 10 – Control II, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 200810int x; //globalvoid B(void) { int i; i = x/2;}void A(void) { int x,y; x = y*10; B();}main() { A(); return 0;}xxyix: global variable indefining environmentx,y: local variable in calling environmenti: local variable in called environmentActivation Recordfor ProceduresLecture 10 – Control II, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 200811int x; //globalvoid B(void) { int i; i = x/2;}void A(void) { int x,y; x = y*10; B();}main() { A(); return 0;}Can only access global variables indefining environmentNo direct access to the local variables in the calling environment(Need to communicate through parameters)xxyiProcedure Call•Caller: Callee:… int f(int a){f(i); ...; … ...a...; ...;}Lecture 10 – Control II, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 200812actual parameter / argument formal parameter / argumentParameter Passing Mechanisms:•When and how to evaluate parameters•How actual parameter values are passed to formal parameters•How formal parameter values are passed back to actual parametersParameter Passing Mechanisms•Pass/Call by Value•Pass/Call by Reference•Pass/Call by Value-Result•Pass/Call by NameLecture 10 – Control II, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 200813Example•What is the result?void swap(int a, int b) {int temp;temp = a; a = b; b = temp;}main(){int i=1, j=2;swap(i,j);printf(“i=%d, j=%d\n”, i, j);}Lecture 10 – Control II, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 200814•It depends…Pass by Value•Caller: Callee:… int f(int a){f(i); ...a...; … }Lecture 10 – Control II, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 200815i a•Most common one•Replace formal parameters by the values of actual parameters•Actual parameters: No change•Formal parameters: Local variables (C, C++, Java, Pascal)Example: Pass By Valuevoid swap(int a, int b) {int temp;temp = a; a = b; b = temp;}main(){int i=1, j=2;swap(i,j);printf(“i=%d, j=%d\n”, i, j);}Lecture 10 – Control II, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 200816i1j2abi1j2a1b2i1j2a2b1Are these Pass-by-Value?•C:void f(int *p) { *p = 0; }void f(int a[]) { a[0]=0; }•Java:void f(Vector v) { v.removeAll(); }Lecture 10 – Control II, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,
View Full Document