The EXTERNAL statement is used to flag external procedures or block data program units as external procedures. When a procedure is declared to be external, it allows the programmer to pass the name of that procedure through an argument list to another program unit.
Before a program unit can pass the name of a function or subroutine through an argument list to another program unit, that function or subroutine needs to be declared as external. The EXTERNAL declaration must be carried out at the beginning of the program unit right after the program, use and type declaration statements. So, if I had a function called stdev that I wanted to pass from my main program to other program units, I would declare it external in the following manner.
Program statistics use global implicit none real stdev,x(1000),y(1000) integer i,j,k external stdev
With that done, I could then pass it to a subroutine that needed to use this function by adding stdev to the call statement's argument list much like this.
As long as the curvefit subroutine does not use the passed function as an argument to a function reference or call to a subroutine, it is not necessary to include an EXTERNAL statement. In the example below the compiler deduces that "stdev" is a function from the context of its usage.
Subroutine curvefit(i,j,x,stdev) Use global implicit none integer i,j real x(1000),stdev,result result = stdev(i,x)
If the above subroutine curvefit also passed "stdev" as an argument to another subroutine or function, then a statement "external stdev" would be required within "curvefit". Please note that if the procedure that you wanted to pass another program unit was an intrinsic procedure, then you would not use the external statement. Instead, you would have to declare the intrinsic procedure with the intrinsic statement before passing it as an argument.
Another use of the external statement is to give the programmer the power to bypass intrinsic procedures. For instance, if I wanted to write and use my own version of the tangent function I could use it instead of FORTRAN's intrinsic tan function by declaring tan to be an external procedure in the program units that it was needed in. Be aware that when bypassing intrinsic procedures in this manner the intrinsic procedure becomes inaccessible to the programmer inside any program units that declare the intrinsic procedure's name to be external.
As one final rule on using the external statement, a procedure can not be declared external more than once in each program unit in which it is used.
lecture twenty nine
examples: plot2.f, external.f
Written by Jason Wehr : email@example.com and Maintained by John Mahaffy : firstname.lastname@example.org