Fortran functions as arguments of functions

In summary, the conversation is about a user asking for help with programming in Fortran. They have a question about using user defined functions as arguments and have written a code for numerical integration. However, when they run the program, they get an error message. After further discussion and research, they were able to fix the issue by using the EXTERNAL declaration statement.
  • #1
Biederman
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.
 
Last edited:
Technology news on Phys.org
  • #2
You should research the EXTERNAL declaration statement if you are passing function or subroutine names as arguments.
 
  • #3
Thank you SteamKing. I've got the program working after checking the EXTERNAL statement, as you had suggested.

Thanks again
 
Last edited:

Related to Fortran functions as arguments of functions

What is a Fortran function?

A Fortran function is a block of code that performs a specific task and returns a value to the calling program. It is a reusable piece of code that can be called multiple times within a program.

Can you pass a Fortran function as an argument of another function?

Yes, Fortran allows functions to be passed as arguments of other functions. This allows for greater flexibility and modularity in code, as functions can be reused and called from different parts of a program.

What are the advantages of using Fortran functions as arguments of functions?

Using Fortran functions as arguments of functions allows for more efficient and modular code. It also reduces the need for repetitive code, as functions can be reused in multiple places within a program. Additionally, it can make code easier to read and debug.

Are there any limitations to using Fortran functions as arguments of functions?

One limitation is that the function being passed as an argument must have a compatible type and number of arguments with the function it is being passed to. Additionally, some compilers may have restrictions on the use of functions as arguments, so it is important to check the documentation for specific guidelines.

How do you call a Fortran function that is passed as an argument of another function?

To call a Fortran function that is passed as an argument, you can use the "call" statement followed by the function name and its arguments inside the parentheses. The result of the function can then be stored in a variable for further use.

Similar threads

  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
Replies
11
Views
2K
  • Programming and Computer Science
Replies
6
Views
982
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
5
Views
3K
  • Programming and Computer Science
Replies
4
Views
745
  • Programming and Computer Science
Replies
15
Views
2K
  • Programming and Computer Science
Replies
25
Views
622
  • Programming and Computer Science
Replies
8
Views
1K
Back
Top