Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example of the block construct for local variables #377

Open
jalvesz opened this issue Jan 27, 2024 · 0 comments
Open

Add example of the block construct for local variables #377

jalvesz opened this issue Jan 27, 2024 · 0 comments

Comments

@jalvesz
Copy link
Contributor

jalvesz commented Jan 27, 2024

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant