Thursday, June 23, 2011

Fortran 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:

  • Function subprograms and any other subprograms are placed after the END statement of the main program.
  • They are started with a line that includes the type of value the function will return, the function name, and the list of arguments the function takes as inputs.
  • Any variables the function uses, including the arguments, must be declared in the function right after the first line. The function name is not declared within the function.
  • You must use the function name in an assignment statement within the function. This is how the compiler knows which value to pass back to the main program.
  • A function must finish with RETURN and END statements.

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:

  • You do not need to declare the subroutine name in the main program as you do with a function name.
  • They begin with a line that includes the word SUBROUTINE, the name of the subroutine, and the arguments for the subroutine.

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.

No comments:

Post a Comment