Troubleshooting Function Call Error in Subroutine

Click For Summary

Discussion Overview

The discussion revolves around troubleshooting a function call error within a Fortran subroutine. Participants explore issues related to function accessibility and proper definitions within modules, focusing on syntax and compiler behavior.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant describes an error when trying to call a function from within a subroutine, indicating that the function is not recognized.
  • Another participant provides an example of defining a function within a module, suggesting that this approach works for them.
  • A participant attempts to call the function within a subroutine but encounters issues with variable redefinition, suggesting that the function name is being treated as a variable instead.
  • Further clarification is offered regarding the accessibility of functions defined in the same module, with a participant asserting that explicit interfacing should not be necessary.
  • A participant requests the exact code being compiled to better understand the issue at hand.

Areas of Agreement / Disagreement

Participants express differing experiences with function accessibility and error messages, indicating that there is no consensus on the cause of the issue or the correct approach to resolve it.

Contextual Notes

Participants mention different Fortran compilers, which may influence the behavior of the code. There is also a lack of clarity regarding the specific code being used, which may contribute to the unresolved errors.

autobot.d
Messages
67
Reaction score
0
So I am making a module to run in the main program. The module contains a subroutine and several functions. I want to call one of the functions into the subroutine but it is not getting recognized. I am just doing

Real :: fct %fct is the function name that returns a real value

but when I try to use the function i get the error

Error: Unclassifiable statement at (1)

I also tried

Real, external :: fct

but the same problem occurs.
 
Technology news on Phys.org
I'm not a Fortran expert, and I've only used Fortran95, but defining functions like in the following (trivial example) works for me.

Code:
!  Example using modules
module module1
implicit none
contains

 function times2(x)
 real :: times2,x

   times2 = x*2.0

 end function times2

end module module1
 
I get that part, but calling that function inside a subroutine in the module as follows:
Code:
!Example using modules
module module1
implicit none
contains

 function times2(x)
 real :: times2,x

   times2 = x*2.0

 end function times2

!This is what I am wondering about

subroutine proc(input1)
real :: input1
real :: times2  !This does not seem to work for me
                    !Nor does real, external :: times2
times2(input1)

end subroutine procend module module1
 
autobot.d said:
I get that part, but calling that function inside a subroutine in the module as follows:
Oh I see, you're making much the same mistake as in your previous question. :)

You are redefining "times2" as just a plain old variable (instead of a function) inside the scope of the subroutine.

All you need is this :
Code:
!Example using modules
module module1
implicit none
contains

 function times2(x)
 real :: times2,x

   times2 = x*2.0

 end function times2

 subroutine printx2(input1)
 real :: input1

   print *,times2(input1)

 end subroutine printx2

end module module1
 
now I get the error using your modifications

times2(input1)
1 (1 is under first parenthesis)
Error: 'times2' at (1) is not a variable?
 
autobot.d said:
now I get the error using your modifications

times2(input1)
1 (1 is under first parenthesis)
Error: 'times2' at (1) is not a variable?
It should work, functions defined in the same module should be accessible to each other without any explicit interfacing. It works ok for me in "g95" Fortran, what Fortran compiler are you using?

BTW. Can you post the exact code you're trying to compile?
 
Last edited:

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 20 ·
Replies
20
Views
3K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 59 ·
2
Replies
59
Views
12K
  • · Replies 14 ·
Replies
14
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 10 ·
Replies
10
Views
10K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K