RangerGirl
RangerGirl

Reputation: 11

Return an array from a function and store it in the main program

Here is the Main Program:

      PROGRAM integration
      EXTERNAL funct
      DOUBLE PRECISION funct, a , b, sum, h
      INTEGER n, i
      REAL s

      PARAMETER (a = 0, b = 10, n = 200)  

      h = (b-a)/n
      sum = 0.0

      DO i = 1, n
         sum = sum+funct(i*h+a)
      END DO

      sum = h*(sum-0.5*(funct(a)+funct(b)))

      PRINT *,sum

      CONTAINS

      END     

And below is the Function funct(x)

      DOUBLE PRECISION FUNCTION funct(x)
      IMPLICIT NONE

      DOUBLE PRECISION x
      INTEGER K

      Do k = 1,10
      funct = x ** 2 * k
      End Do

      PRINT *, 'Value of funct is', funct

      RETURN
  END

I would like the 'Sum' in the Main Program to print 10 different sums over 10 different values of k in Function funct(x).

I have tried the above program but it just compiles the last value of Funct() instead of 10 different values in sum.

Upvotes: 1

Views: 401

Answers (1)

Alexander Vogt
Alexander Vogt

Reputation: 18098

Array results require an explicit interface. You would also need to adjust funct and sum to actually be arrays using the dimension statement. Using an explicit interface requires Fortran 90+ (thanks for the hints by @francescalus and @VladimirF) and is quite tedious:

        PROGRAM integration
        INTERFACE funct
          FUNCTION funct(x) result(r)
            IMPLICIT NONE
            DOUBLE PRECISION r
            DIMENSION r( 10 )
            DOUBLE PRECISION x
          END FUNCTION
        END INTERFACE
        DOUBLE PRECISION a , b, sum, h
        DIMENSION sum( 10)
        INTEGER n, i

        PARAMETER (a = 0, b = 10, n = 200)  

        h = (b-a)/n
        sum = 0.0

        DO i = 1, n
           sum = sum+funct(i*h+a)
        END DO

        sum = h*(sum-0.5*(funct(a)+funct(b)))

        PRINT *,sum

        END  

        FUNCTION funct(x)
        IMPLICIT NONE
        DOUBLE PRECISION funct
        DIMENSION funct( 10)
        DOUBLE PRECISION x
        INTEGER K

        Do k = 1,10
          funct(k) = x ** 2 * k
        End Do

        PRINT *, 'Value of funct is', funct

        RETURN
        END

If you can, you should switch to a more modern Standard such as Fortran 90+, and use modules. These provide interfaces automatically, which makes the code much simpler.

Alternatively, you could take the loop over k out of the function, and perform the sum element-wise. This would be valid FORTRAN 77:

        PROGRAM integration
c       ...
        DIMENSION sum( 10)
c       ...
        INTEGER K
c       ...
        DO i = 1, n
          Do k = 1,10
            sum(k)= sum(k)+funct(i*h+a, k)
          End Do
        END DO
c       ...

Notice that I pass k to the function. It needs to be adjusted accordingly:

        DOUBLE PRECISION FUNCTION funct(x,k)
        IMPLICIT NONE
        DOUBLE PRECISION x
        INTEGER K

        funct = x ** 2 * k

        PRINT *, 'Value of funct is', funct

        RETURN
        END

This version just returns a scalar and fills the array in the main program.


Apart from that I'm not sure it is wise to use a variable called sum. There is an intrinsic function with the same name. This could lead to some confusion...

Upvotes: 2

Related Questions