Troubleshooting Function Call Error in Subroutine

In summary, the conversation discusses the use of a module containing a subroutine and several functions in Fortran. The main issue is with calling one of the functions within the subroutine, as it is not recognized. The conversation also discusses the mistake of redefining a function as a variable within the subroutine. The solution is to use the correct function name and ensure that the function is defined within the same module.
  • #1
autobot.d
68
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
  • #2
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
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
 
  • #4
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
 
  • #5
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
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:

What is a "Function Call Error"?

A "Function Call Error" refers to an error that occurs when a function or subroutine is called in a program, but there is an issue with the way the function is being called. This can include incorrect arguments, missing parentheses, or calling a non-existent function.

Why am I getting a "Function Call Error" in my subroutine?

There are a few possible reasons for this error. It could be due to a mistake in how the function is being called, such as using the wrong number or type of arguments. It could also be caused by an error within the function itself, such as a syntax error or a variable being used incorrectly.

How can I troubleshoot a "Function Call Error" in my subroutine?

To troubleshoot this type of error, you can start by carefully reviewing the code where the function is being called. Make sure the arguments are correct and that the function is being called in the right place. If the error persists, you may need to review the code within the function itself to identify any potential issues.

Can a "Function Call Error" be caused by a missing library or module?

Yes, a missing library or module can cause a "Function Call Error" if the function being called is defined within that library or module. In this case, the error may be due to the program not being able to find or access the necessary files. Make sure all required libraries and modules are properly installed and configured.

How can I prevent "Function Call Errors" in my code?

To prevent "Function Call Errors", it is important to carefully review and test your code before running it. Double check that all functions are being called correctly and that any necessary libraries or modules are properly included. Additionally, using proper coding practices, such as commenting and organizing your code, can help to minimize the likelihood of errors occurring.

Similar threads

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