由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Computation版 - something strange in fortran90
相关主题
Question about overloading in fortran90where to down a standard Fortran90 complier?
problem about Fortran 77 in unixfortran90的超级怪问题
弱问:FORTRAN90调用子程序问题A fortran problem in CFDRC
偶也问一fortran问题Fortran里面哪个函数显示系统时间?
求矩阵逆的算法help on interface for Matlab and Fortran
a question about mpi-fortranRe: fortran subroutine and function
numerical recipe c++Fortran code optimization
请教一个程序调用问题(FORTRAN DLL),谢谢!fortran菜鸟一问
相关话题的讨论汇总
话题: subroutine话题: pg2话题: contains话题: implicit话题: pg1
进入Computation版参与讨论
1 (共1页)
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?

1 (共1页)
进入Computation版参与讨论
相关主题
fortran菜鸟一问求矩阵逆的算法
Subroutine of luinc in Matlab ?a question about mpi-fortran
MPI Write?numerical recipe c++
[转载] 问一个fortran的问题请教一个程序调用问题(FORTRAN DLL),谢谢!
Question about overloading in fortran90where to down a standard Fortran90 complier?
problem about Fortran 77 in unixfortran90的超级怪问题
弱问:FORTRAN90调用子程序问题A fortran problem in CFDRC
偶也问一fortran问题Fortran里面哪个函数显示系统时间?
相关话题的讨论汇总
话题: subroutine话题: pg2话题: contains话题: implicit话题: pg1