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.
Array results require an explicit interface. You would also need to adjust
funct
andsum
to actually be arrays using thedimension
statement. Using an explicit interface requires Fortran 90+ (thanks for the hints by @francescalus and @VladimirF) and is quite tedious: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:Notice that I pass
k
to the function. It needs to be adjusted accordingly: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...