Re: double confusion
- From: Brooks Moses <bmoses-nospam@xxxxxxxxxxxxxxxxxx>
- Date: Mon, 17 Jul 2006 09:43:00 -0700
Julian Bessenroth wrote:
I can't see the tree for the wood ...
my listing:
program bcont
double precision c
double precision a /2.0/
double precision b /2.0/
c=r(a,b)
write(*,*) 'result = ',c
stop
end
This block is one scope. Within this block, r has been implicitly declared as real, because it starts with a letter outside the range i-n and it has not been otherwise declared. (You should put an "implicit none" declaration at the beginning of your main program and each function or other scope unless you specifically want implicit declaration of this sort; otherwise it will mask all sorts of bugs like this.)
double precision function r(m,t)
double precision m
double precision t
r = m*t
c write(*,*) 'm = ',m,' t = ',t,' m*t= ',r
return
end
This block is a different scope, and within this scope r has been explicitly declared as double precision.
The type mismatch is causing your problems.
Within FORTRAN 77, the only way to deal with this is to also declare the function r() within the main program, as there is no way around the problem of separate scopes. As others have said, the function and the program might as well be in separate files and compiled independently; there is no notion of "file scope".
This problem has, however, been solved in more recent versions of Fortran. In Fortran 95, you could do the following:
module my_module
implicit none
contains
double precision function r(m,t)
double precision m
double precision t
r = m*t
c write(*,*) 'm = ',m,' t = ',t,' m*t= ',r
return
end function
end module
program bcont
use my_module
implicit none
double precision c
double precision a /2.0/
double precision b /2.0/
c=r(a,b)
write(*,*) 'result = ',c
end program
The module contains the declaration of the r() function, and the "use my_module" declaration at the beginning of the main program instructs the compiler to import these declarations into the program scope -- and thus you do not need to declare the function twice, or worry about mismatch in the declarations. Note that the module has to preceed the program, as it must be compiled first.
Alternately, you could simply include the function within the main program, like so:
program bcont
implicit none
double precision c
double precision a /2.0/
double precision b /2.0/
c=r(a,b)
write(*,*) 'result = ',c
contains
double precision function r(m,t)
double precision m
double precision t
r = m*t
c write(*,*) 'm = ',m,' t = ',t,' m*t= ',r
return
end function
end program
This includes the function r() within the scope of the main program directly.
- Brooks
--
The "bmoses-nospam" address is valid; no unmunging needed.
.
- References:
- double confusion
- From: Julian Bessenroth
- double confusion
- Prev by Date: F2008 draft
- Next by Date: Re: F2008 draft
- Previous by thread: Re: double confusion
- Next by thread: F2008 draft
- Index(es):
Relevant Pages
|