Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28383 Discussions

Test does not initialize a variable.

Lukas_W_
Beginner
391 Views

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
Arjen_Markus
Honored Contributor I
391 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
IanH
Honored Contributor II
391 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
Arjen_Markus
Honored Contributor I
391 Views

Oops, simple arithmetic is failing me :)

0 Kudos
jimdempseyatthecove
Honored Contributor III
391 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
Lukas_W_
Beginner
391 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
Reply