Re: double confusion



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.
.



Relevant Pages

  • Re: Help convert C function to Pascal function
    ... HB's code delivers seconds rather than milliseconds. ... A Real doesn't have enough precision for that scope at ... that resolution. ...
    (comp.lang.pascal.borland)
  • Re: Basic oscilloscope recommendations
    ... precision what you want your scope to do. ... I interpret to take about 3 orders of magnitude less memory depth ... That determines what you need from a digital scope. ... for the rest of its life and nothing else. ...
    (sci.electronics.equipment)
  • Re: Mex Segmentation Violation
    ... matrix 25 x 4 (double precision) and bugged for bigger ones. ... Use some manual debugging techniques to reduce the scope of the problem. ... I recommend eliminating various pieces of the code ...
    (comp.soft-sys.matlab)
  • Re: To Hungarian or not to Hungarian
    ... aware that there may be rounding or loss of precision. ... variables is immediately suspect in my book, ... Well, maybe it doesn't have the term "global" in the language, but it ... This is all about scope. ...
    (borland.public.delphi.non-technical)
  • Re: NAG, Sun, Compaq possible f95 bug
    ... when IMPLICIT NONE is in effect in the enclosing ... > scoping unit, it would be impossible to use such variables. ... declaration is not actually in the scope that it applies to. ...
    (comp.lang.fortran)