Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

assigning variables in different subroutines

Sirat_S_
Beginner
452 Views

Hi Everyone

I wonder whether it is possible to assign variable to values in different subroutines. for example, 

subroutine a

b=5

a=b+c

call cvalue

end subroutine a

subroutine cvalue

c=2

end subroutine cvalue

Please help.

0 Kudos
2 Replies
andrew_4619
Honored Contributor III
452 Views
module fred

implicit none      ! always always always use this!!!!!
integer :: a, b, c ! the variables are shared  by the contained subroutines

contains  ! module fred contains subroutines a and cvalue
subroutine a
   b=5
   a=b+c
  call cvalue
end subroutine a
subroutine cvalue
   integer :: b ! this b is local it is not the same as the module level b!!!!!!
   b=1
   c=2+b
end subroutine cvalue

end module fred

 

0 Kudos
Arjen_Markus
Honored Contributor II
452 Views

Wouldn't it be easier to pass the variable c via the argument list? Something aong these lines:

subroutine a
implicit none
integer :: a, b, c ! Inferred from the value 5

b=5
a=b+c ! Hm, c has no value yet ...

call cvalue( c )

end subroutine a

subroutine cvalue( c )
implicit none
integer, intent(out) :: c 

c=2

end subroutine cvalue

 

0 Kudos
Reply