Re: double confusion



Julian Bessenroth wrote:


Ian Bush schrieb:

How is it different from C ?

If the subprogram and the main program were in different files and
compiled separately how could you ensure that the main program knows what
return type the function has ?

But these declaration are located onlz _once_ at the begining of the
file (before the main routine starts).

That just reflects the scoping rules in the language - C has a concept of file
scope, while Fortran has different rules. In particular you should look into
Modules that came in in Fortran 90, where you can just declare things "once at
the beginning"

Fortran requires its
declarations "within the main routine" and within every
subroutine/function where i'm going to use the this function, and what
confuses me somewhat more, that it's just required for double
precision, but not for integer or real ...


This reflects the implicit Typing of the languages - in C functions return
int by default so one could argue that it only applies to functions returning
other types. Let's take a look at your example:

program bcont

double precision c
double precision a /2.0/
double precision b /2.0/

c=r(a,b)


write(*,*) 'result = ',c

stop
end


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

By default in fortran love it or loath it variables and functions
beginning with a-h and o-z are real, while i-n is integer. So "god"
is real, but not complex or logical. If you want something that is
not real or integer you have to declare it ( or ... no I won't go there ).
Thus you must declare all double precision variables. In fact many people
think using the implicit typing scheme is bad practice, or at the very least
leaves your code more susceptible to bugs due to typos, and so many people would
use "Implicit None" which forces you to declare all your variables. Having to
declare all the variables that are in scope ? Is that really that much different
from many other languages ?

BTW as hinted above you seem to be using Fortran 77, which is 3 standard revisions
out of date. I would suggest if you want to start using Fortran again you look
into Fortran 90 & 95, I would suggest the latest standard but there aren't any full
compilers out for it yet,

Ian

.



Relevant Pages

  • Object Oriented Optimization
    ... I have the following problem with re-using a fortran 77 optimization ... double precision, dimension, intent:: co ... several objectives and constraints. ...
    (comp.lang.fortran)
  • Re: help about ARPACK solver
    ... Fortran uses single precision variables by default. ... Fortran program, that is *single* precision. ... I still dont know what kind of criteria Matlab adopts to form ...
    (sci.math.num-analysis)
  • Re: help about ARPACK solver
    ... Fortran uses single precision variables by default. ... Fortran program, that is *single* precision. ...
    (sci.math.num-analysis)
  • Re: help about ARPACK solver
    ... could be as precise as, however, when I used the eigenvector ... Fortran uses single precision variables by default. ... Fortran program, that is *single* precision. ...
    (sci.math.num-analysis)
  • Re: help about ARPACK solver
    ... Matlab's eigsfunction uses ARPACK, so logically your Fortran ... Matlab uses 'double precision' variables throughout by default. ... Fortran uses single precision variables by default. ... Fortran program, that is *single* precision. ...
    (sci.math.num-analysis)