<?xml version="1.0" encoding="utf-8"?>
<feed xml:lang="en" xmlns="http://www.w3.org/2005/Atom"><title>Recent posts to how can I use STATIC VARIABLE in FUNCTION</title><link href="https://forge.codesys.com/forge/talk/CODESYS-V2/thread/a93d6a652c/" rel="alternate"/><link href="https://forge.codesys.com/forge/talk/CODESYS-V2/thread/a93d6a652c/feed.atom" rel="self"/><id>https://forge.codesys.com/forge/talk/CODESYS-V2/thread/a93d6a652c/</id><updated>2020-02-27T20:37:28.341000Z</updated><subtitle>Recent posts to how can I use STATIC VARIABLE in FUNCTION</subtitle><entry><title>how can I use STATIC VARIABLE in FUNCTION</title><link href="https://forge.codesys.com/forge/talk/Codesys%2520V2.3/thread/a93d6a652c/?limit=25#f963" rel="alternate"/><published>2020-02-27T20:37:28.341000Z</published><updated>2020-02-27T20:37:28.341000Z</updated><author><name>timvh</name><uri>https://forge.codesys.com/u/timvh/</uri></author><id>https://forge.codesys.com2ea9e4d178fb9bf21fe4744449764d03c562fafb</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;It is not really clear what you mean.&lt;br/&gt;
Static variables in functions are not available in V2.3. In V3 you can define them.&lt;br/&gt;
What you could do is use function blocks, or use global variables (which is not really good programming).&lt;/p&gt;
&lt;p&gt;If you mean how you need to declare a variable you could use:&lt;br/&gt;
VAR&lt;br/&gt;
     y : INT := 100;&lt;br/&gt;
END_VAR&lt;/p&gt;
&lt;p&gt;Or&lt;/p&gt;
&lt;p&gt;VAR CONSTANT&lt;br/&gt;
     y : INT := 100;&lt;br/&gt;
END_VAR&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>how can I use STATIC VARIABLE in FUNCTION</title><link href="https://forge.codesys.com/forge/talk/Codesys%2520V2.3/thread/a93d6a652c/?limit=25#d0e7" rel="alternate"/><published>2020-02-27T20:37:26.911000Z</published><updated>2020-02-27T20:37:26.911000Z</updated><author><name>sarathbabu</name><uri>https://forge.codesys.com/u/sarathbabu/</uri></author><id>https://forge.codesys.comc5eccfd17b090c0125ce538d0e7a81aa8e820eaa</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;hi,&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;   &lt;span class="n"&gt;how&lt;/span&gt; &lt;span class="k"&gt;to&lt;/span&gt; &lt;span class="k"&gt;declare&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="k"&gt;and&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="k"&gt;not&lt;/span&gt; &lt;span class="n"&gt;recognized&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="n"&gt;an&lt;/span&gt; &lt;span class="nb"&gt;integer&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Thanks &lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>how can I use STATIC VARIABLE in FUNCTION</title><link href="https://forge.codesys.com/forge/talk/Codesys%2520V2.3/thread/a93d6a652c/?limit=25#cf33" rel="alternate"/><published>2020-02-27T20:37:22.896000Z</published><updated>2020-02-27T20:37:22.896000Z</updated><author><name>sarathbabu</name><uri>https://forge.codesys.com/u/sarathbabu/</uri></author><id>https://forge.codesys.comddb13deed8296080eb7ba9a909b575ca9a492eda</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Hi  &lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;  &lt;span class="nv"&gt;That&lt;/span&gt; &lt;span class="nv"&gt;means&lt;/span&gt; &lt;span class="nv"&gt;we&lt;/span&gt; &lt;span class="nv"&gt;can&lt;/span&gt; &lt;span class="nv"&gt;declare&lt;/span&gt; &lt;span class="nv"&gt;as&lt;/span&gt; &lt;span class="nv"&gt;a&lt;/span&gt; &lt;span class="nv"&gt;global&lt;/span&gt; &lt;span class="nv"&gt;variable&lt;/span&gt; &lt;span class="nv"&gt;and&lt;/span&gt; &lt;span class="nv"&gt;use&lt;/span&gt; &lt;span class="nv"&gt;that&lt;/span&gt; &lt;span class="nv"&gt;one&lt;/span&gt; &lt;span class="nv"&gt;in&lt;/span&gt; &lt;span class="nv"&gt;a&lt;/span&gt; &lt;span class="nv"&gt;function&lt;/span&gt;?&lt;span class="nv"&gt;Because&lt;/span&gt; &lt;span class="nv"&gt;function&lt;/span&gt; &lt;span class="nv"&gt;is&lt;/span&gt; &lt;span class="nv"&gt;just&lt;/span&gt; &lt;span class="nv"&gt;a&lt;/span&gt; &lt;span class="nv"&gt;machine&lt;/span&gt; &lt;span class="nv"&gt;which&lt;/span&gt; &lt;span class="nv"&gt;gives&lt;/span&gt; &lt;span class="nv"&gt;output&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="nv"&gt;different&lt;/span&gt; &lt;span class="nv"&gt;recipes&lt;/span&gt; &lt;span class="nv"&gt;differently&lt;/span&gt;. &lt;span class="nv"&gt;of&lt;/span&gt; &lt;span class="nv"&gt;course&lt;/span&gt; &lt;span class="nv"&gt;memory&lt;/span&gt; &lt;span class="nv"&gt;allocation&lt;/span&gt; &lt;span class="nv"&gt;is&lt;/span&gt; &lt;span class="nv"&gt;only&lt;/span&gt; &lt;span class="nv"&gt;once&lt;/span&gt; &lt;span class="nv"&gt;even&lt;/span&gt; &lt;span class="nv"&gt;though&lt;/span&gt; &lt;span class="nv"&gt;reused&lt;/span&gt;.
&lt;/pre&gt;&lt;/div&gt;

