Section 2.2
Programming with recursion
program findFactorial
The program to the right shows a program
which 2
Unit
function factorial (n: integer) : integer
calculates the factorial of a number.
Stack Frame
begin
if n = 1
then result ← 1
else
result ← n * factorial (n-1)
endif
end function
Function calls
Section 2.2: Recursive techniques
n
Calculations
{******MAIN PROGRAM****}
begin
←
Recursion step-by-step
answer
factorial (4)
output answer
end.
Output
AQA Computing A2 © Nelson Thornes 2009
Section 2.2
Programming with recursion
program findFactorial
The program to the right shows a program which
function factorial (n: integer) : integer
calculates the factorial of a number.
Stack Frame
begin
if n = 1
then result ← 1
else
result ← n * factorial (n-1)
endif
end function
Function calls
n
Calculations
{******MAIN PROGRAM****}
begin
answer ← factorial (4)
output answer
end.
Output
AQA Computing A2 © Nelson Thornes 2009
Section 2.2
Programming with recursion
program findFactorial
The program to the right shows a program which
function factorial (n: integer) : integer
calculates the factorial of a number.
Stack Frame
begin
if n = 1
then result ← 1
else
result ← n * factorial (n-1)
endif
end function
Function calls
n
Calculations
{******MAIN PROGRAM****}
begin
answer ← factorial (4)
output answer
end.
Output
AQA Computing A2 © Nelson Thornes 2009
Section 2.2
Programming with recursion
program findFactorial
The program to the right shows a program which
function factorial (n: integer) : integer
calculates the factorial of a number.
Stack Frame
begin
if n = 1
then result ← 1
else
result ← n * factorial (n-1)
endif
end function
Function calls
n
Calculations
In the main program, the function factorial
is called with a value of 4 as a parameter
Output
AQA Computing A2 © Nelson Thornes 2009
{******MAIN PROGRAM****}
begin
answer ← factorial (4)
output answer
end.
Section 2.2
Programming with recursion
program findFactorial
The program to the right shows a program which
function factorial (n: integer) : integer
calculates the factorial of a number.
Stack Frame
Return Address: F422
Parameter: 4
begin
if n = 1
then result ← 1
else
result ← n * factorial (n-1)
endif
end function
Function calls
factorial(4)
n 4
Calculations
The stack frame holds the return address so it knows
where to go back to after executing factorial(4)
Output
AQA Computing A2 © Nelson Thornes 2009
{******MAIN PROGRAM****}
begin
answer ← factorial (4)
output answer
end.
Section 2.2
Programming with recursion
program findFactorial
The program to the right shows a program which
function factorial (n: integer) : integer
calculates the factorial of a number.
Stack Frame
Return Address: F422
Parameter: 4
begin
if n = 1
then result ← 1
else
result ← n * factorial (n-1)
endif
end function
Function calls
factorial(4)
n 4
Calculations
{******MAIN PROGRAM****}
begin
answer ← factorial (4)
output answer
The value of n is compared with 1
end.
Output
AQA Computing A2 © Nelson Thornes 2009
Section 2.2
Programming with recursion
program findFactorial
The program to the right shows a program which
function factorial (n: integer) : integer
calculates the factorial of a number.
Stack Frame
Return Address: F422
Parameter: 4
begin
if n = 1
then result ← 1
else
result ← n * factorial (n-1)
endif
end function
Function calls
factorial(4)
n4
Calculations
A recursive call is made to the factorial
function with n-1.
Output
AQA Computing A2 © Nelson Thornes 2009
{******MAIN PROGRAM****}
begin
answer ← factorial (4)
output answer
end.
Section 2.2
Programming with recursion
program findFactorial
The program to the right shows a program which
function factorial (n: integer) : integer
calculates the factorial of a number.
Stack Frame
Return Address: F543
Parameter: 3
Accumulator: 4
Return Address: F422
Parameter: 4
begin
if n = 1
then result ← 1
else
result ← n * factorial (n-1)
endif
end function
Function calls
factorial(3)
factorial(4)
n3
Calculations
The stack frame holds the return address,
parameter and contents of the accumulator.
Output
AQA Computing A2 © Nelson Thornes 2009
{******MAIN PROGRAM****}
begin
answer ← factorial (4)
output answer
end.
Section 2.2
Programming with recursion
program findFactorial
The program to the right shows a program which
function factorial (n: integer) : integer
calculates the factorial of a number.
Stack Frame
Return Address: F543
Parameter: 3
Accumulator: 4
Return Address: F422
Parameter: 4
begin
if n = 1
then result ← 1
else
result ← n * factorial (n-1)
endif
end function
Function calls
factorial(3)
factorial(4)
n3
Calculations
The function factorial is called with n=3
Output
AQA Computing A2 © Nelson Thornes 2009
{******MAIN PROGRAM****}
begin
answer ← factorial (4)
output answer
end.
Section 2.2
Programming with recursion
program findFactorial
The program to the right shows a program which
function factorial (n: integer) : integer
calculates the factorial of a number.
Stack Frame
Return Address: F543
Parameter: 3
Accumulator: 4
Return Address: F422
Parameter: 4
begin
if n = 1
then result ← 1
else
result ← n * factorial (n-1)
endif
end function
Function calls
factorial(3)
factorial(4)
n 3
Calculations
The value of n is compared with 1
Output
AQA Computing A2 © Nelson Thornes 2009
{******MAIN PROGRAM****}
begin
answer ← factorial (4)
output answer
end.
Section 2.2
Programming with recursion
program findFactorial
The program to the right shows a program which
function factorial (n: integer) : integer
calculates the factorial of a number.
Stack Frame
Return Address: F543
Parameter: 3
Accumulator: 4
Return Address: F422
Parameter: 4
begin
if n = 1
then result ← 1
else
result ← n * factorial (n-1)
endif
end function
Function calls
factorial(3)
factorial(4)
n 3
Calculations
result ← 3 * factorial (2)
Another recursive call is made.
Output
AQA Computing A2 © Nelson Thornes 2009
{******MAIN PROGRAM****}
begin
answer ← factorial (4)
output answer
end.
Section 2.2
Programming with recursion
program findFactorial
The program to the right shows a program which
function factorial (n: integer) : integer
calculates the factorial of a number.
Stack Frame
Return Address: F543
Parameter: 2
Accumulator: 3
Return Address: F543
Parameter: 3
Accumulator: 4
Return Address: F422
Parameter: 4
begin
if n = 1
then result ← 1
else
result ← n * factorial (n-1)
endif
end function
Function calls
factorial(2)
factorial(3)
factorial(4)
n 2
Calculations
The stack frame holds the return address,
parameter and contents of the accumulator.
Output
AQA Computing A2 © Nelson Thornes 2009
{******MAIN PROGRAM****}
begin
answer ← factorial (4)
output answer
end.
Section 2.2
Programming with recursion
program findFactorial
The program to the right shows a program which
function factorial (n: integer) : integer
calculates the factorial of a number.
Stack Frame
Return Address: F543
Parameter: 2
Accumulator: 3
Return Address: F543
Parameter: 3
Accumulator: 4
Return Address: F422
Parameter: 4
begin
if n = 1
then result ← 1
else
result ← n * factorial (n-1)
endif
end function
Function calls
factorial(2)
factorial(3)
factorial(4)
n 2
Calculations
The function factorial is called with n=2
Output
AQA Computing A2 © Nelson Thornes 2009
{******MAIN PROGRAM****}
begin
answer ← factorial (4)
output answer
end.
Section 2.2
Programming with recursion
program findFactorial
The program to the right shows a program which
function factorial (n: integer) : integer
calculates the factorial of a number.
Stack Frame
Return Address: F543
Parameter: 2
Accumulator: 3
Return Address: F543
Parameter: 3
Accumulator: 4
Return Address: F422
Parameter: 4
begin
if n = 1
then result ← 1
else
result ← n * factorial (n-1)
endif
end function
Function calls
factorial(2)
factorial(3)
factorial(4)
n 1
Calculations
The value of n is compared with 1
Output
AQA Computing A2 © Nelson Thornes 2009
{******MAIN PROGRAM****}
begin
answer ← factorial (4)
output answer
end.
Section 2.2
Programming with recursion
program findFactorial
The program to the right shows a program which
function factorial (n: integer) : integer
calculates the factorial of a number.
Stack Frame
Return Address: F543
Parameter: 2
Accumulator: 3
Return Address: F543
Parameter: 3
Accumulator: 4
Return Address: F422
Parameter: 4
begin
if n = 1
then result ← 1
else
result ← n * factorial (n-1)
endif
end function
Function calls
factorial(2)
factorial(3)
factorial(4)
n 2
Calculations
result ← 2 * factorial (1)
Another recursive call is made.
Output
AQA Computing A2 © Nelson Thornes 2009
{******MAIN PROGRAM****}
begin
answer ← factorial (4)
output answer
end.
Section 2.2
Programming with recursion
program findFactorial
The program to the right shows a program which
function factorial (n: integer) : integer
calculates the factorial of a number.
Stack Frame
Return Address: F543
Parameter: 1
Accumulator: 2
Return Address: F543
Parameter: 2
Accumulator: 3
Return Address: F543
Parameter: 3
Accumulator: 4
Return Address: F422
Parameter: 4
Function calls
factorial(1)
factorial(2)
factorial(3)
factorial(4)
Calculations
The stack frame holds the return address,
parameter and contents of the accumulator.
Output
AQA Computing A2 © Nelson Thornes 2009
begin
if n = 1
then result ← 1
else
result ← n * factorial (n-1)
endif
end function
n 1
{******MAIN PROGRAM****}
begin
answer ← factorial (4)
output answer
end.
Section 2.2
Programming with recursion
program findFactorial
The program to the right shows a program which
function factorial (n: integer) : integer
calculates the factorial of a number.
Stack Frame
Return Address: F543
Parameter: 1
Accumulator: 2
Return Address: F543
Parameter: 2
Accumulator: 3
Return Address: F543
Parameter: 3
Accumulator: 4
Return Address: F422
Parameter: 4
Function calls
factorial(1)
factorial(2)
factorial(3)
factorial(4)
Calculations
The function factorial is called with n=1
Output
AQA Computing A2 © Nelson Thornes 2009
begin
if n = 1
then result ← 1
else
result ← n * factorial (n-1)
endif
end function
n 1
{******MAIN PROGRAM****}
begin
answer ← factorial (4)
output answer
end.
Section 2.2
Programming with recursion
program findFactorial
The program to the right shows a program which
function factorial (n: integer) : integer
calculates the factorial of a number.
Stack Frame
Return Address: F543
Parameter: 1
Accumulator: 2
Return Address: F543
Parameter: 2
Accumulator: 3
Return Address: F543
Parameter: 3
Accumulator: 4
Return Address: F422
Parameter: 4
begin
if n = 1
then result ← 1
else
result ← n * factorial (n-1)
endif
end function
Function calls
factorial(1)
factorial(2)
factorial(3)
factorial(4)
n 1
Calculations
The value of n is compared with 1.
Now n =1. This is known as the base case.
Output
AQA Computing A2 © Nelson Thornes 2009
{******MAIN PROGRAM****}
begin
answer ← factorial (4)
output answer
end.
Section 2.2
Programming with recursion
program findFactorial
The program to the right shows a program which
function factorial (n: integer) : integer
calculates the factorial of a number.
Stack Frame
Return Address: F543
Parameter: 1
Accumulator: 2
Return Address: F543
Parameter: 2
Accumulator: 3
Return Address: F543
Parameter: 3
Accumulator: 4
Return Address: F422
Parameter: 4
Calculations
Function calls
factorial(1)
factorial(2)
factorial(3)
factorial(4)
result ←1
factorial(1) returns a value of 1
Output
AQA Computing A2 © Nelson Thornes 2009
begin
if n = 1
then result ← 1
else
result ← n * factorial (n-1)
endif
end function
n1
{******MAIN PROGRAM****}
begin
answer ← factorial (4)
output answer
end.
Section 2.2
Programming with recursion
program findFactorial
The program to the right shows a program which
function factorial (n: integer) : integer
calculates the factorial of a number.
Stack Frame
Return Address: F5432
Parameter: 2
Accumulator: 3
Return Address: F543
Parameter: 3
Accumulator: 4
Return Address: F422
Parameter: 4
begin
if n = 1
then result ← 1
else
result ← n * factorial (n-1)
endif
end function
Function calls
factorial(3)
factorial(4)
n 1
Calculations
result ←1
Control passes to the function that called factorial(1).
The values needed are taken from the stack frame.
Output
AQA Computing A2 © Nelson Thornes 2009
{******MAIN PROGRAM****}
begin
answer ← factorial (4)
output answer
end.
Section 2.2
Programming with recursion
program findFactorial
The program to the right shows a program which
function factorial (n: integer) : integer
calculates the factorial of a number.
Stack Frame
Return Address: F5432
Parameter: 2
Accumulator: 3
Return Address: F543
Parameter: 3
Accumulator: 4
Return Address: F422
Parameter: 4
begin
if n = 1
then result ← 1
else
result ← n * factorial (n-1)
endif
end function
Function calls
factorial(3)
factorial(4)
n 2
Calculations
result ← 2 * 1
The function that called it is factorial(2)
Output
AQA Computing A2 © Nelson Thornes 2009
{******MAIN PROGRAM****}
begin
answer ← factorial (4)
output answer
end.
Section 2.2
Programming with recursion
program findFactorial
The program to the right shows a program which
function factorial (n: integer) : integer
calculates the factorial of a number.
Stack Frame
Return Address: F5432
Parameter: 2
Accumulator: 3
Return Address: F543
Parameter: 3
Accumulator: 4
Return Address: F422
Parameter: 4
begin
if n = 1
then result ← 1
else
result ← n * factorial (n-1)
endif
end function
Function calls
factorial(3)
factorial(4)
n2
Calculations
result ← 2 * 1
factorial(2) can now return a value and end.
Output
AQA Computing A2 © Nelson Thornes 2009
{******MAIN PROGRAM****}
begin
answer ← factorial (4)
output answer
end.
Section 2.2
Programming with recursion
program findFactorial
The program to the right shows a program which
function factorial (n: integer) : integer
calculates the factorial of a number.
Stack Frame
Return Address: F543
Parameter: 3
Accumulator: 4
Return Address: F422
Parameter: 4
begin
if n = 1
then result ← 1
else
result ← n * factorial (n-1)
endif
end function
Function calls
factorial(4)
n 3
Calculations
result ← 3 * 2
Control passes to the function that called factorial(2) which is
factorial(3). The values needed are taken from the stack frame.
Output
AQA Computing A2 © Nelson Thornes 2009
{******MAIN PROGRAM****}
begin
answer ← factorial (4)
output answer
end.
Section 2.2
Programming with recursion
program findFactorial
The program to the right shows a program which
function factorial (n: integer) : integer
calculates the factorial of a number.
Stack Frame
Return Address: F543
Parameter: 3
Accumulator: 4
Return Address: F422
Parameter: 4
begin
if n = 1
then result ← 1
else
result ← n * factorial (n-1)
endif
end function
Function calls
factorial(4)
n 3
Calculations
result ← 3 * 2
factorial(3) can now return a value and end.
Output
AQA Computing A2 © Nelson Thornes 2009
{******MAIN PROGRAM****}
begin
answer ← factorial (4)
output answer
end.
Section 2.2
Programming with recursion
program findFactorial
The program to the right shows a program which
function factorial (n: integer) : integer
calculates the factorial of a number.
Stack Frame
Return Address: F422
Parameter: 4
begin
if n = 1
then result ← 1
else
result ← n * factorial (n-1)
endif
end function
Function calls
factorial(4)
n 4
Calculations
result ← 4 * 6
Control passes to the function that called factorial(3) which is
factorial(4). The values needed are taken from the stack frame.
Output
AQA Computing A2 © Nelson Thornes 2009
{******MAIN PROGRAM****}
begin
answer ← factorial (4)
output answer
end.
Section 2.2
Programming with recursion
program findFactorial
The program to the right shows a program which
function factorial (n: integer) : integer
calculates the factorial of a number.
Stack Frame
Return Address: F422
Parameter: 4
begin
if n = 1
then result ← 1
else
result ← n * factorial (n-1)
endif
end function
Function calls
n 4
Calculations
result ← 4 * 6
factorial(4) can now return a value and end.
Output
AQA Computing A2 © Nelson Thornes 2009
{******MAIN PROGRAM****}
begin
answer ← factorial (4)
output answer
end.
Section 2.2
Programming with recursion
program findFactorial
The program to the right shows a program which
function factorial (n: integer) : integer
calculates the factorial of a number.
Stack Frame
begin
if n = 1
then result ← 1
else
result ← n * factorial (n-1)
endif
end function
Function calls
n
Calculations
result ← 4 * 6
Control passes back to the main program. The
values needed are taken from the stack frame.
Output
AQA Computing A2 © Nelson Thornes 2009
{******MAIN PROGRAM****}
begin
answer ← factorial (4)
output answer
end.
Section 2.2
Programming with recursion
program findFactorial
The program to the right shows a program which
function factorial (n: integer) : integer
calculates the factorial of a number.
Stack Frame
begin
if n = 1
then result ← 1
else
result ← n * factorial (n-1)
endif
end function
Function calls
n 4
Calculations
factorial(4) ← 24
The answer is output
Output 24
AQA Computing A2 © Nelson Thornes 2009
{******MAIN PROGRAM****}
begin
answer ← factorial (4)
output answer
end.
© Copyright 2026 Paperzz