Professor Diomar Cesar Lobao

Universidade Federal Fluminense-Volta Redonda, RJ, Brasil

Diomar Cesar


Dept. Ciências Exatas - Exact Science Dept.

Search

dual-errors.f

c<html>
c<body>
c<pre>
      program interface
c
c    Example of the use of the INTERFACE structure to
c    create a generic function interface.  No function
c    named "saxpy" actually exists, but the INTERFACE
c    lets Fortran chose between "rsaxpy" and "isaxpy"
c    depending upon argument types.
c
c     John Mahaffy  4/3/96
c
      implicit none
      integer i
      real, dimension(10) :: z,y,x=(/(i,i=1,10)/)
c<a name="in"><font color="FF0000">
      integer, dimension(10) :: m,l,k=(/(i,i=1,10)/)
c</font></a>
      data y /5*1,5*2/
      data l /5*1,5*2/
c
c   interface block to permit a vector valued
c   function named saxpy
c
      interface saxpy
c
c       interface to the REAL version of the function
c
         function rsaxpy (a,x,y)
            real x(:),y(:),a
            real rsaxpy(size(x))
c
c    Declare x,y,a as input to saxpy, a good compiler
c    should warn you if they are undefined
c
            intent (in) x,y,a
         end function rsaxpy
c
c       interface to the INTEGER version of the function
c
         function isaxpy (a,x,y)
            integer, intent(in) :: x(:),y(:),a
            integer isaxpy(size(x))
         end function isaxpy
c
      end interface
c
c
c   Begin  Executable Statements
c
c   The next use of "saxpy" will actually  be using
c   "rsaxpy"
c
      z = saxpy(2.,x,l)
      write (*,2001)'a', 2.0
      write(*,2001) 'x', x
      write(*,2001) 'y', y
      write(*,*) ' For z = a x + y '
      write(*,2001) 'z',z
c
c   The next use of "saxpy" will actually  be using
c   "isaxpy"
c
      m = saxpy(2 ,k,l)
      write (*,2002)'a', 2
      write(*,2002) 'k', k
      write(*,2002) 'l', l
      write(*,*) ' For m = a k + l '
      write(*,2002) 'm',m
      stop
 2001 format (/, 1x,a,' = ', 5f8.1,/, (5x,5f8.1))
 2002 format (/, 1x,a,' = ', 5i5,/, (5x,5i5))
      end
c
      function rsaxpy(a,x,y)
c
c   Multply all contents of an array "x" by the scalar "a"
c   then add the result to the array "y"
c
c   John Mahaffy    4/3/96
c
c
      implicit none
      real, dimension (:) :: x,y
      real a, rsaxpy(size(x))
c
c   The next line combined with a good compiler should
c   prevent you from accidently redefining a, x, or y
c   within the subprogram.  Silly here, but not so far-
c   fetched if the subprogram drags on for a 1000 lines
c
      intent (in) a,x,y
c
      a=3.1
      rsaxpy =  a*x + y
      return
      end
c
      function isaxpy(a,x,y)
c
c   Multply all contents of an array "x" by the scalar "a"
c   then add the result to the array "y"
c
c   John Mahaffy    4/3/96
c
      implicit none
      integer, dimension (:), intent(in) :: x,y
      integer, intent(in) :: a
      integer isaxpy(size(x))
      isaxpy =  a*x + y
      return
      end
c</pre>
c</body>
c</html>
Skip to content