<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[Approximatrix Forums — Modify variable inside a subroutine]]></title>
	<link rel="self" href="https://forums.approximatrix.com/extern.php?action=feed&amp;tid=212&amp;type=atom" />
	<updated>2013-06-07T16:26:12Z</updated>
	<generator>PunBB</generator>
	<id>https://forums.approximatrix.com/viewtopic.php?id=212</id>
		<entry>
			<title type="html"><![CDATA[Re: Modify variable inside a subroutine]]></title>
			<link rel="alternate" href="https://forums.approximatrix.com/viewtopic.php?pid=821#p821" />
			<content type="html"><![CDATA[<p>I would suggest possibly moving your variables into a module that both the main program and the subroutine &quot;use.&quot;</p><div class="codebox"><pre><code>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</code></pre></div><p>In the above example, the variables are still compartmentalized via the myData module, but both your subroutine and the main program can modify them.</p>]]></content>
			<author>
				<name><![CDATA[jeff]]></name>
				<uri>https://forums.approximatrix.com/profile.php?id=2</uri>
			</author>
			<updated>2013-06-07T16:26:12Z</updated>
			<id>https://forums.approximatrix.com/viewtopic.php?pid=821#p821</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Modify variable inside a subroutine]]></title>
			<link rel="alternate" href="https://forums.approximatrix.com/viewtopic.php?pid=819#p819" />
			<content type="html"><![CDATA[<p>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.</p><p>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.</p><p>Example :<br />MODULE myModule<br />&nbsp; &nbsp; IMPLICIT NONE<br />&nbsp; &nbsp; CONTAINS<br />&nbsp; &nbsp; &nbsp; &nbsp; subroutine mySub()<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; integer :: a, b, c<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;a=2;b=5;c=3<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end subroutine mySub</p><p>program main<br />&nbsp; &nbsp;use myModule<br />&nbsp; &nbsp;integer :: a, b, c<br />&nbsp; &nbsp; &nbsp;a = 0<br />&nbsp; &nbsp; &nbsp;call mySub()<br />&nbsp; &nbsp; a = a +1<br />end program main</p><p>I want my program to give the value 3 to variable &#039;a&#039;, not 1. I know I can do :<br />subroutine mySub(a,b,c)<br />&nbsp; &nbsp; integer, intent(inout) :: a, b, c</p><p>But that&#039;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?</p><p>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.</p><p>Sorry for my bad english...</p>]]></content>
			<author>
				<name><![CDATA[pierre.peclet-ext]]></name>
				<uri>https://forums.approximatrix.com/profile.php?id=3453</uri>
			</author>
			<updated>2013-06-07T09:18:07Z</updated>
			<id>https://forums.approximatrix.com/viewtopic.php?pid=819#p819</id>
		</entry>
</feed>
