webpage icon indicating copy to clipboard operation
webpage copied to clipboard

Add example of the block construct for local variables

Open jalvesz opened this issue 1 year ago • 0 comments

Following this comment https://fortran-lang.discourse.group/t/fortran-returns-to-top-20-tiobe-index/1069/184 by @Beliavsky

What about adding something like this to the page https://github.com/fortran-lang/webpage/blob/main/source/learn/quickstart/variables.md?plain=1 ?

## Local scope variables with `block` construct
The 2008 Fortran standard introduced the notion of `block` which enables using local scope variables within a program or procedure.

**Example:**

```{play-code-block} fortran
module your_module
    implicit none
    integer :: n = 2
end module

program main
    implicit none
    real :: x

    block
        use your_module, only: n ! you can import modules within blocks
        real :: y ! local scope variable
        y = 2.0
        x = y ** n
        print *, y
    end block
    ! print *, y ! this is not allowed as y only exists during the block's scope
    print *, x  ! prints 4.00000000
end program

jalvesz avatar Jan 27 '24 09:01 jalvesz