10.2: Recursive Functions v.s. Loop Statements

Introduction to Programming with C++ · LibreTexts

0%

Contents

    Text

    Size
    100%
    Theme
    Typeface
    Line length
    Spacing

    From LibreTexts, under CC BY-NC-SA 4.0, as the source states it; its equations are written in TeX, which the PDF shows typeset. This page at LibreTexts.

    10.2: Recursive Functions v.s. Loop Statements

    In theory, any programming problem that can be solved with a looping statement can be solved with a recursive function and vice versa, because both techniques are recursion in nature. We begin our recursive function study by finding the recursive function solution of a programming problem that we have had a solution with a looping statement.

    Example

    Write a function that receives a positive integer as its input. The function asks a user to enter a set of numbers, and the counts of numbers in the set is based on the integer value the function receives. For example, if the integer the function receives is 10, then the function will ask a user to enter 10 numbers, if the integer value the function receives is 5, then the function will ask a user to enter 5 numbers and so on. Then the function returns the sum of those numbers the user entered.

    First, we provide the solution of this problem by using a looping statement:

    Image 10-2-1.png

    and a sample run of this program has following output:

    Image 10-2-2.png

    Now, let's create a recursive version of this program. Following is the code of the recursive function along with a main() for testing purposes.

    Image 10-2-5.png

    The recursive logic:

    If the integer value the function receives is 1, then the function only asks a user to enter 1 number and return it as the sum.

    If integer value the function receives more than 1, then the function also asks the user to enter 1 number, and call the function itself getSum() letting this function to ask the user to enter the rest of the numbers and return the sum of those numbers. When the function receives the return value of the function call getSum(), which is the sum of the rest of c-1 numbers the user entered, adds the number the user entered in this function, s, to the return value of the function getSum() to produce the sum of all the numbers user entered and return it (Line 15). As we can see, both functions produce the same solution:

    Image 10-2-4.png

    Example

    Write a function that receives a positive integer, say n, then the function displays all the integers from 1 to n.

    Again, we first provide a solution by using a looping statement:

    Image 10-2-6.png

    The output of this program:

    Image 10-2-7.png

    Now, let's create a recursive version of this function:

    Image 10-2-8.png

    The output of this program:

    Image 10-2-9.png

    Both functions provide identical solutions.

    Logic:

    If the integer the function receives is 1 (n==1), then just display this number which is 1.

    If the integer the function receives is greater than 1 (n>1), then call the function itself, getSum(n-1) at Line 9, this function displays the number from 1 to n-1, and then, display the last number, which is n at line 10.


    This page titled 10.2: Recursive Functions v.s. Loop Statements was last modified on Wed, 24 Dec 2025 20:37:23 GMT and is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Jin He.