Functions and Subroutines

Functions and subroutines are FORTRAN's subprograms. Most problems that require a computer program to solve them are too complex to sit down and work all the way through them in one go. Using subprograms allows you to tackle bite size pieces of a problem individually. Once each piece is working correctly you then put the pieces together to create the whole solution. To implement functions and subroutines, first write a main program that references all of the subprograms in the desired order and then start writing the subprograms. This is similar to composing an outline for an essay before writing the essay and will help keep you on track.

Functions

The purpose of a function is to take in a number of values or arguments, do some calculations with those arguments and then return a single result. There are some functions which are written into FORTRAN and can be used without any special effort by you, the programmer. They are called intrinsic functions. There are over 40 intrinsic functions in FORTRAN and they are mainly concerned with mathematical functions.

The general way to activate a function is to use the function name in an expression. The function name is followed by a list of inputs, also called arguments, enclosed in parenthesis:

        answer = functionname (argumentl, argument2, . . .

Example

PRINT*, ABS (T) The compiler evaluates the absolute value of T and prints it out
Y = SIN (X) + 45

The compiler calculates the value of sin x, adds 45 then puts the result into the variable Y, where x is in radians.

M=MAX(a,b,c,d)

The compiler puts the maximum value of a, b, c and d into the variable M. If a=2, b=4, c=1 and d=7, then M would have a value of 7.

C=SQRT ( a* * 2 +b* * 2 ) The compiler evaluates the expression, a**2+b**2, sends that value to the SQRT function, and places the answer in the variable

As shown by the MAX function example above, a function may have one or more arguments but will only give one result. Also, as shown by the SQRT function example above, the argument for a function does not have to be a variable. It can be an expression or even a constant if you want to reference it again. One last item to remember, you must use result of a function call in an assignment statement or a PRINT statement, as shown in the examples above.

External Functions

The intrinsic functions in FORTRAN are useful but there will be a time when there is no intrinsic function to meet your needs. When this occurs you may write your own function subprogram.

You have to do one thing in the main program to use an external function. You need to declare the function name in the variable declaration section. Function names follow the same rules as for variable names: less than six letters or numbers and beginning with a letter. Because of this, function names should not be used as variable names. Once that is done and the function is written, activating it is just like activating an intrinsic function.

Now you are ready to write your function. There are a few rules for writing external functions:

The example program below shows how to write an external function which calculates the average of three numbers. Note the argument list in the main program does not use the same variable names as the argument list in the function. This is not a problem because a function is a self contained entity whose only tie with the main program is the order of the values in the argument list. So in the first reference to the function, the value in A (5.0) gets transferred to x, the value in B (2.0) to Y and the value in c (3.0) to z. However, in the third reference to the function, it is the squared values (25.0, 4.0, 9.0) that are transferred to x, Y and z respectively in the function. Note also that the variable SUM is used only in the function and therefore is declared only in the function.

Example Program
      PROGRAM FUNDEM
C     Declarations for main program
      REAL A,B,C
      REAL AV, AVSQ1, AVSQ2
      REAL AVRAGE
C     Enter the data
      DATA A,B,C/5.0,2.0,3.0/

C     Calculate the average of the numbers
      AV = AVRAGE(A,B,C)
      AVSQ1 = AVRAGE(A,B,C) **2
   	  AVSQ2 = AVRAGE(A**2,B**2,C**2)

	  PRINT *,'Statistical Analysis'
      PRINT *,'The average of the numbers is:',AV
      PRINT *,'The average squared of the numbers: ',AVSQl
      PRINT *,'The average of the squares is: ', AVSQ2
      END

     REAL FUNCTION AVRAGE(X,Y,Z)
     REAL X,Y,Z,SUM
     SUM = X + Y + Z
     AVRAGE = SUM /3.0
     RETURN
     END

Subroutines

You will want to use a function if you need to do a complicated calculation that has only one result which you may or may not want to subsequently use in an expression. Recall the external function example program where the average was called and then squared in one line. Subroutines, on the other hand, can return several results. However, calls to subroutines cannot be placed in an expression.

In the main program, a subroutine is activated by using a CALL statement which include the subroutine name followed by the list of inputs to and outputs from the subroutine surrounded by parenthesis. The inputs and outputs are collectively called the arguments.

A subroutine name follows the same rules as for function names and variable names: less than six letters and numbers and beginning with a letter. Because of this, subroutine names should be different than those used for variables or functions.

As with functions, there are some rules for using subroutines. Keep these in mind when writing your subroutines:

One way of indicating which variables are inputs and which are outputs is to put the inputs on the first line, use a continuation marker and put the outputs on the second line. See the example program for an application of this programming style.

All variables used by the subroutine, including the arguments, must be declared in the subroutine. The subroutine name is not declared anywhere in the program.

A subroutine is finished off with a RETURN and an END statement.

Exercise 4: Subroutines

In larger programs it is good programming style to include after the FUNCTION or SUBROUTINE statements comments explaining the meanings of the arguments and what the subprogram does.

A hint when you are debugging your programs: When extraordinary, incorrect numbers start appearing from nowhere as your program runs, you probably have not got your subroutine arguments in the right order in either the main program or in the subroutine. The same trick applies to functions. Example Program
	PROGRAM SUBDEM 
        REAL A,B,C,SUM,SUMSQ 
        CALL INPUT( + A,B,C)
   	CALL CALC(A,B,C,SUM,SUMSQ)
   	CALL OUTPUT(SUM,SUMSQ)
   	END

   	SUBROUTINE INPUT(X, Y, Z)
   	REAL X,Y,Z
   	PRINT *,'ENTER THREE NUMBERS => '
 	READ *,X,Y,Z
 	RETURN
 	END

 	SUBROUTINE CALC(A,B,C, SUM,SUMSQ)
   	REAL A,B,C,SUM,SUMSQ
   	SUM = A + B + C
   	SUMSQ = SUM **2
   	RETURN
   	END

   	SUBROUTINE OUTPUT(SUM,SUMSQ)
   	REAL SUM, SUMSQ
   	PRINT *,'The sum of the numbers you entered are: ',SUM
   	PRINT *,'And the square of the sum is:',SUMSQ
   	RETURN
   	END

Now go to the exercises and work your way through the subprograms and functions.


Work your way through the following components attempting the exercises as you come across them:

| Programs | Variables | Arithmetic Operations | Input and Output | Looping in Programs | Arrays in Programs | Checking variables | Subprograms and functions |