Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Beginner
28 Views

Test does not initialize a variable.

Is it OK in subroutine Program_main? Should be inicialized variable c in subroutine Program_main (before calling Subprogram)? When I started with Fortan (10 years ago) this example caused the program crash.  It is now a long time and I do not remember the details. Now, the example works well (Fortran v 2016.0.110). For performance reasons, I want to initialize only the necessary variables. 

!*******************************************************************************
  subroutine Program_main
    use, intrinsic :: ieee_arithmetic, only: IEEE_VALUE, IEEE_QUIET_NAN, IEEE_QUIET_NAN, IEEE_POSITIVE_DENORMAL, IEEE_NEGATIVE_DENORMAL
    implicit none
    real(8) :: a, b, c
    
    a = 10.d0
    b = 20.d0
    
    c = IEEE_VALUE (c, IEEE_QUIET_NAN)  ! IEEE_QUIET_NAN
    call Experiment13_subprogram (a, b, c)
    
    return
  end subroutine Program_main
  
  
  !*******************************************************************************
  subroutine Subprogram (a, b, c)
    implicit none
    real(8), intent(in) :: a, b
    real(8), intent(out) :: c
    c = a + b
    return
  end subroutine Subprogram

 

0 Kudos
5 Replies
Highlighted
Valued Contributor II
28 Views

I have no idea why this program crashed, just that ten years ago there was no module IEEE_ARITHMETIC defined within the standard :).

In general though:

  • Fortran does not implicitly initialise variables, you have to do that yourself.
  • Consequently, using a variable that has not been set in an expression is a bug

Now, in the code you first set c to a particular value (a special one, by the way) and then call a subroutine to set that variable again. Even if you were to leave out the statement at line 10, nothing should go wrong: you are not using variable c before it has been set. The other two values also have values before they are used.

0 Kudos
Highlighted
Black Belt
28 Views

Arjen Markus wrote:

I have no idea why this program crashed, just that ten years ago there was no module IEEE_ARITHMETIC defined within the standard :).

You may be older than you think!

0 Kudos
Highlighted
Valued Contributor II
28 Views

Oops, simple arithmetic is failing me :)

0 Kudos
Highlighted
28 Views

This is (should) not be a bug, your ONLY clause lists IEEE_QUIET_NAN twice. I think you intended to have one of them as IEEE_SIGNALING_NAN. Your 10 year old compiler may have choked on the redundant ONLY.

Jim Dempsey

0 Kudos
Highlighted
Beginner
28 Views

On line 11 should be call Subprogram (a, b, c) and no call Experiment13_subprogram (a, b, c), but you understand my question. Thank you for all the answers.

0 Kudos