Subroutine

Statement Purpose

The subroutine statement is the first statement in a subroutine subprogram.

Examples and Rules of Use

In writing a subroutine, you always need to begin it with the subroutine statement. It is essential so that the computer knows where to transfer control to when a call is issued to the subroutine. The subroutine statement should take on the following basic format,
		Subroutine name (dummy argument list)
Where "name" is the whatever you are calling your subroutine and the dummy argument list is a (comma delimited) list of variable names that are the local names for variables in the corresponding position for any call to that subroutine. An example of a subroutine that could be used to input some data is given below to show how the subroutine statement could be used in a real life case.
		Subroutine input( fname,n )
		implicit none
		character(len = *) fname
		integer n
		print *, 'Name of file containing data:'
		read *, fname
		print *, 'Number of iterations in solution:'
		read *, n
		return
		end 
If you are not yet familiar with some of the code used in the character declaration and the print statement, don't worry as you will be by the end of the semester.

One final thing should be noted about the subroutine statement. If you get to an advanced level of programming, you may reach a point where you discover that you need to write recursive procedures. A recursive procedure is a procedure which can issue a call itself. This is normally not permissible in FORTRAN unless you specify the subroutine to be recursive in the following manner.
		recursive subroutine name (dummy argument list)
I advise think carefully before writing recursive subroutines. A fresh copy of the subroutine must be generated at each call, which can eat up tons of computer time. They only become valuable when used in certain advanced computer applications.

For additional examples see

roof.f and fall3.f

For more information on subroutines

Read the lecture on modular programs

Up one level / Home


Written by Jason Wehr : jcw142@psu.edu and Maintained by John Mahaffy : jhm@cac.psu.edu