# Recursion and its use for calculating factorials

[XML] [Smart Pascal] [JavaScript] [Python] [output]

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. The Blockly Playground generated the JavaScript and Python code from the same blocks. See on the following page the use of this function for the display of Pascal's triangle.

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">
<field name="NUM">1</field>
<block id="MZ.-v2)Dqd~jutCZrlf5" type="variables_get">
<field name="VAR">x</field>
</block>
</value>
<value name="B">
<field name="NUM">1</field>
<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">
<field name="NUM">1</field>
<block id=":?p1WQHhyOuq/8Hq@Uez" type="variables_get">
<field name="VAR">x</field>
</block>
</value>
<value name="B">
<field name="NUM">1</field>
</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">
<field name="TEXT">abc</field>
<block id="@Z#:rdfwCMjy{t^U]12G" type="text_join">
<mutation items="2"></mutation>
<block id="[acq=.0{HECK@zOu_i8c" type="text">
<field name="TEXT">5! = </field>
</block>
</value>
<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)));

```

## Generated JavaScript

```var intResult, x;

function Factorial(x) {
if (x == 0) {
intResult = 1;
} else {
intResult = x * Factorial(x - 1);
}
return intResult;
}

```

## Generated Python Code

```intResult = None
x = None

def Factorial(x):
global intResult
if x == 0:
intResult = 1
else:
intResult = x * Factorial(x - 1)
return intResult

print(str('5! = ') + str(Factorial(5)))

```

## Copy of Output

`5! = 120`
Programming - a skill for life!

XML, images and generated Smart Pascal code of examples including loops, arrays, validation and recursion