By now, you are familiar with the idea of variables, and you have met a number of different types. Now we will introduce the Fortran constant. A constant can be thought of as a variable that takes a single value, which never changes i.e. it is fixed. Constants are useful for defining quantities, such as the speed of light, or other physical constants, that only ever take one value. A constant is defined by using a parameter statement.
The following piece of code illustrates their use, and shows a case in which they are particularly convenient: in dimensioning arrays.
program constants c implicit none c double precision pi, c parameter ( pi = 3.14159265358979d0 ) parameter ( c = 2.998d8 ) c integer size parameter (size = 100 ) c double precision x (size), y (5), z (size, size) integer i, j, k c do i = 1, size x (i) = i * pi z (i,i) = c enddo c k = size * 3 c end
Note how the constants size, c and pi are used in the same way as variables. The only difference is that their values may not change. This adds a level of security to your programs. Thus, a line such as:
c = c * 2.0d0
in the above program would generate an error. Note also, the advantage of having a parameter for dimensioning arrays, so that where many arrays have the same size, changing a single parameter statement will alter all of them. This saves time and effort on the part of the programmer, and helps to reduce mistakes.
You have already used Fortran's built-in mathematical functions in your programs. Now you will learn how to write your own functions to carry out more complicated, or repetitive operations, usually of a mathematical nature.
As with subroutines, functions may either be contained in the same file as the main program, or in separate files. In the example that follows, the function, which calculates the size of a cartesian vector, is kept in the same file. However, in the exercises, you will learn how to compile and run programs in which the sub-programs (i.e. the functions and subroutines) are contained in several files.
A function is defined using a block of code which begins with a function statement. The function statement includes the definition of the type of value that the function returns i.e. integer or double precision. The function definition ends with a return and end statement. For example, in the following program, two functions are defined and used. Study the example carefully and make sure that you understand what is going on.
program function_example c implicit none double precision x1, y1, r1 double precision x2, y2, r2 double precision x3, y3, r3 integer size, i, total parameter (size = 10) integer squares (size) c double precision modulus ! Function 1 integer summation ! Function 2 c c.....Calculate the sizes of some vectors. c x1 = 1.0d0 y1 = 2.0d0 r1 = modulus ( x1, y1 ) print *,'The modulus of the vector is: ',r1 c x2 = 3.0d0 y2 = 4.0d0 r2 = modulus ( x2, y2 ) print *,'The modulus of the vector is: ',r2 c x3 = 5.0d0 y3 = 6.0d0 r3 = modulus ( x3, y3 ) print *,'The modulus of the vector is: ',r3 c c.....Sum the first 10 squares. c do i = 1, size squares (i) = i * i enddo total = summation (squares, size) print *, 'The sum of the first 10 squares is: ',total c end ! This is the end of the MAIN program. double precision function modulus (a, b) ! Start of function 1... c c.....Function to calculate the modulus of a vector, whose components are c 'a' and 'b'. c implicit none double precision a, b c modulus = sqrt ( a*a + b*b ) ! Note that the answer is assigned to c the function itself. c return ! This transfers control back to the main program. end ! This is the end of the subroutine. integer function summation (array, size) ! Start of Function 2. c implicit none c integer size integer array (size) integer i c summation = 0 c do i = 1, size summation = summation + array (i) enddo c return end
In this example you should note two things. Firstly, the function types (double precision and integer) are declared in the main program. This is because the function itself returns the value, and Fortran needs to know what sort of value it is. Secondly, within the functions, it is the name of the function which is used to return the final value to the main program. It is very important that functions are declared in the same way in both the main program and the function definition. If not, they will not work.
As with subroutines, it is also crucial to make sure that the argument lists in the function reference (i.e. the point in the main program where they are used) must match the function definition in both number and variable type. The program will fail if this rule is not adhered to.
Modify your program from Exercise 3(b) so that it contains two functions: (1) A function which converts degrees to radians and (2) a function which converts radians to degrees. Define p = 3.14159265358979d0 as a parameter.
It is often convenient to spread functions and subroutines across several separate files. This might be, for example, because the programmer has built a private library of useful routines that find uses in several different programs. If this is the case, it is relatively straightforward to compile and link all of the routines together. For example, consider a program called prog.f and functions stored in files fun1.f, fun2.f and fun3.f . The following command would link them altogether:
f77 prog.f fun1.f fun2.f fun3.f
Note that the file containing the main program is specified first.
Previous: Section 3: IF Statements and Subroutines
Return to Dr. Alex Brown's Teaching
This page maintained by alex.brown@ualberta.ca of the Department of Chemistry, University of Alberta
Last updated August 8, 2003.