SuperExamsSuperExams
Search papers…
Menu
DashboardBrowse papersRevision notesBooksSavedRevision packsFlashcardsMy progressAchievementsAI TutorMy classMessages
Back to dashboard

Unlock worked solutions

Step-by-step answers by examiners. From €5/mo.

Try Premium free →
← Computer Science notes
Cambridge A-Level·Computer Science·Cambridge AS & A Level Computer Science

Recursion

13 min read

Defining recursion, base and general cases, the call stack, tracing recursive calls, comparing recursion with iteration, and worked recursive algorithms.

What is recursion?

A recursive subroutine is one that calls itself. Every correct recursion needs:

    a base case — a condition that stops the recursion (no further call), and
    a general (recursive) case — which calls the routine again with a smaller/simpler argument that moves towards the base case.

Without a reachable base case the recursion never stops and causes a stack overflow.

`` FUNCTION Factorial(n) RETURNS INTEGER IF n = 0 THEN // base case RETURN 1 ELSE RETURN n * Factorial(n - 1) // general case ENDIF ENDFUNCTION ``

Viewing only

This content is free to read on superexams.com and cannot be printed or downloaded.

Read the full note, free

Create a free account to read this note in full. Every free account gets 2 complete revision notes, no card needed.

Sign up free →Log in

More Computer Science notes

Information Representation

Data Compression & Encryption

Communication & Networking

Hardware & the Processor