<?xml version="1.0" encoding="utf-8"?>
<feed xml:lang="en" xmlns="http://www.w3.org/2005/Atom"><title>Recent posts to Reentrant function</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/1e83997eaf/" rel="alternate"/><link href="https://forge.codesys.com/forge/talk/Engineering/thread/1e83997eaf/feed.atom" rel="self"/><id>https://forge.codesys.com/forge/talk/Engineering/thread/1e83997eaf/</id><updated>2020-02-27T13:48:59.046000Z</updated><subtitle>Recent posts to Reentrant function</subtitle><entry><title>Reentrant function</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/1e83997eaf/?limit=25#fdf2" rel="alternate"/><published>2020-02-27T13:48:59.046000Z</published><updated>2020-02-27T13:48:59.046000Z</updated><author><name>Anonymous</name><uri>https://forge.codesys.com/u/*anonymous/</uri></author><id>https://forge.codesys.com3e089b1dd688e061ac41e4631376c5a7b9f1ae3e</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;&lt;em&gt;Originally created by:&lt;/em&gt; scott_cunningham&lt;/p&gt;
&lt;p&gt;One word of caution - Recursion can spell trouble for stack size and other "behind the scenes" mechanisms.  If you are dealing with Machine controllers and time sensitive field uses where their number one job is to not crash the whole machine, recursive solutions can be "risky" (just like for/next loops).&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>Reentrant function</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/1e83997eaf/?limit=25#b711" rel="alternate"/><published>2020-02-27T13:48:58.917000Z</published><updated>2020-02-27T13:48:58.917000Z</updated><author><name>josepmariarams</name><uri>https://forge.codesys.com/u/josepmariarams/</uri></author><id>https://forge.codesys.com8a75d956e7effa2cb999cffa9817f708a327b816</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Thanks Joan.&lt;/p&gt;
&lt;p&gt;I had not thought on this possibility, use an Fb. I will have it in mind.&lt;/p&gt;
&lt;p&gt;At the moment I had solved my problem with an for loop. But sometimes recursion is the most elegant solution.&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>Reentrant function</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/1e83997eaf/?limit=25#9ac7" rel="alternate"/><published>2020-02-27T13:48:58.788000Z</published><updated>2020-02-27T13:48:58.788000Z</updated><author><name>Joan M</name><uri>https://forge.codesys.com/u/joan-m/</uri></author><id>https://forge.codesys.comda135554ebd646861ca9aeed19a26f33d28f5a09</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Hi Josep!&lt;/p&gt;
&lt;p&gt;It looks like you can't do that with a function...   &lt;/p&gt;
&lt;p&gt;Recursion is not handled gracefully by Codesys.&lt;/p&gt;
&lt;p&gt;The closest option here is to use a function block as:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="nv"&gt;FUNCTION_BLOCK&lt;/span&gt; &lt;span class="nv"&gt;fb_Recursiu&lt;/span&gt;
&lt;span class="nv"&gt;VAR_INPUT&lt;/span&gt;
   &lt;span class="nv"&gt;i&lt;/span&gt; : &lt;span class="nv"&gt;INT&lt;/span&gt; :&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="c1"&gt;;&lt;/span&gt;
&lt;span class="nv"&gt;END_VAR&lt;/span&gt;
&lt;span class="nv"&gt;VAR_OUTPUT&lt;/span&gt;
   &lt;span class="nv"&gt;iResult&lt;/span&gt; : &lt;span class="nv"&gt;INT&lt;/span&gt; :&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="c1"&gt;;&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="o"&gt;-----------------------------------------------&lt;/span&gt;
&lt;span class="k"&gt;IF&lt;/span&gt; &lt;span class="ss"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="ss"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;THEN&lt;/span&gt;
   &lt;span class="nv"&gt;i&lt;/span&gt; :&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&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;i&lt;/span&gt; :&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;i&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;THIS&lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="ss"&gt;()&lt;/span&gt;&lt;span class="c1"&gt;;&lt;/span&gt;
&lt;span class="nv"&gt;END_IF&lt;/span&gt;
&lt;span class="nv"&gt;iResult&lt;/span&gt; :&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;i&lt;/span&gt;&lt;span class="c1"&gt;;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;And call it like this:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="n"&gt;fbRecursiu&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;iStart&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;iResult&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;iResult&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;That could be solved the day functions can be pointed by pointers... or when recursion worked as expected...&lt;/p&gt;
&lt;p&gt;Hope this helps...&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>Reentrant function</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/1e83997eaf/?limit=25#5ce6" rel="alternate"/><published>2020-02-27T13:48:58.697000Z</published><updated>2020-02-27T13:48:58.697000Z</updated><author><name>josepmariarams</name><uri>https://forge.codesys.com/u/josepmariarams/</uri></author><id>https://forge.codesys.comc897d01023ac6763c5f83dc68c2baba669843e68</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Hi all.&lt;/p&gt;
&lt;p&gt;Do you know if it is possible to call a function from himself in st.&lt;/p&gt;
&lt;p&gt;If yes, how. &lt;/p&gt;
&lt;p&gt;If I do:&lt;/p&gt;
&lt;p&gt;Funtion factorial:lreal&lt;br/&gt;
Var_input&lt;br/&gt;
   Value:lreal&lt;br/&gt;
End_var&lt;/p&gt;
&lt;p&gt;If(value&amp;lt;2) then&lt;br/&gt;
  Factorial:=1&lt;br/&gt;
Else&lt;br/&gt;
  Factorial:=value*factorial(value-1)&lt;br/&gt;
End_if&lt;/p&gt;
&lt;p&gt;Returns an error&lt;/p&gt;&lt;/div&gt;</summary></entry></feed>