Can‘t call a subroutine name in a subroutine in Fortran90

In summary,The code for example appears to have a problem when it is compiled and executed. Moving the subroutine B out of the 'contains' clause of the interface solves the problem.
  • #1
pilipili2021
5
0
Homework Statement
Coding by ifort
Try to call a uncertain name subroutine
Relevant Equations
NONE
Here is the code for example:
Fortran:
PROGRAM  main
  CALL B()
  CALL CaoB(B)
  
  contains
   
  subroutine B()
  IMPLICIT NONE
  write (*,*) 'fk'
  end subroutine B

 subroutine CaoB(fcn)
 implicit none

 INTERFACE
   SUBROUTINE fcn()
     IMPLICIT NONE
   END SUBROUTINE fcn
 END INTERFACE

 write (*,*) '1'
 call B() 
 write (*,*) '2'
 call fcn()
 write (*,*) '3'

 end subroutine CaoB 

END
Then I compile it.

ifort main.F90

It seems no error. Then run it.

./a.outIt show that

fk
1
fk
2
forrtl: severe (174): SIGSEGV, segmentation fault occurred
Image PC Routine Line Source
a.out 000000000040492A Unknown Unknown Unknown
libpthread-2.31.s 00007F15A65153C0 Unknown Unknown Unknown
Unknown 00007FFFC18B82C0 Unknown Unknown Unknown

Surely there is something wrong when I call the subroutine fcn, which it should be named as B.

So, what should I do? what's wrong? Please let me know if any of you have any ideas or any comments.

thank you
 
Physics news on Phys.org
  • #2
pilipili2021 said:
which it should be named as B
How does the main program know that ?
[edit] Or rather: the calling program, in this case CaoB

##\ ##
 
Last edited:
  • #3
BvU said:
How does the main program know that ?

##\ ##
Thank you for your reply.

Do you mean add some code like 'external B' or 'external fcn'? That not works.

In the subroutine 'CaoB' it have a subroutine name 'fcn' determined by the interface. When I call 'CaoB(B)', 'B' as a subroutine name should be instead of 'fcn' in the subroutine 'CaoB'. It should be, but something goes wrong.
 
  • #4
Your program appears to be fine. I have checked both with GNU Fortran (GCC) 10.2.0 and ifort version 2021.2.0 20210228.
 
  • #5
Thank you for your reply!
It's a great help.
May be my environment have some problems.
my ifort version is 2021.4.0

So confused.
 
  • #6
pilipili2021 said:
Thank you for your reply!
It's a great help.
May be my environment have some problems.
my ifort version is 2021.4.0

So confused.
Problems are usually fixed with newer versions :smile:.

By the way, my ifort is on Linux.
 
  • #7
DrClaude said:
Problems are usually fixed with newer versions :smile:.

By the way, my ifort is on Linux.
My ifort is on ubuntu 20.04 , also on Linux.
Everything worked well before my hard disk suddenly broken.
May problems created with newer versions:cry:
 
  • #8
finally I find the problem.

If I code like

Call CaoB()
contains
subroutine B()

the error comes.

If I move subroutine B out of 'contains' like

subroutine B()

subroutine something
call CaoB()
end subroutine something

Then the problem is solved.
 

FAQ: Can‘t call a subroutine name in a subroutine in Fortran90

What is a subroutine in Fortran90?

A subroutine in Fortran90 is a named block of code that can be called multiple times within a program. It allows for the organization and reuse of code, making programs more efficient and easier to maintain.

Can a subroutine call another subroutine in Fortran90?

Yes, a subroutine can call another subroutine in Fortran90. This is known as nested subroutines and allows for more complex and modular programming.

Why am I getting an error message when trying to call a subroutine name in a subroutine in Fortran90?

This error message may be caused by a number of reasons, such as a misspelled subroutine name, incorrect syntax, or attempting to call a subroutine before it is defined. Double check your code and make sure all subroutine names are correct and in the correct order.

Can I pass parameters to a subroutine in Fortran90?

Yes, you can pass parameters to a subroutine in Fortran90. This allows for the subroutine to receive information from the main program and use it in its calculations or operations.

Is there a limit to the number of subroutines I can call within a program in Fortran90?

No, there is no limit to the number of subroutines that can be called within a program in Fortran90. However, it is important to use subroutines efficiently and avoid unnecessary nesting to maintain a clear and organized program structure.

Similar threads

Replies
5
Views
1K
Replies
3
Views
4K
Replies
1
Views
3K
Replies
5
Views
7K
Replies
1
Views
2K
Replies
5
Views
3K
Replies
8
Views
4K
Back
Top