&lt;/div&gt;</summary></entry><entry><title>how can I use STATIC VARIABLE in FUNCTION</title><link href="https://forge.codesys.com/forge/talk/Codesys%2520V2.3/thread/a93d6a652c/?limit=25#e4b7" rel="alternate"/><published>2020-02-27T20:37:21.514000Z</published><updated>2020-02-27T20:37:21.514000Z</updated><author><name>timvh</name><uri>https://forge.codesys.com/u/timvh/</uri></author><id>https://forge.codesys.com047b7d51b9c1eed2d664b31ecbfd9d2fdb7050d5</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Maybe good to know is that CoDeSys V3 will support the new variable type VAR_STAT. This feature is an extension to the IEC 61131-3 standard. This can also be used in Functions.&lt;/p&gt;
&lt;p&gt;Your PLC should have the CoDeSys V3 runtime to be able to use this.&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>how can I use STATIC VARIABLE in FUNCTION</title><link href="https://forge.codesys.com/forge/talk/Codesys%2520V2.3/thread/a93d6a652c/?limit=25#86f1" rel="alternate"/><published>2020-02-27T20:37:17.619000Z</published><updated>2020-02-27T20:37:17.619000Z</updated><author><name>Jaycephus</name><uri>https://forge.codesys.com/u/jaycephus/</uri></author><id>https://forge.codesys.comac0bfb495763ec8a8c0c5abc48b841a1b7e430cd</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Yes, I agree that it is a perversion of the intention of 61131 function, the key being that a function should always return the same output with a given input, but if accessing global variables that are essentially static, then the function, possibly by design, will NOT return the same output every time for a given input.&lt;/p&gt;
&lt;p&gt;I might be tempted to use such a function if it actually made the programming clearer and simpler. From reading a version of the 61131 spec, I assumed the above code wasn't even possible until I decided to test it.&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>how can I use STATIC VARIABLE in FUNCTION</title><link href="https://forge.codesys.com/forge/talk/Codesys%2520V2.3/thread/a93d6a652c/?limit=25#dc87" rel="alternate"/><published>2020-02-27T20:37:16.200000Z</published><updated>2020-02-27T20:37:16.200000Z</updated><author><name>spfeif</name><uri>https://forge.codesys.com/u/spfeif/</uri></author><id>https://forge.codesys.com14e3fa6b761d24a8d0539bee57adf752b5e5debe</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;I had forgotten also that the function is confusing to a user. A function is suppose to have action and the same input will yield the same output. If I were the end user what would I expect as the output especially if there are no inputs? How would I be aware of what the function is doing unless I had access to the internal code. What if I made a function like:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="k"&gt;FUNCTION&lt;/span&gt; &lt;span class="k"&gt;ADD&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;WORD&lt;/span&gt;
&lt;span class="k"&gt;ADD&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;What would the end user think when using this function? I would like to ADD something but what, there are no inputs? In your case calling that global function will cause an action that the end user will not even know about and cause all kinds of havoc.&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>how can I use STATIC VARIABLE in FUNCTION</title><link href="https://forge.codesys.com/forge/talk/Codesys%2520V2.3/thread/a93d6a652c/?limit=25#43ab" rel="alternate"/><published>2020-02-27T20:37:14.832000Z</published><updated>2020-02-27T20:37:14.832000Z</updated><author><name>spfeif</name><uri>https://forge.codesys.com/u/spfeif/</uri></author><id>https://forge.codesys.com5bcb162fb945b11dcb2c46844b6113f517254e50</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Interesting but I would considered it poor programming practice. As described above you can not "DECLARE a global variable". You declared global variables else where in Main so it is legal. But now you have a function that is not reusable unless you have the global variables defined in main that are referenced in the function. The only time I see a need to use a global in a function would be a GLOABL CONSTANT. Like MAX_FAULTS = 32 to define an array bound. There is a SIZEOF in CoDeSys but if you are a stickler for pure IEC61131 then sizeof is not a part of the standard.&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>how can I use STATIC VARIABLE in FUNCTION</title><link href="https://forge.codesys.com/forge/talk/Codesys%2520V2.3/thread/a93d6a652c/?limit=25#eff2" rel="alternate"/><published>2020-02-27T20:37:10.977000Z</published><updated>2020-02-27T20:37:10.977000Z</updated><author><name>Jaycephus</name><uri>https://forge.codesys.com/u/jaycephus/</uri></author><id>https://forge.codesys.com69296c9a9ff692fc2daaea97151a94a92616b8cc</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;This may be a perversion of the 'standard', and I don't know if this is a Beckhoff-ONLY implementation or standard across Codesys v2, but you use a global var in a function to achieve a static var capability. (...or cross-function comms or other dirty tricks... )&lt;/p&gt;
&lt;p&gt;I can write one thus:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="nv"&gt;FUNCTION&lt;/span&gt; &lt;span class="nv"&gt;F_TestFunGlobals&lt;/span&gt; : &lt;span class="nv"&gt;BOOL&lt;/span&gt;
&lt;span class="nv"&gt;VAR_INPUT&lt;/span&gt;
&lt;span class="nv"&gt;END_VAR&lt;/span&gt;
&lt;span class="nv"&gt;VAR&lt;/span&gt;
&lt;span class="nv"&gt;END_VAR&lt;/span&gt;
&lt;span class="k"&gt;IF&lt;/span&gt; &lt;span class="nv"&gt;bTest1&lt;/span&gt; &lt;span class="k"&gt;THEN&lt;/span&gt;
   &lt;span class="nv"&gt;iTest3&lt;/span&gt; :&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;iTest3&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="c1"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;ELSE&lt;/span&gt;
   &lt;span class="nv"&gt;iTest4&lt;/span&gt; :&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;iTest4&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="c1"&gt;;&lt;/span&gt;
&lt;span class="nv"&gt;END_IF&lt;/span&gt;&lt;span class="c1"&gt;;&lt;/span&gt;
&lt;span class="nv"&gt;bTest2&lt;/span&gt; :&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;bTest1&lt;/span&gt;&lt;span class="c1"&gt;;&lt;/span&gt;
&lt;span class="nv"&gt;F_TestFunGlobals&lt;/span&gt; :&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;bTest1&lt;/span&gt;&lt;span class="c1"&gt;;&lt;/span&gt;
&lt;span class="nv"&gt;MAIN&lt;/span&gt;.&lt;span class="nv"&gt;bTest&lt;/span&gt; :&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;FALSE&lt;/span&gt;&lt;span class="c1"&gt;;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Every var above is global, declared in the main global var resource, except for var_input MAIN.bTest, which is a bool in MAIN that permits the function to be called from MAIN, and the function itself resets it (another test of to what a function can write).&lt;/p&gt;
&lt;p&gt;So if bTest1 global is true, the function outputs true, bTest2 is set true, and iTest3 is incremented. If bTest1 is false, the function outputs false, bTest2 is set false, iTest4 is incremented. Finally the function resets the bool that is used to conditionally execute the function, resulting in a single run through the function for each setting of MAIN.bTest.&lt;/p&gt;
&lt;p&gt;Is this the path to the dark side of programming, or a useful technique?&lt;/p&gt;
&lt;p&gt;or  &lt;/p&gt;
&lt;p&gt;It reminds me of why I don't like anyone using SET and RESET outputs in ladder.&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>how can I use STATIC VARIABLE in FUNCTION</title><link href="https://forge.codesys.com/forge/talk/Codesys%2520V2.3/thread/a93d6a652c/?limit=25#18e2" rel="alternate"/><published>2020-02-27T20:37:09.604000Z</published><updated>2020-02-27T20:37:09.604000Z</updated><author><name>shooter</name><uri>https://forge.codesys.com/u/shooter/</uri></author><id>https://forge.codesys.com7f6504bc2f06c5a3ee04aa9dd46460b98f0421fc</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;you will have to tell the function what 100 is. i do it normal to make also the statics to be in a variable so hundred:=100:uint;&lt;/p&gt;
&lt;p&gt;for example.&lt;/p&gt;
&lt;p&gt;100 is no type.&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>how can I use STATIC VARIABLE in FUNCTION</title><link href="https://forge.codesys.com/forge/talk/Codesys%2520V2.3/thread/a93d6a652c/?limit=25#43c7" rel="alternate"/><published>2020-02-27T20:37:05.754000Z</published><updated>2020-02-27T20:37:05.754000Z</updated><author><name>spfeif</name><uri>https://forge.codesys.com/u/spfeif/</uri></author><id>https://forge.codesys.comc0c0e0152adfefc83d1304e676845648d3f8583c</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;As a very basic rule of thumb if your POU doesn't rely on saving any prior state internally then use a function, else use a FB.&lt;/p&gt;&lt;/div&gt;</summary></entry></feed>