s*****l 发帖数: 167 | 1 I wrote a program with 2 subroutines, say I have
program main
contains
subroutine pg1
...
real(8) :: x(20)
call pg2
end subroutien pg1
subroutine pg2
...
print*, x(10)
end subroutine pg2
Even if I donot pass x to from pg1 to pg2,
and I donot have any variable x in pg2,
I can still find x in pg2!
actually I can print it, or compute it.
what is wrong? | f*******d 发帖数: 339 | 2 "contains" declare the subroutine or function as internal, which has access to all
variables of the module or other internal subroutines in the same module. If you
don't want this behavior, do not use "contains"
【在 s*****l 的大作中提到】 : I wrote a program with 2 subroutines, say I have : program main : contains : subroutine pg1 : ... : real(8) :: x(20) : call pg2 : end subroutien pg1 : subroutine pg2 : ...
| y***r 发帖数: 1845 | 3 你说错了。他是因为没有使用显式声明。试一试下面的程序
program test
implicit none
call p1
contains
subroutine p1
implicit none
real x
x = 20
call p2
end subroutine p1
subroutine p2
implicit none
print *, x
end subroutine p2
end program test
You will meet a severe err if your compiler is okay.
【在 f*******d 的大作中提到】 : "contains" declare the subroutine or function as internal, which has access to all : variables of the module or other internal subroutines in the same module. If you : don't want this behavior, do not use "contains"
| f*******d 发帖数: 339 | 4 What I said was correct (see your F90 manual description about "contains").
But it is also true that if you use implicit none in the subroutine,
it will prevent the internal subroutines to reference other internal
subroutine.
Nevertheless, even in that case they can still access the variables in the
main program.
Instead of the example you give, try the following:
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
program test
implicit none
real x
call p1
contains
subroutine p1
implicit none
x
【在 y***r 的大作中提到】 : 你说错了。他是因为没有使用显式声明。试一试下面的程序 : program test : implicit none : call p1 : : contains : subroutine p1 : implicit none : real x : x = 20
| s*****l 发帖数: 167 | 5 Thanks guys!
But suppose that you have quite a few variables
and probably one of them has the same name as
another in the main program, it could be dangerous.
I guess I better off writing a module to gather all
the subroutines.
There is no way to claim LOCAL variable?
【在 f*******d 的大作中提到】 : What I said was correct (see your F90 manual description about "contains"). : But it is also true that if you use implicit none in the subroutine, : it will prevent the internal subroutines to reference other internal : subroutine. : Nevertheless, even in that case they can still access the variables in the : main program. : Instead of the example you give, try the following: : !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! : program test : implicit none
| f*******d 发帖数: 339 | 6 I think the idea is that if a subroutine does not need to access the
variables in the host, it should not be made internal (i.e. "contains" should not be used).
Also, if some variables and routines are often used at the same time, you should make them
a module, and explicitely specify "use" this module when necessary. Occasional use of
local variable in the internal subroutine can be declared by making the implicit none statement
in the subroutine, as in yager's example.
【在 s*****l 的大作中提到】 : Thanks guys! : But suppose that you have quite a few variables : and probably one of them has the same name as : another in the main program, it could be dangerous. : I guess I better off writing a module to gather all : the subroutines. : There is no way to claim LOCAL variable?
|
|