Presentation is loading. Please wait.

Presentation is loading. Please wait.

Subroutines – parameter passing

Similar presentations


Presentation on theme: "Subroutines – parameter passing"— 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 = a: 1 b = 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 x: /1/ | y = x + y y: /2/ | return '

4 Subroutines – parameter passing
call-by-value example 2 int a = 1; /* global variable */ int subr( int b, int c ) { a = 2*a; b = b + 2; c = c - 1; return( a * b * c ); } void main(void) { int d = 5, e; e = subr( a, d ); Show final values after calls to subr() b: call by value c: call by value 16. a _____ 17. d _____ 18. e _____ 2 5 24

5 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 = a: 1 b = 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 x: /?/ ? | y = x + y y: /?/ ? | return pass ?,? back via stack '

6 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 = a: 1 b = 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 x: /1/ | y = x + y y: /2/ | return pass 2,4 back via stack --'

7 Subroutines – parameter passing
call by value-result example 2 int a = 1; /* global variable */ int subr( int b, int c ) { a = 2*a; b = b + 2; c = c - 1; return( a * b * c ); } void main(void) { int d = 5, e; e = subr( a, d ); Show final values after calls to subr() b: call by value-result c: call by value-result 16. a _____ 17. d _____ 18. e _____ 3 4 24

8 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; 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 changes made using the formal parameter are being executed on the object passed as the actual parameter

9 Subroutines – parameter passing
Parameter passing (cont’d) call-by-reference (cont’d) main a = a: a: /1/ 2 action in subr b = b: 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 x: &a thus a = a | y = x + y y: &b thus b = a + b | return '

10 Subroutines – parameter passing
call by reference example 2 int a = 1; /* global variable */ int subr( int b, int c ) { a = 2*a; b = b + 2; c = c - 1; return( a * b * c ); } void main(void) { int d = 5, e; e = subr( a, d ); Show final values after calls to subr() b: call by reference c: call by reference 16. a _____ 17. d _____ 18. e _____ 4 4 64

11 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 d e

12 Subroutines – parameter passing (tradeoffs)
advantages disadvantages 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) optimizations can pass parameters in registers (not using stack) can sometimes not save/restore registers when executing a leaf routine


Download ppt "Subroutines – parameter passing"

Similar presentations


Ads by Google