Next: Procedure Interfaces Up: More on Procedures Previous: Scope   Contents

Scope example

```program test_scope
implicit none
integer :: i=1, j=2, k=3, glb=4, loc=5
!
print*,"(1) main:   i=",i,", j=",j, &
", k=",k,", glb=",glb,", loc=",loc
call my_sum(i,j,k)
print*,"(2) main:   i=",i,", j=",j, &
", k=",k,", glb=",glb,", loc=",loc
!
contains
!
subroutine my_sum(a,b,c)
integer, intent(in)  :: a,b
integer, intent(out) :: c
integer :: loc=7
print*,"(1) my_sum: i=",i,", j=",j, &
", k=",k,", glb=",glb,", loc=",loc, &
", a=",a,", b=",b,", c=",c
c = a**2 + b**2
!
! c = i**2 + j**2 is ok, same result,
!     since we read i, j in <my_sum>
! k = a**2 + b**2 is illegal since
!     we cannot write k;
!
glb = 6  ! seen by main (global)
loc = 8  ! NOT seen by main (local)
!
print*,"(2) my_sum: i=",i,", j=",j, &
", k=",k,", glb=",glb,", loc=",loc, &
", a=",a,", b=",b,", c=",c
end subroutine my_sum
!
end program test_scope
```

In the subroutine my_sum the following variables are visible

• a,b,c by argument association
• i,j,k,glb by host association (global variables)
• loc locally owned by my_sum (no relation to m in main).

The results produced by the above program are

``` (1) main:   i=1, j=2, k=3, glb=4, loc=5
(1) my_sum: i=1, j=2, k=3, glb=4, loc=7, a=1, b=2, c=3
(2) my_sum: i=1, j=2, k=5, glb=6, loc=8, a=1, b=2, c=5
(2) main:   i=1, j=2, k=5, glb=6, loc=5
```

glb is a global variable; its declaration in the main program is valid within my_sum. In consequence, the modification glb = 6 is visible in the main program also (even after the function ends).

loc is declared in main, but is also re-declared as a local variable in my_sum. This local re-declaration overrides the global declaration within the body of the function. Therefore, loc is a local variable of the function my_sum (and bears no relationship with the loc in main - they're like John from California and John from Wisconsin). The modification loc = 8 is seen locally only; when the function terminates and control is returned to main program, loc is restored to the value it had before the function call (this value, 5, is local to the main program).

Variables i,j,k are in a special situation: they are the actual arguments when calling my_sum (and therefore are visible by argument association under the dummy names a,b,c) and are in the same time visible by host association (under their original names i,j,k). In order to prevent uncontrollable side-effects, F90 syntax allows us to read i,j,k inside my_sum, but forbids us to modify them (they behave like dummy arguments with INTENT(IN)). If we want to modify k, for example, we have to use the equivalent dummy (argument) name c.

The hosted functions and subroutines inherit not only the declared variables but also the IMPLICIT NONE declaration.

Finally, note that internal procedures cannot be used as arguments (only INTRINSIC and EXTERNAL procedures can, and they need to be declared with the proper attribute).

Next: Procedure Interfaces Up: More on Procedures Previous: Scope   Contents