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

Fun with fixed-form source and parameter

Harald1
New Contributor II
1,053 Views

Hello,

the following code in fixed-form source surprised me:

      program funny
      call foo0 ()
      call foo1 ()
      call foo2 ()
      end

      subroutine foo0 ()
      parameter (s = 42)
*     s = 42                    ! Check if PARAMETER was seen
      print *, s
      end

      subroutine foo1 ()
      parameters = 42.
*     s = 42                    ! Check if PARAMETER was seen
      print *, s
      end

      subroutine foo2 ()
      parameters = 42           ! this is somehow different...
*     s = 42                    ! Check if PARAMETER was seen
      print *, s
      end

 I get:

% ifx ifort-funny-parameter.f -what && ./a.out 
 Intel(R) Fortran 24.0-1472.3
   42.00000    
   42.00000    
          42

 I was wondering why the last print actually thinks that s is an integer and not a real.

Of course the code is not standard-conforming (diagnosed with -stand), although "legacy-style", given that the parameter statements are accepted without major complaint.

Is there something I should know about parameter in fixed-form?  Is the type of the variable s really derived from the r.h.s.?

 

0 Kudos
1 Solution
mecej4
Honored Contributor III
1,022 Views

The Intel Fortran documentation says:

Omission of the parentheses in a PARAMETER statement is an extension controlled by compiler option altparam. In this form, the type of the name is taken from the form of the constant rather than from implicit or explicit typing of the name.

Thus, when the non-standard interpretation is allowed/sought, the type of the variable is integer when the constant has no decimal point.

Lines 14 and 20 could be parsed as setting the  variable parameters (implicitly declared as type real) to 42.

Change those lines to

 

      parameterx = 42

 

and see the effect.

View solution in original post

5 Replies
mecej4
Honored Contributor III
1,023 Views

The Intel Fortran documentation says:

Omission of the parentheses in a PARAMETER statement is an extension controlled by compiler option altparam. In this form, the type of the name is taken from the form of the constant rather than from implicit or explicit typing of the name.

Thus, when the non-standard interpretation is allowed/sought, the type of the variable is integer when the constant has no decimal point.

Lines 14 and 20 could be parsed as setting the  variable parameters (implicitly declared as type real) to 42.

Change those lines to

 

      parameterx = 42

 

and see the effect.

Harald1
New Contributor II
928 Views

Hmm, I just tried my example above with -altparam and with -noaltparam and I do *not* see any difference!  Any type on the r.h.s. (integer, even complex) is used to infer the lhs type.

Maybe these options are no longer fully functional...

 

0 Kudos
andrew_4619
Honored Contributor III
1,014 Views

When you stray into the delta zone of implicit typing fixed form and worst of all no standards checking all manner of strange and puzzling things start to happen. In my world if you can't apply a standards then it is broken code.

Steve_Lionel
Honored Contributor III
951 Views

A bit of history: the form of PARAMETER without the parentheses was in the FORTRAN 77 draft, and DEC implemented it in several of their compilers. Then the final standard came out and the syntax (and semantics) had changed, so DEC had to support both.  This is why DEC (and other vendors) became reluctant to implement features found in draft standards.

tanishawn
Beginner
150 Views

 

Modern Free-Form Fortran Version (funny_parameters.f90)

 

fortran
CopyEdit
program funny implicit none call foo0() call foo1() call foo2() end program funny !------------------------------------------------------------ subroutine foo0() implicit none integer, parameter :: s = 42 ! s is a constant integer print *, "foo0: s =", s end subroutine foo0 !------------------------------------------------------------ subroutine foo1() implicit none real, parameter :: s = 42.0 ! s is a constant real print *, "foo1: s =", s end subroutine foo1 !------------------------------------------------------------ subroutine foo2() implicit none integer :: s s = 42 ! s is a regular integer variable print *, "foo2: s =", s end subroutine foo2

Output When You Compile and Run:

bash
CopyEdit
foo0: s = 42 foo1: s = 42.00000 foo2: s = 42

 

🧪 How to Compile and Run:

 

bash
CopyEdit
ifort funny_parameters.f90 -o funny ./funny

 

or using gfortran:

 

bash
CopyEdit
gfortran funny_parameters.f90 -o funny ./funny
0 Kudos
Reply