Subroutines

 

References:     Section 9 of the FORTRAN95 Manual and the Fortran Lecture4

 

            Like user-defined functions, subroutines are self-contained, separate subprograms. Through the argument list of a subroutine, data can be exchanged between the subroutine and the program which calls the subroutine.

            A subroutine is invoked by

                                    CALL subroutine-name (argument-list)

            The general form of a subroutine program is

SUBROUTINE subroutine-name (argument-list)

[IMPLICIT NONE]

type declarations

executable statements

END [SUBROUTINE [name]]

 

            For example:

PROGRAM EXAMPLE

PRINT *, 'Input X, Y'

READ *, X, Y

CALL DISTANCE( X, Y, RADIUS )

PRINT *, 'Distance = ', RADIUS

END PROGRAM EXAMPLE

!=========================

SUBROUTINE DISTANCE( A, B, R )

R = SQRT( A ** 2 + B ** 2 )

END SUBROUTINE DISTANCE

 

            In the main program, X and Y obtain values from keyboard before the subroutine DISTANCE is called. Data in X and Y are passed on to A and B in the subroutine (RADIUS is not initialized; therefore no data is transferred to R). In the subprogram, there is no change in A and B, but variable R has been given a new value, R = SQRT( A ** 2 + B ** 2 ).  When returning back to the main program, values in A, B and R are passed on to X, Y and RADIUS in the main program. After the CALL statement, X and Y have same values as before and RADIUS has the distance calculated by X and Y.

           

            In our examples, the programs will be much simpler and do the same job without using either functions or subroutines. Why do we need functions or subroutines? Why do we deliberately separate a part of the main program and put it in a subprogram? The advantages of using user-defined functions or subroutines are shown

            1) when some of the functions which are complicated and need to be repeated more than once. Using a subprogram can avoid repeating same codes again and again.

            2) when the main program is big (some programs can have thousands of lines). Using subprograms can keep the main program short and well structured. The main task of a program can be divided into a number of small tasks. Each small task can be dealt with by a subprogram. 

 

            Another advantage of using subprograms is that subprograms can be easily adopted to be used in another program, since the variables used in the subprograms are independent of the main program. The arguments in the subprogram statement are called dummy arguments: they exist only for the purpose of defining this subprogram and have no connection to other variables of the same name in other program units.

 

Exercises

1. In the example program, what is the subroutine name? How many arguments? What are the arguments in the main program and what are the arguments in the subroutine?

2. If the arguments in the subroutine are changed to X, Y, RADIUS, do they share same memory location as those in the main program? Why?

3. In the main program, which variables are used to pass data into the subprogram and which variables are used to get data from the subprogram?

4. (optional) Program Prog16 in Fortran Lecture4 is a bit too long. Split is into 5 different subroutines:

                        INPUT: input data

                        AVERAGE: calculate the average

                        ABOVE: find the number of data which are above the average

                        SORT: sort the data in a numerical order

                        OUTPUT: output the sorted data


5. Copy Prog18 in Fortran Lecture4 to U: drive, save as Prog18.f95 and execute it (optional). In Prog18.f95,  t,d,v,a are four variables in the main program, what are the corresponding variables in the subroutine trak? In the subroutine, which variables are used for getting data from the main program and which are for sending data back to the main program?

6. Modify Prog18.f95 (save as Prog18_modified.f95), so the main program and the subroutine are merged into a single main program.

7. Modify the mprod.f95 (save as mprod_sub.f95), so three subroutines are used.

            INPUT3- for reading a 3x3 array

            PROD3- for calculating the multiplication of two 3x3 matrices

            SAVE3- for saving a 3x3 matrix in a file

   In the main program, the subroutine INPUT3 is called twice to read data for the two 3x3 arrays; PROD3 is called to calculated the product of the two matrices and the result matrix is returned to the main program; and SAVE3 is called to save the result matrix in a file. The display of result matrix on the PC screen is carried out in the main program.

8. List all the arguments in the subroutines: INPUT3, PROD3 and SAVE3, and indicate the intended data transfer directions of those arguments.

9. Write the output of the following program. What are the data transfer directions of the arguments in the subroutine (sub1)? What does the function (func1) do in the program?

 

      Program testsub

 

      Real x(3), y(3), z(3)

 

      Do 100 i = 1, 3

            X(i) = i

            y(i) = 2.0*i

100   continue

 

      call sub1(x,y,z)

 

      do 200 i = 1, 3

            write(*,*) x(i), y(i), z(i)

200   continue

 

      X0 = func1(x)

      Y0 = func1(y)

      Z0 = func1(z)    

write(*,*)

      write(*,*) x0, y0, z0

 

      stop

      end program testsub

!-----------------------------------

      function func1(x)

      real x(3)

      sum = 0.0

      do i = 1, 3

      sum = sum + x(i)

      end do

      func1 = sum/3.0

      return

      end

!-------------------------------------

      subroutine sub1(a,b,c)

      real a(3), b(3), c(3)

      do 100 i = 1, 3

            c(i) = a(i) + b(i)

100   continue

      return

      end