Recursion and its use for displaying Pascal's triangle

This is a tiny demonstration to show that in Blockly a function can call itself. We use the common example of factorial to show this. See on the following page the use of this function for the display of Pascal's triangle.

The blocks

The blocks

XML Code of Blocks

<xml xmlns="http://www.w3.org/1999/xhtml">
  <block id="*apa,;:.WfaNdiyBQDvK" type="procedures_defreturn" x="47" y="76">
    <mutation>
      <arg name="x"></arg>
    </mutation>
    <field name="NAME">Factorial</field>
    <statement name="STACK">
      <block id="?h.BF/T{J=~L*wr[5w5Q" type="controls_if">
        <mutation else="1"></mutation>
        <value name="IF0">
          <block id="Tu`d:8Y[LoMy6:BuB:m." type="logic_compare">
            <field name="OP">EQ</field>
            <value name="A">
              <block id="Y5/1_./uR=+O}vMJ*e|L" type="variables_get">
                <field name="VAR">x</field>
              </block>
            </value>
            <value name="B">
              <block id="{2KEP5e)Doy-khuG]n?8" type="math_number">
                <field name="NUM">0</field>
              </block>
            </value>
          </block>
        </value>
        <statement name="DO0">
          <block id="i0]8os#B#C#*}n6:[%$o" type="variables_set">
            <field name="VAR">intResult</field>
            <value name="VALUE">
              <block id="gpJDci5MI*;dcUQQlc*]" type="math_number">
                <field name="NUM">1</field>
              </block>
            </value>
          </block>
        </statement>
        <statement name="ELSE">
          <block id="m,ucmM,`@tI~}j?K*7}2" type="variables_set">
            <field name="VAR">intResult</field>
            <value name="VALUE">
              <block id="aMg[2Rt%$.m5Yn94d#^_" type="math_arithmetic">
                <field name="OP">MULTIPLY</field>
                <value name="A">
                  <shadow id="_rh:mkM;g#5S#grP.RB;" type="math_number">
                    <field name="NUM">1</field>
                  </shadow>
                  <block id="MZ.-v2)Dqd~jutCZrlf5" type="variables_get">
                    <field name="VAR">x</field>
                  </block>
                </value>
                <value name="B">
                  <shadow id="J1W~?[T^(BCYrp7mL-4*" type="math_number">
                    <field name="NUM">1</field>
                  </shadow>
                  <block id="hn`Sr5.dCqg!(^]sj~Q{" type="procedures_callreturn">
                    <mutation name="Factorial">
                      <arg name="x"></arg>
                    </mutation>
                    <value name="ARG0">
                      <block id="NVE=3~8ZHUp^y,0oq=Vt" type="math_arithmetic">
                        <field name="OP">MINUS</field>
                        <value name="A">
                          <shadow id="EMEYs4^_:Ctaf@{$90p$" type="math_number">
                            <field name="NUM">1</field>
                          </shadow>
                          <block id=":?p1WQHhyOuq/8Hq@Uez" type="variables_get">
                            <field name="VAR">x</field>
                          </block>
                        </value>
                        <value name="B">
                          <shadow id="Kd;)Y$=s+o7mCf*qOmJQ" type="math_number">
                            <field name="NUM">1</field>
                          </shadow>
                        </value>
                      </block>
                    </value>
                  </block>
                </value>
              </block>
            </value>
          </block>
        </statement>
      </block>
    </statement>
    <value name="RETURN">
      <block id="3PwOVzx;phq5vD,LpK@e" type="variables_get">
        <field name="VAR">intResult</field>
      </block>
    </value>
  </block>
  <block id="{!MzjY=m}GIJXYkT{DyK" type="text_print" x="38" y="313">
    <value name="TEXT">
      <shadow id="2!F/R_n`eX|y3H8G^.5@" type="text">
        <field name="TEXT">abc</field>
      </shadow>
      <block id="@Z#:rdfwCMjy{t^U]12G" type="text_join">
        <mutation items="2"></mutation>
        <value name="ADD0">
          <block id="[acq=.0{HECK@zOu_i8c" type="text">
            <field name="TEXT">5! = </field>
          </block>
        </value>
        <value name="ADD1">
          <block id="n3Edz-F=F^aF4uzfV_u:" type="procedures_callreturn">
            <mutation name="Factorial">
              <arg name="x"></arg>
            </mutation>
            <value name="ARG0">
              <block id="k/jD7]mp=cZl@9[]uN1H" type="math_number">
                <field name="NUM">5</field>
              </block>
            </value>
          </block>
        </value>
      </block>
    </value>
  </block>
</xml>

Generated Smart Pascal Code

var intResult: Integer;


function Factorial(x: Variant): Integer;
begin
  if (x = 0) then
  begin
    intResult := 1;
  end
  else
  begin
    intResult := x * Factorial(x - 1);
  end;
  Result := intResult;
end;


function Str(v: Variant) : string;
begin
  asm
    if (@v === 0) {
      @Result = '0';
    }
    else if (@v === '') {
      @Result = '';
    }
    else if (isNaN(@v)) {
      @Result = @v;
    }
    else {
      @Result = (@v).toString();
    }
  end;
end;



Console.writeln(Str('5! = ') + Str(Factorial(5)));
    

Copy of Output

5! = 120
Programming - a skill for life!

XML, images and generated Pascal code of examples