View previous topic :: View next topic |
Author |
Message |
DanRRight
Joined: 10 Mar 2008 Posts: 2908 Location: South Pole, Antarctica
|
Posted: Tue Apr 01, 2025 5:43 pm Post subject: What was the reason behind this rule? |
|
|
Was always puzzled why this is allowed only if you set variable i as a PARAMETER and not as regular variable?
Code: | integer :: i = 1
integer :: j = 1-i
*** Error 538: I is not a PARAMETER, so cannot appear in an initialisation expression |
|
|
Back to top |
|
 |
Kenneth_Smith
Joined: 18 May 2012 Posts: 797 Location: Hamilton, Lanarkshire, Scotland.
|
Posted: Tue Apr 01, 2025 6:19 pm Post subject: |
|
|
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. |
|
Back to top |
|
 |
|