Is there some simple way to initiate group of variables in Fortran subroutines? Something like DATA but working each time when subroutine is called. Or the only solution is to use x = 0.0
for all variables?
2 Answers
Yes, to set a value of a variable use an assignment (=
).
You could make a derived type and a user-defined assignment to simplify the syntax.

- 57,977
- 4
- 76
- 119
I also needed to zero several variables in the beginning of a subroutine which was to be called more than once. I just copied the text used in the declaration of variables to a blank new file in the code editor and used the Find and Replace to replace all ,
by =0;
or =0.0DE0;
Here is an example:
Double precision AP(nPor), ACCor(nThr,4), ...
Replacing all (nPor),
and (nThr,4),
by =0.0D0;
AP=0.0D0; ACCor=0.0D0;
Then all variables were promptly set to zero. I only have not managed to use Wildcards like (*),
to save the time to look at each type of arguments used in the variables though.
In case this turn around is not suitable at all, you may try to avoid the SAVE attribute in subroutines, as mentioned in Does fortran preserve the value of internal variables through function and subroutines

- 1
- 1

- 11
- 2
-
Excuse me, but I am pretty sure the quesion was about a method to automatically initiate a bunch of variable, task which I called zeroing variables. This way, to simply avoid typing e.g. "=0" or "(dim,dim)=0.0D0" for ALL variables, I suggested the method to write it all quickly. @Alexander – Giovane BLN Feb 08 '15 at 14:48
-
OK, I edited your answer to make more clear what you did. Note you can just use `ACCor=0`. The complications with `dim` are unnecessary. – Vladimir F Героям слава Feb 08 '15 at 14:57
-
I appreciate that a lot and ask you to kindly remove your Vote Down, since my answer did help, besides not being so clear for this time. Thanks – Giovane BLN Feb 08 '15 at 15:12
-
Unfortunately, I cannot remove the down vote, because it is not mine. – Vladimir F Героям слава Feb 08 '15 at 15:13