Why Are My Variables Changing Unexpectedly in My Freefall Program?

  • Thread starter Thread starter LucasCampos
  • Start date Start date
  • Tags Tags
    Issues Variables
Click For Summary

Discussion Overview

The discussion revolves around a student's issues with variable changes in a freefall simulation program written in Fortran. The focus is on understanding why certain variables ('y', 'v', and 'nshow') exhibit unexpected behavior after subroutine calls, particularly in the context of parameter passing and variable declaration.

Discussion Character

  • Technical explanation
  • Debugging

Main Points Raised

  • One participant identifies a potential issue with the order of actual and formal parameters in the subroutine call, suggesting that mismatches could lead to unexpected variable behavior.
  • Another participant confirms that the order of parameters was indeed the problem, indicating that it was resolved by correcting the order.
  • A participant points out that the variable 'nshow' was not declared, which could contribute to the issues faced in the program.
  • There is a mention of the importance of using 'IMPLICIT NONE' to enforce variable declarations, which may help prevent similar issues in the future.
  • A historical anecdote is shared about a past debugging experience involving a common typographical error in variable names, highlighting the challenges in programming.

Areas of Agreement / Disagreement

Participants generally agree on the importance of parameter order and variable declaration in avoiding unexpected behavior in the program. However, the discussion does not reach a consensus on all aspects of the variable issues, as some points remain open to interpretation.

Contextual Notes

There are limitations regarding the assumptions made about variable declarations and the specific behavior of the Fortran language in this context. The discussion does not resolve all potential causes of the variable changes.

Who May Find This Useful

This discussion may be useful for students and programmers working with Fortran, particularly those facing challenges with subroutine parameter passing and variable management in their code.

LucasCampos
Messages
17
Reaction score
0
I'm a student, and I had to do a freefall program. But I'm having some issues with variables. As soon as a subroutine ends, they go bananas! The problematic trio is 'y','v' and 'nshow'. When the subroutine Euler ends, 'v' gets the module of y, y go to zero and nshow to 32767!

How can I fix that?

_________________________________________

Code:
PROGRAM free_fall
IMPLICIT NONE
DOUBLE PRECISION y,a,g,t,dt,v
INTEGER nshow, counter
t = 0.0d0
OPEN(UNIT=1, FILE="TEST.dat")
OPEN(UNIT=2, FILE="TESTEULER.dat")
CALL initial (y,v,a,g,t,dt)
WRITE (1,*) v,y,nshow
!^Here my variables change misteriously
counter=0
CALL Euler (y,v,a,t,dt,nshow,counter)
CALL print_table(y,v,a,dt,t,nshow)
END

SUBROUTINE initial(a,y,v,g,t,dt)
DOUBLE PRECISION y,a,g,t,dt,v
WRITE(*,*) 'time step ='
READ(*,*) dt
WRITE(*,*) 'height ='
READ(*,*) y
WRITE(*,*) 'initial velocity ='
READ(*,*) v
WRITE(*,*) 'number of times steps between output ='
READ(*,*) nshow
WRITE (1,*) v,y,nshow
!^Until this point, variables are OK
END

SUBROUTINE Euler(y,v,a,t,dt,nshow,counter)
DOUBLE PRECISION y,a,t,dt,v
INTEGER nshow,counter
OPEN(UNIT=4, FILE="vt.dat")
OPEN(UNIT=3, FILE="yt.dat")
a = -9.8
100 v = v+a*dt;
y = y+v*dt
t = t+dt
counter = counter + 1
IF (mod(counter,nshow) .EQ. 0.0D0) CALL print_table(y,v,a,dt,t,nshow)
WRITE (2,*) t,y,v,dt,a,counter,nshow
WRITE (*,*) t,y,v,dt,a
IF (y .LE. 0.0D0) go to 200
go to 100
200 STOP
END

SUBROUTINE print_table(y,v,a,dt,t)
DOUBLE PRECISION y,t,v
WRITE (4,*) t,v
WRITE (3,*) t,y
!Each table will be made in a file. TEST and TESTEULER are, obviously, tests
END
______________________________________________________
 
Technology news on Phys.org
When you call initial, the actual parameters are CALL initial (y,v,a,g,t,dt).
The formal parameters in your initial subroutine definition are SUBROUTINE initial(a,y,v,g,t,dt).

Since you are using variables with the same names in the call and the definition, you should keep them in the same order in both places. I think that's what your problem is.
 
OMG! Thanks! I've been wrestling against tis problem for a full day already! It was exatly as you said. Just the order. The nshow problem is that it was not being declared.
 
LucasCampos said:
The nshow problem is that it was not being declared.

That sort of thing is why, when I still used Fortran, I always put IMPLICIT NONE at the beginning of each program and subroutine, to force me to declare all my variables.

I remember once spending an hour helping a student find a bug in her program, and it turned out she had mis-spelled a variable in one occurrence, substituting the letter O for the digit 0, or the letter I for the digit 1, or something like that.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
14K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 8 ·
Replies
8
Views
8K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 8 ·
Replies
8
Views
4K
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K