You are on page 1of 4

2/11/13

Fortran Functions and Subroutines

Subroutines and Functions


A complex and large computer program can be broken up into many more manageable subprograms. There is only one "main" program, the other subprograms are called subroutines or functions. The subprograms can be larger than the main program and they can even manage other subprograms, thus, there are really no restriction on how the subprograms should be organized, the concept is to help the programmer to write the program in smaller and reusable modules.

FUNCTIONS
Functions are designed so they can appear in an algebraic expression, for example, the expression
qascsx-i()/qt10aa(+) =b(o()sny)sr(.+tnxy)

contains 5 different calls to intrinsic functions: abs, sin, cos, sqrt and atan. Intrinsic functions are those provided by the Fortran library. The program can also write specific functions which are not provided by the standard Fortran library. To write a function in fortran, the subprogram unit should start with a function statement which specifies the function name. The name is also a "typed" variable which will return the result of the function. For example, the intrinsic function "abs" can be written as follows:
ra fnto asx el ucin b() ra x el i(.t00te fxl..)hn asb=x es le asx b= edf ni rtr eun ed n

The first line defines the function name to be "abs" and its type to be real. It also indicates the function has one argument, x, and x is defined to be real on the second line. The result of the function, in Fortran, must be stored in the memory location specified by the name of the function, in this particular example, the variable is abs. The return statement is like a "go to" statement and it returns the control back to the program location where the call was made. There can be more than one return statements in a function. The last line of the function subprogram is the "end" statement. Here are a few more examples of functions: (1) A function which returns the remainder of an integer division:
www-classes.usc.edu/engr/ce/108/text/fbk05.htm 1/4

2/11/13

Fortran Functions and Subroutines

itgrfnto rmi(,) nee ucin eanxy itgrxy nee , rmi=-/* eanxxyy rtr eun ed n

If a program statement,
mrmi(17 =ean1,)

is executed, the result of m would be 4. The instrinsic function mod(11,7) would have provided the same result, mod means modulo. (2) A function which calculates the Bessel's function of the first kind and of zeroth order:
ra fnto j() el ucin 0x ra xsmtr, el ,u,emi tr=. em10 smtr u=em i00 =. ii10 =+. tr=tr*.5(/)* em-em02*xi*2 smsmtr u=u+em i(b(em.t1e6g t 1 fastr)g..-)o o j=u 0sm rtr eun ed n

Note that the answer, sum, was not printed out, it was stored in the variable j0 and returned to the calling program. The calling program can be either the main program or another subprogram. IMPORTANT....be sure the variable j0 is specified as real wherever the call to j0 is made. In the function, the "real function j0(x)" statement defines j0 to be real, but in other subprogram units, j0 is an integer variable unless it is specified as real. This is one of the dangers of Fortran, all the newer languages require all the variables to be specified. (In example (1), the variable "remain" should also be specified as "integer" in the calling program, otherwise, it is by default of "real" type.)

SUBROUTINES
Unlike functions, subroutines are used as a separate program unit. To invoke a subroutine, a call must be issued, for example, the statement
cl ti(,,,) al rgxsct

invokes a subroutine call to the subprogram trig. The variables: x, s, c and t are the arguments. The arguments serve as the interface between the calling program and the subprogram. For this particular example, x is the input value for the subprogram and
www-classes.usc.edu/engr/ce/108/text/fbk05.htm 2/4

2/11/13

Fortran Functions and Subroutines

the variables: s,c and t return the values of sin(x), cos(x) and tan(x), respectively, i.e., s, c and t are the output values. The subroutine trig can be written as
sbotn ti(,ic,n uruie rgxs,st) ra xs,st,emitrc, el ,ic,ntrs,emsi lgclcneg oia ovre trs= emix s=emi itrs trc=. ems10 c=ems strc i00 =. ii10 =+. trs=trs**/(.i1)(.i) emi-emixx(2*+.*2*) trc=trc**/(.i*2*-.) ems-emsxx(2*)(.i1) s=itrs is+emi c=strc sc+ems cneg=astrs)l..-)ad(b(ems.t1e7 ovre(b(emi.t1e7.n.astrc)l..-) i(ntcneg)ot 1 f.o.ovreg o t=ic ns/s rtr eun ed n

The above example shows how more than one answer can be returned to the calling program. Note, both the sine and the cosine series are computed on the same loop. The value for tangent is calculated as the quotient, sine over cosine; no check is made for the case when cos(x) is zero. It is clear that the names of the arguments are different for the call statement,
cl ti(,,,) al rgxsct

and the subroutine statement


sbotn ti(,ic,n uruie rgxs,st)

This is entirely acceptable and in many cases, preferred. The arguments on the subroutine statements are called, "dummy arguments," i.e., they possess no memory location. The variable, s, is assigned a memory location to store a real number; but the dummy argument, si, has no memory location, it is just an alias of the variable, s. If si is changed in the subroutine trig, s is also changed in the calling program, i.e., s and si are pointers to the same memory location. Similarly, c and cs points to the same memory location. This method of passing the arguments is named "CALL-BYREFERENCE," meaning the arguments reference the same memory location. Pascal and C and most the newer languages used "CALL-BY-VALUE," a copy of the argument is made before it is passed along to the subprogram, therefore, if the argument is changed inside the subprogram, it will not affect the original argument in the calling program. There are advantages to both methods.

www-classes.usc.edu/engr/ce/108/text/fbk05.htm

3/4

2/11/13

Fortran Functions and Subroutines

The Unive rsity of Southe rn C alifornia doe s not scre e n or control the conte nt on this we bsite and thus doe s not guarante e the accuracy, inte grity, or quality of such conte nt. All conte nt on this we bsite is provide d by and is the sole re sponsibility of the pe rson from which such conte nt originate d, and such conte nt doe s not ne ce ssarily re fle ct the opinions of the Unive rsity adm inistration or the Board of Truste e s

www-classes.usc.edu/engr/ce/108/text/fbk05.htm

4/4

You might also like