[Fortran] Functions Calling in Subroutines

  • Context: Fortran 
  • Thread starter Thread starter Raghnar
  • Start date Start date
  • Tags Tags
    Fortran Functions
Click For Summary

Discussion Overview

The discussion focuses on issues related to calling functions within subroutines in Fortran 77, particularly concerning segmentation faults and variable type mismatches. Participants explore the implications of variable definitions and memory usage in this context.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant reports experiencing segmentation faults when calling a function inside a subroutine, specifically when using a complex variable and an integer argument.
  • Another participant requests a complete, compilable program to better understand the issue.
  • A suggestion is made to ensure that the function is defined with the correct variable type within the subroutine, highlighting that omitting the type can lead to the compiler assuming an incorrect return type.
  • The original poster acknowledges that the type mismatch was indeed the source of the problem.

Areas of Agreement / Disagreement

Participants generally agree on the importance of correctly defining variable types in Fortran functions and subroutines. The discussion appears to resolve the specific issue raised by the original poster, but broader implications about function calls and memory management remain open for further exploration.

Contextual Notes

Limitations include the lack of a complete example program initially, which may have contributed to the confusion regarding the function call and variable types.

Raghnar
Messages
41
Reaction score
1
There are some caveat in fortran 77 while calling functions inside subroutines?
I have some weird errors while calling a function inside a subroutine, e.g. Segmentation Fault while using/writing the argument of the function (but not sooner, I can still do an "Hallo World"!) or starting a do loop.
The function receive a Complex and an integer argument. printing the first complex variable get you 0 whatever the input is, printing the integer variable goes in segmentation fault... there are some memory usage issues, but I don't really know where to look...
This is only inside a subroutine, before calling the subroutin everything works just fine.

Any suggestions?
 
Last edited:
Technology news on Phys.org
Can you post a short but complete (compilable) program that illustrates the problem?
 
I will try to reproduce the program in a short version, seeing if the problem is still there... The one which I use is far too complicated
 
Make sure your function is defined as the correct type of variable, inside the subroutine where you call it.

complex function c(x,y)
real x, y
... do some calculations
c = something
return
end

subroutine s
real x, y
complex z
complex c
...
z = c(x,y)
...
end

If you omit the "complex c" statement inside the subroutine, the compiler will assume that function c returns a REAL value. That certainly won't work properly, and may give you segementation faults etc.
 
That was the problem, thank you! :)
 

Similar threads

  • · Replies 59 ·
2
Replies
59
Views
12K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 10 ·
Replies
10
Views
10K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 11 ·
Replies
11
Views
2K