<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
  <channel>
    <title>'pascal functions' Thread RSS Feed</title>
    <link>http://www.programmersheaven.com/</link>
    <description>Contains the latest posts from the thread 'pascal functions' posted on the 'Pascal' forum at Programmer's Heaven.</description>
    <language>en</language>
    <copyright>Copyright 2013 Programmers Heaven</copyright>
    <pubDate>Sat, 25 May 2013 15:28:22 -0700</pubDate>
    <lastBuildDate>Sat, 25 May 2013 15:28:22 -0700</lastBuildDate>
    <generator>Argotic Syndication Framework 2007.3.0.1, http://www.codeplex.com/Argotic</generator>
    <docs>http://www.rssboard.org/rss-specification</docs>
    <ttl>360</ttl>
    <image>
      <url>http://www.programmersheaven.com/images/ph.gif</url>
      <title>Programmers Heaven</title>
      <link>http://www.programmersheaven.com/</link>
      <width>88</width>
      <height>31</height>
    </image>
    <item>
      <title>pascal functions</title>
      <link>http://www.programmersheaven.com/mb/pasprog/426698/426698/pascal-functions/</link>
      <description>&lt;br /&gt;
1 function fun(x:Integer):Integer;&lt;br /&gt;
2  begin&lt;br /&gt;
3  if x &amp;gt; 100 then&lt;br /&gt;
4  fun:=x-10&lt;br /&gt;
5  else&lt;br /&gt;
6  fun:=fun(fun x+11)&lt;br /&gt;
7   end;&lt;br /&gt;
&lt;br /&gt;
hey guys am pretty new to pascal. Can you please explain me what line 6 does.. and if it is wrong then please correct me&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/pasprog/426698/426698/pascal-functions/</guid>
      <pubDate>Wed, 04 Jan 2012 09:17:51 -0700</pubDate>
      <category>Pascal</category>
    </item>
    <item>
      <title>Re: pascal functions</title>
      <link>http://www.programmersheaven.com/mb/pasprog/426698/426699/re-pascal-functions/#426699</link>
      <description>It's wrong.  The compiler is going to expect a left parenthesis after the identifier "fun."  I'm guessing that it should be as I've changed it below.&lt;br /&gt;
&lt;br /&gt;
In line 6 the function calls itself recursively.  If passed a number &amp;lt;= 100 it will repeatedly increase that number by 11 until it is &amp;gt; 100, then subtract 10 and return. &lt;br /&gt;
&lt;pre class="sourcecode"&gt;
function fun(x : Integer) : Integer ;
begin
     if x &amp;gt; 100 then
          fun := x - 10
     else
          &lt;span style="color: Red;"&gt;fun := fun(x + 11)&lt;/span&gt;
end ;
&lt;/pre&gt;&lt;br /&gt;
For example, if passed 88 it will call itself and return 99.  That's still &amp;lt;= 100 so it calls itself again and returns 100.  That's &amp;gt; 100 so on the final call to itself it subtracts 10 and returns 100.&lt;br /&gt;
&lt;br /&gt;
The intended code may be:&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
          &lt;span style="color: Red;"&gt;fun := fun(fun(x + 11))&lt;/span&gt;
&lt;/pre&gt;&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/pasprog/426698/426699/re-pascal-functions/#426699</guid>
      <pubDate>Wed, 04 Jan 2012 09:42:21 -0700</pubDate>
      <category>Pascal</category>
    </item>
    <item>
      <title>Re: pascal functions</title>
      <link>http://www.programmersheaven.com/mb/pasprog/426698/426715/re-pascal-functions/#426715</link>
      <description>hey thank you very much&lt;br /&gt;
&lt;br /&gt;
i think the line 6 is&lt;br /&gt;
&lt;br /&gt;
fun:=fun(fun(x+11));&lt;br /&gt;
&lt;br /&gt;
So i still did not understand how that line executes. if supose the value of x is 95. can you please explain&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/pasprog/426698/426715/re-pascal-functions/#426715</guid>
      <pubDate>Thu, 05 Jan 2012 08:36:05 -0700</pubDate>
      <category>Pascal</category>
    </item>
    <item>
      <title>Re: pascal functions</title>
      <link>http://www.programmersheaven.com/mb/pasprog/426698/426723/re-pascal-functions/#426723</link>
      <description>: hey thank you very much&lt;br /&gt;
: &lt;br /&gt;
: i think the line 6 is&lt;br /&gt;
: &lt;br /&gt;
: fun:=fun(fun(x+11));&lt;br /&gt;
: &lt;br /&gt;
: So i still did not understand how that line executes. if supose the &lt;br /&gt;
: value of x is 95. can you please explain&lt;br /&gt;
: &lt;br /&gt;
&lt;pre class="sourcecode"&gt;
function fun(x : Integer) : Integer ;
begin
     if x &amp;gt; 100 then
          fun := x - 10
     else
          &lt;span style="color: Red;"&gt;fun := fun(fun(x + 11)) {line 6}&lt;/span&gt;
end ;
&lt;/pre&gt;&lt;br /&gt;
The important thing to grasp about line 6 is that the function calls itself, a process called &lt;strong&gt;recursion.&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
If x = 95 the function jumps to the "else" part (line 6) where it calls itself twice, a double recursion.  Call these recursions the outer and inner calls.&lt;br /&gt;
&lt;br /&gt;
The inner call is passed x + 11 = 106 which is &amp;gt; 100, so it goes to the "then" part and returns 106 - 10 = 96, which becomes the argument of the outer call.  Since 96 &amp;lt;= 100 the function again winds up at the double recursion, line 6.&lt;br /&gt;
&lt;br /&gt;
The code is tricky.  It's simple enough if x &amp;gt;100 but, if not, then it appears to go into recursion, increasing x by one each time until x = 101.  It then subtracts 10 and ends by returning 91.&lt;br /&gt;
&lt;br /&gt;
As you can see, recursion is actually a form of looping. In some cases (not this one) it can make code simpler.  Its down side is that it tends to gobble up memory.  For each level of recursion the function makes another copy of itself.  In this example the program makes two additional copies of of "fun" each time through the loop.  A low enough value of x, like - 32000, would probably consume all memory and the program would crash. &lt;br /&gt;
&lt;br /&gt;
Since this function is named "fun" I assume that it's supposed to be a puzzler, but it's not the sort of thing that you'd want to put in a real program.&lt;br /&gt;
&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/pasprog/426698/426723/re-pascal-functions/#426723</guid>
      <pubDate>Thu, 05 Jan 2012 18:20:54 -0700</pubDate>
      <category>Pascal</category>
    </item>
  </channel>
</rss>