Fortran Troubleshooting Function Call Error in Subroutine

AI Thread Summary
The discussion centers on a user attempting to call a function within a subroutine in a Fortran module but encountering errors. The user defines a function `times2` that doubles its input but struggles to call it from the subroutine `proc`. The initial attempts to declare `times2` as a variable or external function lead to an "unclassifiable statement" error. A suggestion is made to avoid redefining `times2` within the subroutine's scope, as this causes confusion between the function and a variable. The user is reminded that functions defined in the same module should be accessible without explicit interfacing. However, despite following the advice, the user still receives an error indicating that `times2` is not recognized as a variable. The conversation highlights the importance of proper function referencing and compiler compatibility, with a request for the exact code being compiled to further diagnose the issue.
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:
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 had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

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