Fortran Fortran functions as arguments of functions

  • Thread starter Thread starter Biederman
  • Start date Start date
  • Tags Tags
    Fortran Functions
AI Thread Summary
The discussion centers on a user's inquiry about using user-defined functions as arguments in Fortran, specifically in the context of a numerical integration program. The user encountered a "segmentation fault (core dumped)" error when running their code, which did not show any compilation errors. The key issue was identified as the need for an EXTERNAL declaration when passing function names as arguments. After implementing this suggestion, the user successfully resolved the problem and got the program working. The conversation highlights the importance of proper function declarations in Fortran programming to avoid runtime errors.
Biederman
Messages
9
Reaction score
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.
 
Last edited:
Technology news on Phys.org
You should research the EXTERNAL declaration statement if you are passing function or subroutine names as arguments.
 
Thank you SteamKing. I've got the program working after checking the EXTERNAL statement, as you had suggested.

Thanks again
 
Last edited:
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
What percentage of programmers have learned to touch type? Have you? Do you think it's important, not just for programming, but for more-than-casual computer users generally? ChatGPT didn't have much on it ("Research indicates that less than 20% of people can touch type fluently, with many relying on the hunt-and-peck method for typing ."). 'Hunt-and-peck method' made me smile. It added, "For programmers, touch typing is a valuable skill that can enhance speed, accuracy, and focus. While...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Replies
16
Views
2K
Replies
8
Views
4K
Replies
8
Views
2K
Replies
25
Views
3K
Back
Top