Easy fortran Q?

  • Fortran
  • Thread starter autobot.d
  • Start date
  • #1
68
0

Main Question or Discussion Point

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.
 

Answers and Replies

  • #2
uart
Science Advisor
2,776
9
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
 
  • #3
68
0
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 proc


end module module1
 
  • #4
uart
Science Advisor
2,776
9
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
 
  • #5
68
0
now I get the error using your modifications

times2(input1)
1 (1 is under first parenthesis)
Error: 'times2' at (1) is not a variable


??????
 
  • #6
uart
Science Advisor
2,776
9
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:

Related Threads on Easy fortran Q?

  • Last Post
Replies
1
Views
1K
  • Last Post
Replies
2
Views
2K
  • Last Post
2
Replies
32
Views
10K
  • Last Post
Replies
2
Views
2K
  • Last Post
Replies
19
Views
6K
  • Last Post
Replies
4
Views
2K
  • Last Post
Replies
6
Views
781
  • Last Post
Replies
1
Views
1K
  • Last Post
Replies
1
Views
2K
  • Last Post
Replies
6
Views
9K
Top