An Example: Pass-by-value-result Vs. Pass-by-reference

2y ago
75 Views
2 Downloads
298.54 KB
15 Pages
Last View : 1m ago
Last Download : 3m ago
Upload by : Mya Leung
Transcription

10/20/16An Example: pass-by-value-result vs.pass-by-referenceprogram foo;var x: int;procedure p(y: int);beginy : y 1;y : y * x;pass- ‐by- ‐value- ‐resultendxybegin(entrytop)22x : 2;p(x);(a5ery: y 1)23print(x);(atp’sreturn)66endpass- ‐by- ‐referencexy223399N.Meng,S.Arthur1Aliases can be created due to passby-reference Given void fun(int &first, int &second),– Actual parameter collisions E.g., fun(total, total) makes first and second to bealiases– Array element collisions E.g., fun(list[i], list[j]) can cause first and secondto be aliases if i j– Collisions between formals and globals E.g., int* global;void main() { sub(global); }void sub(int* param) { } Inside sub, param and global are aliasesN.Meng,S.Arthur21

10/20/16Pass-by-Name Implement an inout-mode parametertransition method The body of a function is interpreted atcall time after textually substituting theactual parameters into the function body The evaluation method is similar to Cpreprocessor macrosN.Meng,S.Arthur3An Example in Algolprocedure double(x);real x;beginx : x * 2;end;Therefore, double(C[j]) is interpreted asC[j] C[j] * 2N.Meng,S.Arthur42

10/20/16Another Example Assume k is a global variable,procedure sub2(x: int; y: int; z: int);begink : 1;y : x;k : 5;z : x;end; How is the function call sub2(k 1, j, i)interpreted?N.Meng,S.Arthur5Disadvantages of Pass-by-Name Very inefficient references Too tricky; hard to read and understandN.Meng,S.Arthur63

10/20/16Implementing Parameter-PassingMethods Most languages use the runtime stack topass parameters– Pass-by-value Values are copied into stack locations– Pass-by-result Values assigned to the actual parameters are placedin the stack– Pass-by-value-result A combination of pass-by-value and pass-by-result– Pass-by-reference Parameter addresses are put in the stackN.Meng,S.Arthur7An Example Function header: void sub (int a, int b,int c, int d)– a: pass by value– b: pass by result– c: pass by value-result– d: pass by reference Function call: main() calls sub(w, x, y, z)N.Meng,S.Arthur84

10/20/16N.Meng,S.Arthur9Design Considerations for ParameterPassing Efficiency Whether one-way or two-way datatransfer is neededN.Meng,S.Arthur105

10/20/16One Software Engineering Principle Access by subroutine code to data outsidethe subroutine should be minimized– In-mode parameters are used whenever nodata is returned to the caller– Out-mode parameters are used when no data istransferred to the callee but the subroutinemust transmit data back to the caller– Inout-mode parameters are used only whendata must move in both directions between thecaller and calleeN.Meng,S.Arthur11A practical consideration in conflictwith the principle Pass-by-reference is the fastest way topass structures of significant sizeN.Meng,S.Arthur126

10/20/16Parameters that are subroutines In some situations, subroutine namescan be sent as parameters to othersubroutines Only the transmission of computation isnecessary, which could be done bypassing a functional pointerN.Meng,S.Arthur13Two complications with subroutineparameters Are parameters type checked?– Early Pascal and FORTRAN 77 do not typecheck– Later versions of Pascal, Modula-2, andFORTRAN 90 do– C and C doN.Meng,S.Arthur147

10/20/16Two complications with subroutineparameters What referencing environment should beused for executing the passed subroutine?– The environment of the call statement thatenacts the passed subroutine(shallow binding)– The environment of the definition of thesubroutine(deep binding)– The environment of the call statement thatpassed it as an actual parameter(ad hocbinding)N.Meng,S.Arthurfunction 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();};15An Example For shallow binding, thereferencing environmentof sub2 is sub4 For deep binding, thereferencing environmentof sub2 is sub1 For ad hoc binding, thereferencing environmentof sub2 is sub3N.Meng,S.Arthur168

10/20/16What is the output of alert(x)? Shallow binding? Deep binding? Ad hoc binding?N.Meng,S.Arthur17Referencing Environment forSubroutine Parameters Deep binding and ad hoc binding can bethe same when a subroutine is declaredand passed by the same subroutine In reality, ad hoc binding has never beenused Static-scoped languages usually use deepbinding Dynamic-scoped languages usually useshallow bindingN.Meng,S.Arthur189

10/20/16An Examplefunction Sent() {print(x);};function Receiver(func) {var x;x 2;};function Sender() {var x;x 1;Receiver(Sent)}; In static-scopedlanguages, Receiver is notalways visible to Sent, sodeep binding is natural In dynamic-scopedlanguages, it is natural forSent to have access tovariables in Receiver, soshallow binding isappropriateN.Meng,S.Arthur19Design Issues for Functions Are side effects allowed?– Ada requires in-mode parameters, and doesnot allow any side effect– Most languages support two-wayparameters, and thus allow functions tocause side effectsN.Meng,S.Arthur2010

10/20/16Design Issues for Functions What types of values can be returned?– FORTRAN, Pascal, and Modula-2: onlysimple types– C: any type except functions and arrays– Ada: any type (but subroutines are nottypes)– JavaScript: functions can be returned– Python, Ruby and functional languages:methods are objects that can be treated asany other objectN.Meng,S.Arthur21Overloaded Subroutine A subroutine that has the same name asanother subroutine in the samereferencing environment, but its number,order, or types of parameters must bedifferent– E.g., void fun(float);void fun(); C and Ada have overloaded subroutinesbuilt-in, and users can write their ownoverloaded subroutinesN.Meng,S.Arthur2211

