Prev: elemental function question regarding use of array slices
Next: Variable scope: how fortran deals with undeclared variables in subroutines?
From: renatoab on 17 Feb 2010 09:50 Hi everyone, I'm trying to fix a program which have many subroutines and many problems with undeclared variables. I know the best way to do this is to declare them in every subroutines or pass by parameters, but the program is very complicated, and first I am trying to understand what happens with the variables, because I'm afraid to mess something if I make many changes. Sometimes a variable is used in a subroutine A, without being declared first, and it has a value. Where this value come from? - the last time a variable with the same name is declared out of the routine? (it doesn't seem to be the answer, because when the variable is declared in a subroutine B that calls the subroutine A, the values in A and B are different) - the last time a variable is used in a previous call to that subroutine? - any value stored in the memory? I'm confused because it seems to store a value set in a subroutine C that does not have anything to do with A. There is no "common" in any subroutine. Thanks in advance, Renato
From: cjpsimon on 17 Feb 2010 10:27 On 17 fév, 15:50, renatoab <renato...(a)yahoo.com.br> wrote: > Hi everyone, > > I'm trying to fix a program which have many subroutines and many > problems with undeclared variables. > I know the best way to do this is to declare them in every subroutines > or pass by parameters, but the program is very complicated, and first > I am trying to understand what happens with the variables, because I'm > afraid to mess something if I make many changes. > Sometimes a variable is used in a subroutine A, without being declared > first, and it has a value. Where this value come from? > - the last time a variable with the same name is declared out of the > routine? (it doesn't seem to be the answer, because when the variable > is declared in a subroutine B that calls the subroutine A, the values > in A and B are different) > - the last time a variable is used in a previous call to that > subroutine? > - any value stored in the memory? > I'm confused because it seems to store a value set in a subroutine C > that does not have anything to do with A. There is no "common" in any > subroutine. > > Thanks in advance, > > Renato You may have a look at photran, a fortran tool for eclipse : see Photran 5.0 Advanced Features. Refactoring could help you with rename and implicit none.
From: m_b_metcalf on 17 Feb 2010 10:54 On Feb 17, 3:50 pm, renatoab <renato...(a)yahoo.com.br> wrote: > - the last time a variable with the same name is declared out of the > routine? (it doesn't seem to be the answer, because when the variable > is declared in a subroutine B that calls the subroutine A, the values > in A and B are different) No, because that would be a different variable in a different scope. > - the last time a variable is used in a previous call to that > subroutine? Yes, but for the first time see next answer. > - any value stored in the memory? Yes, for the first call, and for subsequent ones too if the variable does not have the save attribute (a default on some processors). Regards, Mike Metcalf
From: Les Neilson on 17 Feb 2010 12:40 "renatoab" <renato_eq(a)yahoo.com.br> wrote in message news:7e197bea-7e7b-42e0-8759-ae4abf8d2a7e(a)g11g2000yqe.googlegroups.com... > Hi everyone, <snip> > I'm confused because it seems to store a value set in a subroutine C > that does not have anything to do with A. There is no "common" in any > subroutine. > > Thanks in advance, > > Renato You didn't show any code so it is difficult to guess the answer but : (a) are there any "include" statements which may have "common" within them ? (b) are there any "use" statements where the variables can be accessed via a module ? Les
From: dpb on 17 Feb 2010 16:36
renatoab wrote: .... ....some comments inline in addition to other good ones... > I'm trying to fix a program which have many subroutines and many > problems with undeclared variables. > I know the best way to do this is to declare them in every subroutines > or pass by parameters, ... Well, that's the only way excluding COMMON (and assuming this is a program that predates F90 and therefore, modules) Now, there may not be an explicit REAL/INTEGER/etc. statement, but they're still implicitly declared and have default types based on the Fortran rule of real unless first letter is I-N unless there is an IMPLICIT statement that changes the default naming behavior. The first thing to remember is that Fortran variables are in scope only in the program unit unless they are either in COMMON or a MODULE-level variable or thru argument association. Hence, w/o such connection there is no relationship between "X" in the main program and any subroutine or function "X" is local unless X is used both as the actual and dummy argument, is in COMMON or is module-global. > I am trying to understand what happens with the variables, because I'm > afraid to mess something if I make many changes. > Sometimes a variable is used in a subroutine A, without being declared > first, and it has a value. Where this value come from? > - the last time a variable with the same name is declared out of the > routine? (it doesn't seem to be the answer, because when the variable > is declared in a subroutine B that calls the subroutine A, the values > in A and B are different) See above...these are different entirely variables. > - the last time a variable is used in a previous call to that > subroutine? Given that it has SAVE attribute which is, as somebody noted already, a default of some compilers. Note that a SAVE statement w/o a variable list means everything. Also note there are some specific ways the SAVE attribute is set; one of which is if the variable or array is initialized in a DATA statement. > - any value stored in the memory? > I'm confused because it seems to store a value set in a subroutine C > that does not have anything to do with A. There is no "common" in any > subroutine. Also as somebody else noted, look for INCLUDE statements and default initializations and BLOCK DATA. You might find either COMMON or other ways for initializations thereby. Then again, there's always a possibility that the original coder didn't get everything initialized and relied on either a compiler switch to zero memory or the behavior of a specific compiler that did so. By default Fortran does not initialize so on an OS that doesn't clear process memory indeed random values from memory can be found. There are some compilers that have features to help in finding non-initialized variables; post your compiler of choice and somebody here will know its features in all likelihood. -- |