1 / 9

Subroutines – parameter passing

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

Download Presentation

Subroutines – parameter passing

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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; • 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

  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 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)

  9. Subroutines – parameter passing (tradeoffs) optimizations can pass parameters in registers (not using stack) can sometimes not save/restore registers when executing a leaf routine

More Related