Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
Welcome to the Intel Community. If you get an answer you like, please mark it as an Accepted Solution to help others. Thank you!

Test does not initialize a variable.

Lukas_W_
Beginner
155 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
Valued Contributor III
155 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.

IanH
Black Belt
155 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!

Arjen_Markus
Valued Contributor III
155 Views

Oops, simple arithmetic is failing me :)

jimdempseyatthecove
Black Belt
155 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

Lukas_W_
Beginner
155 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.

Reply