Silverfrost Forums

Welcome to our forums

What was the reason behind this rule?

1 Apr 2025 4:43 #32048

Was always puzzled why this is allowed only if you set variable i as a PARAMETER and not as regular variable?

integer :: i = 1
integer :: j = 1-i
*** Error 538: I is not a PARAMETER, so cannot appear in an initialisation expression
1 Apr 2025 5:19 #32049

In Fortran, only constant expressions are allowed in an initialization expression.

A constant expression can comprise of:

  1. Literals (constant values)
  2. Named constants declared with the parameter attribute
  3. Intrinsic functions that operate on constants
  4. Arithmetic expressions using constants

Such expressions are evaluated at compile time and not at run time. Only parameters are known at compile time.

If Fortran allowed variable-dependent initialization, it would lead to undefined behavior because the compiler wouldn't know the correct value at compile time.

Please login to reply.