Skip to content

Commit

Permalink
Update gotchas.md (#454)
Browse files Browse the repository at this point in the history
The "implied save" paragraph was not fully correct
  • Loading branch information
PierUgit committed Aug 24, 2024
1 parent e3d902c commit fcc0d33
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions source/learn/quickstart/gotchas.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,15 @@ c = 0
```
`integer :: c=0` is actually a one-shot **compile time initialization**, and it makes the variable persistent between calls to `foo()`. It is actually equivalent to:
```
integer, save :: c=0
integer, save :: c=0 ! "save" can be omitted, but it's clearer with it
```
The `save` attribute is equivalent to the C `static` attribute to make a variable persistent, and it is *implied* in the case the variable is initialized. This is a modernized syntax (introduced in Fortran 90) compared to the legacy (and still valid) syntax:
The `save` attribute is equivalent to the C `static` attribute used inside a function to make a variable persistent, and it is *implied* in the case the variable is initialized. This is a modernized syntax (introduced in Fortran 90) compared to the legacy (and still valid) syntax:
```
integer c
data c /0/
save c
```
Old fortraners just know that the modernized syntax is equivalent to the legacy one, even when `save` is not specified. But as a matter of fact the *implied save* can be misleading to newcomers who are used to the C logic. That's why it is generally recommended to **always** specify the `save` attribute:
```
integer, save :: c=0 ! save could be omitted, but it's clearer with it
```
Old fortraners just know that the modernized syntax is equivalent to the legacy one, even when `save` is not specified. But as a matter of fact the *implied save* can be misleading to newcomers who are used to the C logic. That's why it is generally recommended to **always** specify the `save` attribute.

*Note: an initialization expression of a derived type component is a fully different case:*
```
Expand Down

0 comments on commit fcc0d33

Please sign in to comment.