1 (edited by pierre.peclet-ext 2013-06-07 09:22:32)

Topic: Modify variable inside a subroutine

What I am asking is not safe at all, but I work on an old code, and that seems to be the fastest way to do it.

So, I have a main program, and a module with subroutine. This module is call by my main program. In my subroutine, a lot of variable are modified, and I want all these modifications to be effective in my main program after calling my subroutine.

Example :
MODULE myModule
    IMPLICIT NONE
    CONTAINS
        subroutine mySub()
                          integer :: a, b, c
                         
                           a=2;b=5;c=3
               end subroutine mySub

program main
   use myModule
   integer :: a, b, c
     a = 0
     call mySub()
    a = a +1
end program main

I want my program to give the value 3 to variable 'a', not 1. I know I can do :
subroutine mySub(a,b,c)
    integer, intent(inout) :: a, b, c

But that's not what I want, because there are too many variables (like 100). Is there a way to automatically have all my variable in my subroutine act as an intent(out) value without call them in the call of my subroutine?

In fortran77, when you had a program and a subroutine in the same file, it was possible to modifiy variables in the subroutine, and used the new value in the main program after calling the subroutine. I ask for the same thing.

Sorry for my bad english...

Re: Modify variable inside a subroutine

I would suggest possibly moving your variables into a module that both the main program and the subroutine "use."

module myData
    integer::a, b, c
end module myData

module myModule
implicit none
contains
    subroutine mySub()
    use myData
    implicit none
        a=2
        b=5
        c=3
    end subroutine mySub
end module myModule

program main
use myModule
use myData
implicit none
    
    a = 0
    call mySub()
    a = a +1
    
end program main

In the above example, the variables are still compartmentalized via the myData module, but both your subroutine and the main program can modify them.

Jeff Armstrong
Approximatrix, LLC