问题
I am doing a program in which the main contains many subroutines and functions. For constructing one of these subroutines of the main (let's say subroutine A) I need to make use of another subroutine (let's say B). My question is, how can I make subroutine A call and use subroutine B? I am a beginner and I have searched a lot but found nothing that I understand clearly...
Any help would be appreciated, thanks in advance!
回答1:
Example of layout, in one file:
module MySubs
contains
subroutine A (..)
end subroutine A
subroutine B (..)
call subroutine A (..)
end subroutine B
function C (..)
end function C
end module MySubs
program MyProg
use MySubs
call A (..)
X = C (..)
end program MyProg
You can also place the module and and main program in different files. In that case compile the file with the module first.
回答2:
Normally you put all procedures in a module
and then use
the module.
If, as your question suggest make all procedures as internal to your main program, there is no problem in calling them. Just invoke them normally using the call
statement, or by using the function name with the argument list.
来源:https://stackoverflow.com/questions/20772817/how-to-call-and-use-a-subroutine-inside-another-subroutine-in-fortran