Writing Your own Functions and Subroutines

Assignment :

Work on your Homework, and read a tutorial on dbx.

Now that you know something about using functions, it's worth showing you how to write your own. Why? One simple reason is to save programing work. As your programs become more complicated you will often find that there are certain calculational sequences which are repeated many times. One simple example of repeated code is provided in htcoef.f. In this simple example the heat transfer coefficients are each calculated with just two lines of Fortran. However, for more complicated boiling flows, this could be closer to 1000 lines rather than 2. Even at 2 lines per evaluation, there is an advantage in isolating the calculation in a single function subprogram as done in htcoef1.f. If I decide to improve my approximation or implement the block of programming more efficiently (htcoef3.f), I generally need only to alter the contents of the function.

Before going any further I should introduce some jargon related to subprograms. In my examples when the function is used:

h=htc(Re,D,k,Pr) the string "Re,D,k,Pr" is often referred to as the "calling sequence" of the function reference. In the first line of the function subprogram:

function htc(Re,Hd,k,Pr) the string "Re, Hd,k,Pr" is often called the function's "argument list." Re, D or Hd, k, and Pr are called arguments.

When writing and using a function there are several simple rules that must be followed.

  1. The first line of the function subprogram must be in the form:
function name (arg1,arg2, ...,argn)

integer name ii=name(x,y,z) stop end function name(x1,y1,z1) integer name name=int(x1+y1+z1) return end You should match arguments one for one in count and type between the reference to the function and the function itself. Suppose I have written a function named "test1", whose first argument is declared as a real number within the function, the second as an integer, and the third as a real. Anywhere this function is used within my program, the first argument of the calling sequence must be of type real, the second type integer, and the third real. Results of failure to maintain this consistency can be seen in the last heat transfer coefficient evaluation of htcoef2.f. The Fortran 90 INTERFACE statement can be used to protect you from argument inconsistencies.
  • Names used for argument need not match between the function argument list and the calling sequence.
  • A variable not in the argument list, but with the same name used in another subprogram (or the main program) list does not refer to the same address in memory as its namesake elsewhere, unless you take special action (common blocks and modules). Subprograms are completely separate entities with very limited means of communicating information. You don't worry about conflicts in variable names or label values from a program to a subprogram or between subprograms.
  • Sometimes you see forms like "integer function name", "double precision function name", or "real function name". All are legitimate and save you the type declaration of "name" within the function.
  • Values of arguments can be changed within the function, providing a means for a function to generate more information for use elsewhere in the program.
  • function test (a,b) real test,a,b test=.02*sin(a) b=.03*cos(a) return end You can do virtually anything within a function that you can in a main program.

    What happens when you use a function

    The machine instructions that implement a function contain the following steps. The locations associated with each argument in the calling sequence are stored in a location were the function can find them. This is an important but subtle point. In Fortran, the locations of arguments are passed to the function. In C, the values associated with the arguments are passed to the function. The memory location of the next machine instruction after the function evaluation is also stored where the function can find it. A branch instruction is executed to the machine instructions that constitute the function. These are executed using the arguments as needed. The final value of the function is stored in a known location, and a branch is performed to the instruction address stored before function execution.


    Fortran also provides a way to group and use blocks of programming without returning a value as done with a function. This construct is called a SUBROUTINE. It is very similar to a function, with the following exceptions.

    1. No value is associated with a subroutine name. Hence, the subroutine name is not included in a type statement, and no line exists within the subroutine with the subroutine's name to the left of an "=".
    2. The contents of a subroutine are executed with a CALL statement. See htcoef2.f and htcoef3.f for examples.

    Review Questions

    Test your knowledge of this material with some review questions.

    Up one level / Home

    Maintained by John Mahaffy : jhm@cac.psu.edu