Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Why fortran program produces different values, when used as a subroutine ?

  1. Aug 30, 2012 #1
    fortran users
    I, have a problem regarding values that my program produces. Let me detailed it to you.
    See, i have createrd a separate fortran program to calculete area under a given function i.e.
    it perfroms integration. When this program runs independently it gives me area of the function say 20units for some limits. Now, if i use same programe in another big program which uses this as a subroutine and it calculate area of the same function between the same limits and gives area near about half (bit less or bit more) of the value it calculated independently, e.g. i can say it gives me area less than 10units.
    One more thing is that, whole program is written in the Double Precession mode, and the function to be integrated contains sine and cosine function, i am using their double kind i.e. Dsin, Dcos etc.
    Why i am getting this drastic change? mind it when i use it as a subroutine, i simply copy and paste it in bigger program and i do not put any extra dot to modify it?
    How can copy and patse change the value ?
    Plz. answer?
  2. jcsd
  3. Aug 30, 2012 #2


    User Avatar
    Science Advisor

    If it was just a small difference then it could just something going on with the precision (kind) used. With the huge differences you're reporting however, it sounds like a more serious bug. Usually with this type of thing it's an undeclared local variable. Are you using "implicit none" in your subroutine? If not, then do it now, it might pinpoint the error.

    Anther really important question, can you verify if either of the two different results you obtain are actually the correct value for the integral?
  4. Aug 30, 2012 #3
    Do not use the code as main in one program and as a subroutine in another...make it so that it is a subroutine in BOTH programs.

    Add some debugging statements, like printing the input parameters as soon as you enter the subroutine and printing the result before you exit...maybe you are not actually getting what you think you are.

    Make sure everything is declared explicitly as INTEGER or REAL as necessary.
    Make sure you are not missing out by accidentally carrying out integral division between two integers, if that is not what you want.
    Try to pass EVERYTHING the subroutine needs through its arguments...no other way (common block or include statement or anything like that).
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook