1

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.

2
  • 1
    I don't understand the question. In particular, why "10 different sums"? [I can imagine the "10 different values" part.] Ah, I notice that the title has "function array" in it. There are no arrays in the question. Commented Aug 5, 2015 at 19:22
  • Thanks @francescalus for changing the question accordingly. Seems more appropriate now. Commented Aug 6, 2015 at 13:54

1 Answer 1

2

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...

Sign up to request clarification or add additional context in comments.

2 Comments

Thankyou very much Alex. I'm new to Fortran and this really helps. Thanks again :)
@SwethaS If this solves your problem, you can always accept the answer ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.