Presentation is loading. Please wait.

Presentation is loading. Please wait.

Subroutines – parameter passing passing data to/from a subroutine can be done through the parameters and through the return value of a function subroutine.

Similar presentations


Presentation on theme: "Subroutines – parameter passing passing data to/from a subroutine can be done through the parameters and through the return value of a function subroutine."— Presentation transcript:

1 Subroutines – parameter passing passing data to/from a subroutine can be done through the parameters and through the return value of a function subroutine parameter passing methods include: call-by-value - input parameters (declared with "IN") in Ada - default method for parameters in C, C++, and Pascal - parameters of primitive type in Java call-by-result - output parameters (declared with "OUT") in Ada

2 Subroutines – parameter passing Parameter passing methods (cont’d): call-by-value-result - in/out parameters (declared with "IN OUT") in Ada call-by-reference - large array parameters in Ada - array parameters in C and C++ - reference parameters (declared with "&") in C++ - reference parameters (declared with "VAR") in Pascal - object parameters in Java - default method for parameters in Fortran

3 Subroutines – parameter passing Parameter passing (cont’d) call-by-value – copy values of actual parameters into memory locations of formal parameters before executing the body of the subroutine; do nothing on return main a = 1 a: 1 b = 2 b: 2 call subr(a,b) pass 1,2 via stack print a,b print 1,2 subr(x,y) copy 1,2 into x,y ^ x = x + 1 x: /1/ 2 | y = x + y y: /2/ 4 | return ---------------------'

4 Subroutines – parameter passing Parameter passing (cont’d) call-by-result - do nothing prior to executing the body of the subroutine; copy the final values of the formal parameters into the memory locations of the actual parameters on return main a = 1 a: 1 b = 2 b: 2 call subr(a,b) pass nothing receive ?,? from subr into a,b a: /1/ ? b: /2/ ? print a,b print ?,? subr(x,y) copy ?,? into x,y ^ x = x + 1 x: /?/ ? | y = x + y y: /?/ ? | return pass ?,? back via stack -------'

5 Subroutines – parameter passing Parameter passing (cont’d) call-by-value-result - perform copying of values both before and after executing the body of the subroutine main a = 1 a: 1 b = 2 b: 2 call subr(a,b) pass 1,2 via stack receive 2,4 from subr into a,b a: /1/ 2 b: /2/ 4 print a,b print 2,4 subr(x,y) copy 1,2 into x,y ^ x = x + 1 x: /1/ 2 | y = x + y y: /2/ 4 | return pass 2,4 back via stack --'

6 Subroutines – parameter passing Parameter passing (cont’d) call-by-reference – pass the addresses of the actual parameters – copy these addresses into the memory locations of the formal parameters – on each reference to a formal parameter in the body of the subroutine, perform an indirect reference to the corresponding actual parameter; o i.e. the formal parameter is an alias of the actual parameter, thus both the formal and actual parameter "name" refer to the same object o changes made using the formal parameter are being executed on the object passed as the actual parameter

7 Subroutines – parameter passing Parameter passing (cont’d) call-by-reference (cont’d) main a = 1 a: 1 a: /1/ 2 action in subr b = 2 b: 2 b: /2/ 4 action in subr call subr(a,b) pass &a,&b via stack print a,b print 2,4 subr(x,y) copy &a,&b into x,y ^ x = x + 1 x: &a thus a = a + 1 | y = x + y y: &b thus b = a + b | return ------------------------------------'

8 Subroutines – parameter passing Consider the following code: int a = 5; /* global variable */ int subr( int b, int c ){ a = 4*a; b = b + 3; c = c + 2; return( a + b + c ); } void main(void){ int d = 1, e; e = subr( a, d ); } Show final values after calls to subr() for the variables listed below, by column, according to the specified parameter passing methods. (18 pts. total) b:call by value b:call by value-result b:call by reference c:call by value c:call by value-result c:call by reference a 20 8 23 d 1 3 3 e 31 31 49

9 Subroutines – parameter passing consider Fortran's call-by-reference applied to a constant parameter - what should the following code print? SUBROUTINE ADDONE( I ) I = I + 1 RETURN END... ADDONE( 2 ) WRITE(6,10) 2 10 FORMAT('CONSTANT 2 =',I1)

10 Subroutines – parameter passing (tradeoffs) optimizations can pass parameters in registers (not using stack) can sometimes not save/restore registers when executing a leaf routine advantagesdisadvantages Call-by- value loads and stores operate directly on formal parms copying overhead Call-by- reference no copying, allows subr to change the values of the actual parms indirect reference through addresses in formal parms to actual parms (i.e., you have to chase pointers)


Download ppt "Subroutines – parameter passing passing data to/from a subroutine can be done through the parameters and through the return value of a function subroutine."

Similar presentations


Ads by Google