Solving Fortran 77 Mystery: nran(i)=mod(int(i*ranw(Idum)),i) + 1

  • Context: Fortran 
  • Thread starter Thread starter kanato
  • Start date Start date
  • Tags Tags
    Fortran Line Weird
Click For Summary
SUMMARY

The discussion clarifies the use of the statement function in Fortran 77, specifically regarding the line nran(i)=mod(int(i*ranw(Idum)),i) + 1. This line defines the function nran() that must appear before any executable statements, which is why it generates an error if placed incorrectly. The function utilizes the mod and int functions, and ranw(Idum) to generate a random integer based on the input i. The confusion arises from the syntax, which resembles array access but is indeed a function definition.

PREREQUISITES
  • Understanding of Fortran 77 syntax and structure
  • Familiarity with statement functions in Fortran
  • Knowledge of the mod and int functions
  • Experience with random number generation in Fortran using ranw
NEXT STEPS
  • Study the implementation of statement functions in Fortran 77
  • Explore the differences between functions and arrays in Fortran
  • Learn about random number generation techniques in Fortran
  • Review the structure and usage of the mod and int functions in numerical computations
USEFUL FOR

Fortran developers, programmers working with legacy code, and anyone interested in understanding statement functions and random number generation in Fortran 77.

kanato
Messages
415
Reaction score
1
I have this line in a Fortran 77 program, and I don't understand how it works:

nran(i)=mod(int(i*ranw(Idum)),i) + 1

What I don't understand is that nran is not a function, nor is it dimensioned anywhere (its type is implicitly an integer). This line only works if it comes right after the variable declarations in the function. If I put some non-declaration statement above it then the compiler (ifort 9.1, Linux ia64) complains that nran is not a function or dimensioned anywhere. That's sort of a big "duh" but I don't understand why this works if it's above with the variable declarations. It seems to be declaring an array for nran, but I have no idea how it knows what size/bounds to make it, as there is no indication of what values i should take.

If you are interested, this is a program based off the lisaqmc.f program available http://www.physics.rutgers.edu/~udo/qmc/readme_lisaqmc.html" .

(I should mention, I understand what mod and int are, and ranw is a function which returns a random real number using the seed given in Idum. This line would be totally clear to me if it was in a loop and nran was dimensioned somewhere.)
 
Last edited by a moderator:
Technology news on Phys.org
Hi kanato,

That is a statement function. In fortran 77 rather than write a separate function subprogram, you could use statement functions, which were one-line declarations of functions that had to come before the executable statements (and that were only accessible to the program part that called them). You should be able to find instances where nran() is being called later on in that same part of the program.

So the line you are referring to is the definition of the function nran() that is being used elsewhere in the program.
 
Ok, that makes a lot more sense. I was a little confused because it looks like it's being read as an array latter on, but of course reading from an array and calling a function have the same syntax. Thanks for the clarification.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 13 ·
Replies
13
Views
1K
  • · Replies 17 ·
Replies
17
Views
4K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 36 ·
2
Replies
36
Views
3K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K