CS 342 Lecture -*- Outline -*- * calling (parameter passing) mechanims ** pass by reference (call by reference) the usual mechanism for FORTRAN *** parameters both input (value) and output (storage, result) is this good? ------------- C parameters can be both input and output in FORTRAN SUBROUTINE DIST(D,X,Y) D = ABS(X-Y) RETURN END ------------ *** Implementation pass address of variable (draw picture) Benefits: efficient, because only a reference is passed for each parameter e.g., to pass A(I), compute address of A(I), pass address e.g., pass address of array (i.e., address of A(1)), much more efficient than passing all of array Cost: all references are indirect (extra memory references) *** Insecurity of many FORTRAN implementations (FORTRAN 77 standard prohibits assignment to constants as actual arguments) -area of storage for constants (such as integers 2, 3, etc.) (needed for large literals even on computers with fields in instructions for literals) -passing literal by reference to output parameter would change literal ------------- C insecurity of many FORTRAN implementations PROGRAM OOPS N = MAX3(1,2,3) C the following increments I by 2! I = I+1 PRINT I END FUNCTION MAX3(J,K,L) J = MAX(J,K) MAX3 = MAX(J,L) RETURN END ------------- **** Solutions place constant in temporary variable first (pass reference) place constants in area of memory where cannot be written to without hardware trap (used on Zippy) ** pass by value-result (call by value-result) unusual but permitted by the FORTRAN 77 standard -caller copies value of actual to formal variable -on return, caller copies result value to actual (omit for constants or expressions as actuals) benefits: no indirection preserves security of implementation ** differences mechanisms differ, esp. when have aliases alias created by call by refernce: F(N,N) but not by call by value-result ------------ C difference between pass by reference and pass by value-result PROGRAM CALLBY Z = 3.0 CALL S(Z,Z) PRINT 10, Z 10 FORMAT (F10.6) END SUBROUTINE S(X,Y) X = 1.0 Y = X * Y RETURN END ------------- what is printed with the different mechanisms? reference (on Zippy): 1.0 value-result (left-to-right): 3.0