- #1

- 9

- 0

Hi all,

It is my first time to resort to the help of members of this forum :)

My question is related to programming in fortran but I am rather new with it.

So here is the question:

Can I use user defined functions as arguments of a user defined functions in Fortran?

I have written a simple code for numerical integration but when I run the program I get an error message "segmentation fault (core dumped)"

Here is the code:

C%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

PROGRAM NUMERIC

REAL :: INTEG,SINE

PRINT*,INTEG(SINE,0.0,3.14159,100)

END

FUNCTION INTEG(FUNC,A,B,N)

INTERFACE

FUNCTION FUNC(X)

REAL :: FUNC, X

END FUNCTION

END INTERFACE

REAL :: A,B,INTEG,H,TOTAL,FUNC

INTEGER N,I

! THE INTEGRATION FORMULA IS: INTEG=H*(0.5*FUNC(X0)+FUNC(X1)+.....+0.5*FUNC(XN))

H=(B-A)/N

TOTAL=0.5*(FUNC(A)+FUNC(B))

DO I=1,N-1

TOTAL=TOTAL+FUNC(A+I*H)

ENDDO

INTEG=TOTAL*H

RETURN

END FUNCTION INTEG

FUNCTION SINE(X)

REAL :: X,SINE

SINE=X*SIN(X)

RETURN

END

C%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

After running I get: Segmentation fault (core dumped)

I don't know what's wrong with the code, since there is no error message during the compilation.

I would appreciate your help.

It is my first time to resort to the help of members of this forum :)

My question is related to programming in fortran but I am rather new with it.

So here is the question:

Can I use user defined functions as arguments of a user defined functions in Fortran?

I have written a simple code for numerical integration but when I run the program I get an error message "segmentation fault (core dumped)"

Here is the code:

C%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

PROGRAM NUMERIC

REAL :: INTEG,SINE

PRINT*,INTEG(SINE,0.0,3.14159,100)

END

FUNCTION INTEG(FUNC,A,B,N)

INTERFACE

FUNCTION FUNC(X)

REAL :: FUNC, X

END FUNCTION

END INTERFACE

REAL :: A,B,INTEG,H,TOTAL,FUNC

INTEGER N,I

! THE INTEGRATION FORMULA IS: INTEG=H*(0.5*FUNC(X0)+FUNC(X1)+.....+0.5*FUNC(XN))

H=(B-A)/N

TOTAL=0.5*(FUNC(A)+FUNC(B))

DO I=1,N-1

TOTAL=TOTAL+FUNC(A+I*H)

ENDDO

INTEG=TOTAL*H

RETURN

END FUNCTION INTEG

FUNCTION SINE(X)

REAL :: X,SINE

SINE=X*SIN(X)

RETURN

END

C%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

After running I get: Segmentation fault (core dumped)

I don't know what's wrong with the code, since there is no error message during the compilation.

I would appreciate your help.

Last edited: