next up previous contents
Next: Arrays Up: Examples and Exercises Previous: Basic Fortran Programs

Internal Subprograms

 

In Fortran 77, all subprograms are external with the exception of statement functions. Internal subprograms are now possible under Fortran 90 and achieve an effect similar to Fortran 77's statement functions. They are visible only within the containing program and have an explicit interface, guarding against type mismatches in calls to the subprogram. Internal subprograms must be separated from the main program by the contains statement. An example illustrating an internal subprogram is given below.

program Triangle
   real a, b, c

   print *, 'Enter the lengths of the three sides of the triangle'
   read *, a, b, c
   print *, 'Triangle''s area:  ', triangleArea( a, b, c )

   contains

   function triangleArea( a, b, c )
      real triangleArea
      real, intent( in ) :: a, b, c 

      real theta
      real height

      theta = acos( ( a**2 + b**2 - c**2 ) / ( 2.0 * a * b ) )
      height = a * sin( theta )

      triangleArea = 0.5 * b * height

   end function triangleArea
   
end program Triangle

exercise163



next up previous contents
Next: Arrays Up: Examples and Exercises Previous: Basic Fortran Programs