11.07.2015 Views

Fortran 90 Handbook

Fortran 90 Handbook

Fortran 90 Handbook

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

2SUBROUTINE TOTALCOMMON / CB / ITOTALITOTAL = 0DO 8 I = 1, 100ITOTAL = ITOTAL + I8 CONTINUEENDTo describe even these simple cases and appreciate how they all work alreadyrequires the introduction of some terms and concepts. To precisely describe thephenomenon that the variable ITOTAL in the subroutine is not known outsidethe subroutine, the concept of scope is used. The scope of the variable ITOTALis the subroutine and does not include the main program.It is a common misconception that the scope of a variable in a common block isglobal; this is not the case. The scope of the variable ITOTAL is just thesubroutine, whereas the scope of the variable M is just the main program.However, the scope of the common block name CB is global. It is associationthat is used to describe the connection between M in the main program andITOTAL in the subroutine. In one case it is argument association and in theother it is storage association.To summarize, very roughly, the scope of a variable is that part of the programin which it is known and can be used. Two variables may have the same nameand nonoverlapping scopes; for example, there may be two completelydifferent variables named X in two different subprograms. Association ofvariables means that there are two different names for the same object; thispermits sharing values under certain conditions.With arguments available, it is natural to generalize the computationsomewhat to allow the upper limit of the sum (100 in the example) to vary.Also, a function is more natural than a subroutine, because the object of thecomputation is to return a single value. These changes produce the following<strong>Fortran</strong> 77 program.PROGRAM PTOTALINTEGER TOTALPRINT *, TOTAL (100)ENDFUNCTION TOTAL (N)INTEGER TOTALTOTAL = 0DO 8 I = 1, N<strong>Fortran</strong> Concepts and Terms 31Copyright © 1992 J. Adams, W. Brainerd, J. Martin, B. Smith, and J. Wagener

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!