Fortran [Fortran] Functions Calling in Subroutines

  • Thread starter Thread starter Raghnar
  • Start date Start date
  • Tags Tags
    Fortran Functions
AI Thread Summary
In Fortran 77, calling functions within subroutines can lead to issues such as segmentation faults, particularly when there are mismatches in data types. A common problem arises when a function that returns a complex type is not properly declared within the subroutine. If the function is called without declaring it as a complex variable, the compiler defaults to assuming it returns a real value, which can cause errors during execution. This can manifest as segmentation faults when attempting to use the function's output. Ensuring that the function is correctly defined as a complex type within the subroutine resolves these issues. Proper type declaration is crucial for avoiding memory usage problems and ensuring the program runs smoothly.
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! :)
 
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...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
59
Views
11K
Replies
3
Views
2K
Replies
8
Views
4K
Replies
4
Views
2K
Replies
5
Views
3K
Replies
10
Views
10K
Replies
4
Views
2K
Replies
11
Views
2K
Back
Top