codee-com / open-catalog

A collaborative effort to consolidate expert knowledge on code guidelines for the correctness, modernization, and optimization of code written in C, C++, and Fortran programming languages
https://open-catalog.codee.com/
Apache License 2.0
83 stars 9 forks source link

Fortran allows declaration of variables within loops, contrary to what PWR002 says #1

Closed Beliavsky closed 8 months ago

Beliavsky commented 9 months ago

The statement that "the Fortran programming language does not allow to declare variables inside loops, in the statements of the loop body." at https://github.com/codee-com/open-catalog/tree/main/Checks/PWR002 is incorrect, since Fortran 2008 introduced the BLOCK construct, described at https://www.ibm.com/docs/en/xffbg/121.141?topic=control-block-construct-fortran-2008 . For example, the following code compiles and runs with gfortran:

program main
implicit none
integer :: i
do i=1,5
   block
      integer :: isq
      isq = i**2
      print*,i,isq
   end block
end do
end program main

Note: the original code did not correctly demonstrate that isq can be declared within a loop, as @jalvesz points out below. I have corrected the code.

daniel-otero commented 9 months ago

You are right. We completely missed the Fortran 2008 BLOCK feature. We take note, and we will soon update the rule accordingly.

We will let you know when the wording gets updated. Until then, the issue will remain open.

Thank you very much!

jalvesz commented 9 months ago

Just a small change in the code snippet to showcase what @Beliavsky tried to show:

program main
implicit none
integer :: i
do i=1,5
   block 
    integer :: isq !> variables can be defined here
      isq = i**2
      print*,i,isq
   end block
end do
end program main
alvrogd commented 8 months ago

Resolved in #9! Thank you all for your feedback :)