[Fortran] Functions Calling in Subroutines

In summary, there are some caveats when calling functions inside subroutines in Fortran 77. Some users have reported experiencing segmentation faults and memory usage issues while using and writing function arguments. This is only an issue inside the subroutine, as everything works fine before calling it. To avoid these errors, it is important to make sure the function is defined as the correct type of variable within the subroutine. Omitting this can result in the compiler assuming a REAL value is being returned, leading to potential problems.
  • #1
Raghnar
41
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
  • #2
Can you post a short but complete (compilable) program that illustrates the problem?
 
  • #3
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
 
  • #4
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.
 
  • #5
That was the problem, thank you! :)
 

What is the difference between a function and a subroutine in Fortran?

A function is a block of code that returns a single value, while a subroutine is a block of code that can perform multiple tasks and does not necessarily return a value. Functions are typically used for calculations, while subroutines are used for more complex operations.

How do you call a function within a subroutine in Fortran?

To call a function within a subroutine, you can use the CALL statement followed by the name of the function and any necessary arguments. For example: CALL my_function(arg1, arg2).

Can a subroutine call another subroutine in Fortran?

Yes, a subroutine can call another subroutine in Fortran. This is known as nested subroutines. However, it is important to keep in mind that too many levels of nested subroutines can make a program difficult to read and maintain.

What is the syntax for passing arguments to a subroutine in Fortran?

To pass arguments to a subroutine in Fortran, you can declare the subroutine with a list of arguments in parentheses after the subroutine name. For example: SUBROUTINE my_subroutine(arg1, arg2, arg3). In the calling program, you can then pass the arguments by value or by reference using the appropriate syntax.

Can a subroutine modify the value of an argument passed to it in Fortran?

Yes, a subroutine can modify the value of an argument passed to it in Fortran. This is known as passing arguments by reference. To do this, you can use the %REF or %LOC function to pass the address of the variable to the subroutine, allowing the subroutine to directly modify the value of the variable.

Similar threads

  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
2
Replies
59
Views
9K
  • Programming and Computer Science
Replies
8
Views
3K
  • Programming and Computer Science
Replies
4
Views
602
  • Programming and Computer Science
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Programming and Computer Science
Replies
10
Views
9K
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
Replies
12
Views
3K
  • Programming and Computer Science
Replies
2
Views
2K
Back
Top