10/20/16Generic Subroutine A generic or polymorphic subroutine takesparameters of different types on differentactivations An example in C template class Type Type max(Type first, Type second) {return first second ? first: second;}int a, b, c;char d, e, f; c max(a, b);N.Meng,S.Arthurf max(d, e);23Generic Subroutine Overloaded subroutines provide aparticular kind of polymorphism called adhoc polymorphism– Overloaded subroutines need not behavesimilarly Parametric polymorphism is provided by asubroutine that takes generic parametersto describe the types of parameters Parametric polymorphic subroutines areoften called generic subroutinesN.Meng,S.Arthur2412

10/20/16Coroutine A special kind of subroutine. Ratherthan the master-slave relationship, thecaller and callee coroutines are on amore equal basis A coroutine is a subroutine that hasmultiple entry points, which arecontrolled by coroutines themselvesN.Meng,S.Arthur25Coroutine The first execution of a coroutine beginsat its beginning, but all subsequentexecutions often begin at points otherthan the beginning Therefore, the invocation of a coroutineis named a resume Typically, coroutines repeatedly resumeeach other, possibly forever Their executions interleave, but do notoverlapN.Meng,S.Arthur2613

10/20/16An Example The first time co1 is resumed, itsexecution begins at the firststatement, and executes down toresume(co2) (with the statementincluded) The next time co1 is resumed, itsexecution begins at the firststatement after resume(co2) The third time co1 is resumed,its execution begins at the firststatement after resume(co3)sub co1() { resume(co2); resume(co3);}N.Meng,S.Arthur27Coroutine The interleaved execution sequence isrelated to the way multiprogrammingoperating systems work– Although there may be one processor, all ofthe executing programs in such a systemappear to run concurrently while sharingthe processor– This is called quasi-concurrency Coroutines provide quasi-concurrentexecution of program unitsN.Meng,S.Arthur2814

10/20/16Reference[1] Robert W. Sebesta, Concepts ofProgramming Languages, 8th edition, pg.383-434N.Meng,S.Arthur2915

statement after resume(co2) The third time co1 is resumed, its execution begins at the first statement after resume(co3) Coroutine The interleaved execution sequence is related to the way multiprogramming operating systems work – Although there may be one proc

Related Documents:

Print Scan Card Reader LPR* Print Scan Card Reader LPR* 1 Avision MF3200 Pass Pass N/A Pass Pass Pass N/A Pass 2 Avision MF3230 Pass Pass N/A Pass Pass Pass N/A Pass . 19 Brother MFC-7420 MFP Pass Pass N/A Pass Pass Pass N/A Pass 20 Brother MFC

LPR* Print Scan Card Reader LPR* 1 Avision MF3200 Pass Pass N/A Pass Pass Pass N/A Pass 2 Avision MF3230 Pass Pass N/A Pass Pass Pass N/A Pass . 17 Brother MFC-7420 MFP Pass Pass N/A Pass Pass Pass N/A Pass 18

LPR* Print Scan Card Reader LPR* Avision MF3230 Pass Pass* N/A Pass Pass Pass* N/A Pass BenQ CM3000 Pass Pass N/A N/A Pass Pass N/A N/A . Brother MFC-7420 Pass Pass N/A Pass Pass Pass* N/A Pass Canon CanonScan Lide

26 Canon CanonScan Lide 5.0 N/A Pass N/A - N/A Pass N/A - Model Name Update: 03/05/2012 TP-LINK Print Server Compatibility List Windows XP Windows Vista / Windows 7 . 130 Canon MX850 Pass Pass Pass Pass Pass Pass Pass Pass 131 Canon Pixus 990i Pass N/A N/A Pass Pass N/A N/A Pass

Epson ME205 Pass Pass N/A Pass Epson LQ-635C Pass N/A N/A Pass Epson LQ-690C Pass N/A N/A Pass Epson LQ-2190 Pass N/A N/A Pass Epson ME 320 Pass Pass N/A Pass

IEC 61000-4-3: 2010 . IEC 61000-4-4: 1995 . IEC 61000-4-5: 1995 . IEC 61000-4-6: 1996 . IEC 61000-4-8: 1993 . IEC 61000-4-11: 1994 Pass Pass Pass Pass Pass Pass Pass Pass Pass Pass Pass . Test Data: EMC Immunity Test Resu

Nov 16, 2020 · ASTM C1064 - Temperature of Concrete Pass Fail Pass Fail WYDOT 477.0 ASTM C172 - Sampling Freshly Mixed Concrete Pass Fail Pass Fail WYDOT 480.0 ASTM C143 - Slump of Hydraulic-Cement Concrete Pass Fail Pass Fail WYDOT 479.0 ASTM C138 - Density (Unit Weight) and Yield Pass Fail Pass Fail WYDOT 481.0

SanDisk Cruzer Extreme Contour 32GB Pass Glasses 3 Glasses D2 Vanguard Edition Pass Headset Logitech G633 Gaming Headset Pass HDD BUFFALO HD-LB1.0TU2 1TB Pass HUB D-Link DUB-H7 (7 PORT USB 2.0) Pass Transcend TS-RDPP7K Card Reader/HUB(USB2.0) Pass LAN(Wireless) BUFFALO WLI-UC-G300N Pass D-Link DWA-140 (Wireless N) Pass