02.07.2013 Views

HP Fortran Programmer's Reference

HP Fortran Programmer's Reference

HP Fortran Programmer's Reference

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Program units and procedures<br />

Procedure interface<br />

is a comma-separated list of names of module procedures that have<br />

generic-spec as a generic interface. Each module-procedure name must be<br />

accessible either by use association or—if this interface block is in a module<br />

that defines the module procedure—by host association.<br />

If the MODULE PROCEDURE statement is present, then generic-spec must<br />

also be present.<br />

The following example, proc_interface.f90, uses an interface block in the main program unit to<br />

provide an explicit interface for the function avg.<br />

Example 7-8 proc_interface.f90<br />

! Define an external function avg with one assumed-shape dummy<br />

! argument. Note that the definition of the function must<br />

! lexically precede its declaration in the interface block.<br />

REAL FUNCTION avg(a)<br />

REAL a(:)<br />

avg = SUM(a)/SIZE(a)<br />

END FUNCTION avg<br />

PROGRAM main<br />

REAL,DIMENSION(3) :: x<br />

INTERFACE<br />

REAL FUNCTION avg(a)<br />

REAL, INTENT(IN) :: a(:)<br />

END FUNCTION avg<br />

END INTERFACE<br />

x=(/2.0, 4.0, 7.0/)<br />

PRINT *, avg(x)<br />

END PROGRAM main<br />

Here are the command lines to compile and execute the program, along with the output from<br />

asamplerun:<br />

$ f90 proc_interface.f90<br />

$ a.out<br />

4.33333<br />

Generic procedures<br />

The <strong>Fortran</strong> 90 concept of generic procedures extends the FORTRAN 77 concept of generic<br />

intrinsics to allow user-defined generic procedures. A procedure is generic if its name—a<br />

generic name—is associated with a set of specific procedures. Referencing the generic name<br />

allows actual arguments to differ in type, kind, and rank. The differences in the arguments<br />

determine which specific procedure is invoked.<br />

Chapter 7 151

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!