Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

function args by reference or value?

Status
Not open for further replies.

bobo12

Programmer
Dec 26, 2004
74
US
suppose we have a call...
CALL ALPHA1(SN,SCOR1,SCOR2,SUM,SUM2,SUMXY,ALP,NI,IT,KK3,IS)

SUBROUTINE ALPHA1(SN,SUMI,SUMI2,SUM,SUM2,SUMXY,ALPH,ISUM,
1 IT,KK3,IS)

why change the 2 names(SCOR1,SCOR2) in the parameter arg list and keep everything else the same? are there any general reasons for doing this.

ok here is my ?, are params passed by reference or value? i assume by reference in f77 right?

so if by reference, if a change was made to SUMI, is this change automatically reflected to SCOR1. iow, if i set SUMI=1, is SCOR1=1 after the function is called,since it's by reference?

is the declaration above same as...
CALL ALPHA1(SN,SCOR1,SCOR2,SUM,SUM2,SUMXY,ALP,NI,IT,KK3,IS)

SUBROUTINE ALPHA1(SN,SCOR1,SCOR2,SUM,SUM2,SUMXY,ALP,NI,
1 IT,KK3,IS)



 
First question: no idea whatsoever. Programmers do what they please.

All parameters are passed by reference.

Declarations are the same provided the same identifiers are used within the routine.
 
In Fortran 77 (and F90 too) we have by reference-value parameters. Suppose we have:
Code:
subroutine sub(n1,n2)
integer n1, n2
...
n1 = 1
...
n2 = 2
...
end
...
integer n
call sub(n,n)
Now we have a problem (it's a wrong code!). In the sub body no any implicit linkage between n1 and n2 formal (and actual) parameters. Only after sub termination we have rights to get changed (in the body) values of n. In that case we don't know if n = 1 or n = 2. So sub(n,n) call introduces wrong aliasing with n1/n2 reference to n.

By reference-value means that a compiler can copy any input parameter in a local temp var then returns its changed value by reference at return time. Of course, it can get/put parameters by reference on any access - it's an implementation-dependent issue.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top