Recursive Function


Introduction

The recursive function is a unique type of function used in coding. It is defined as the function that uses itself to execute the other terms. This function is generally used to determine the factorial number, palindrome number, power of a number, etc. In this tutorial, we will learn about the basic definition, recursively defined function, formulae for arithmetic, geometric sequences, and some solved examples.

Recursive Definition

The meaning of recursive is repeat or recall itself. In computing science, recurring occurs when a function repeats itself. A recursive function is defined as a code that is used itself to calculate the consecutive terms of a sequence. In other words, the function called itself during the execution is known as a recursive function. The recursive function is also used to simplify a complex problem. Let’s consider a real-life example to understand the definition of a recursive function. Assume Ram has Rs 12000 in cash. He wants to count it. But, it is quite difficult for him to count the money alone. Hence, he asked Gopal and Hari to help in counting the money. They divided the money randomly into three parts and started counting. Ram added all the money when they finished counting and got the sum of Rs 1200. The total money will be exact if it counts alone by Ram. In coding, it is known as recursion. The recursion has the following advantages as

  • Complex problems can be divided into simpler tasks.

  • Recursion saves time and energy by sorting the coding length.

Recursively Defined Functions

Every recursively defined function has two components. One is the first term (i.e., smallest value) of the sequence and denoted as 𝑓(0) 0r 𝑓(1). The second one is the expression to determine the consecutive terms of the sequence and denoted by 𝑓(𝑝).

We will understand this with the help of an example. Let’s consider a sequence 2, 4, 6, 8, and 10.

In the above example, the first term of the sequence $\mathrm{=\:f(1)\:=\:2}$

The expression to determine the consecutive terms of the sequence can be written as

$$\mathrm{f(n)\:=\:f(n\:-\:1)\:+\:2}$$

Now we can evaluate the consecutive terms of the sequence using the above expression as

$$\mathrm{f(2)\:=\:f(1)\:+\:2\:=\:2\:+\:2\:=\:4}$$

$$\mathrm{f(3)\:=\:f(2)\:+\:2\:=\:4\:+\:2\:=\:6}$$

$$\mathrm{f(4)\:=\:f(3)\:+\:2\:=\:6\:+\:2\:=\:8}$$

$$\mathrm{f(5)\:=\:f(4)\:+\:2\:=\:8\:+\:2\:=\:10}$$

What Makes the Function Recursive?

A function is said to be recursive if the previous term is required to find the sequence. That means we need $\mathrm{(n\:-\:1)^{th}}$ and $\mathrm{(n\:-\:2)^{th}}$ terms to find the $\mathrm{n^{th}}$ term of the sequence. The previous terms are necessary to determine whether the given sequence is recursive or not. Generally, the first term and the correlation between the terms of a sequence are provided in a recursive function.

Formula

Assume a sequence given by 𝑝1, 𝑝2, 𝑝3, 𝑝4, . . . . . . . . . , 𝑝𝑛 follows a recursive expression. The recursive formula to determine the $\mathrm{n^{th}}$ term of the sequence can be obtained by

$$\mathrm{P_{n}\:=\:P_{n\:-\:1}\:+\:P_{1}}$$

The above formula is also known as the recursive formula for the arithmetic sequence. Moreover, there is also a formula for the geometric sequence as follows

$$\mathrm{p_{n}\:=\:r\times\:p_{n\:-\:1}\:(Where\:r\:is\:the\:common\:ratio)}$$

Recursive Formula for Arithmetic Sequence

The following steps should be followed to derive the formula for an arithmetic sequence.

  • Step 1 βˆ’ Check whether the sequence is an arithmetic sequence or not. This can be verified by determining the difference values between two consecutive terms. If the subtraction values remain the same, then the sequence can be called an arithmetic sequence.

  • Step 2 βˆ’ Evaluate the common difference of the sequence and let it be h.

  • Step 3 βˆ’ Construct a correlation between the $\mathrm{n^{th}}$ term with its previous term (i.e., $\mathrm{(n\:-\:1^{th}}$ using the common difference. Therefore, the recursive formula for the arithmetic sequence can be obtained as $\mathrm{p_{n}\:=\:p_{n\:-\:1}\:+\:h}$

Recursive Formula for Geometric Sequence

The following steps should be followed to derive the formula for an arithmetic sequence

  • Step 1: Check the sequence whether it is a geometric sequence or not. This can be verified by evaluating the ratio between two consecutive terms. If the values of the ratio remain the same, then the sequence can be called a geometric sequence.

  • Step 2: Evaluate the common ratio of the sequence and let it be r.

  • Step 3: Construct a correlation between the $\mathrm{n^{th}}$ term with its previous term (i.e., $\mathrm{(n\:-\:1^{th}}$ using the common ratio. Therefore, the recursive formula for the geometric sequence can be obtained as $\mathrm{p_{n}\:=\:r\times\:p_{n\:-\:1}}$.

Solved Problems

1)A sequence is given as follows:

$$\mathrm{-\frac{1}{2}\:,\:-\frac{1}{6}\:,\:\frac{1}{6}\:.\:\frac{1}{2}\:,\:\frac{5}{6}\:,\:\frac{7}{6}\:........}$$

Evaluate the sequence formula of the above sequence.

Answer βˆ’

In the first step, we have to check whether the sequence is an arithmetic or geometric sequence.

For arithmetic sequences, there exists a common difference.

Hence,$\mathrm{-\frac{1}{6}\:-\:(\frac{1}{2})\:=\:\frac{1}{3}}$

$$\mathrm{\frac{1}{6}\:-\:(-\frac{1}{6})\:=\:\frac{1}{3}}$$

$$\mathrm{\frac{1}{2}\:-\:(\frac{ }{6})\:=\:\frac{1}{3}}$$

$$\mathrm{\frac{5}{6}\:-\:(\frac{1}{2})\:=\:\frac{1}{3}}$$

Therefore, it is verified that the given sequence is an arithmetic sequence.

The common difference of the sequence $\mathrm{=\:h\:=\:\frac{1}{3}}$

∴The recursive formula for the given function is $\mathrm{p_{n}\:=\:p_{n\:-\:1}\:+\:\frac{1}{3}}$

2)Determine the first 4 terms of the arithmetic sequence $\mathrm{p_{n}\:=\:p_{n\:-\:1}\:-\:7}$ . The first term of the sequence is $\mathrm{p_{1}\:=\:3}$.

Answer βˆ’

It is given that,

The first term of the sequence is $\mathrm{p_{1}\:=\:3}$

Now, using the recursive formula

$$\mathrm{p_{2}\:=\:p_{1}\:-\:7\:=\:3\:-\:7\:=\:-4}$$

$$\mathrm{p_{3}\:p_{2}\:-\:7\:=\:-4\:-\:7\:=\:-11}$$

$$\mathrm{p_{4}\:=\:p_{3}\:-\:7\:=\:-11\:-\:7\:=\:-18}$$

4)A sequence is given as follows

$$\mathrm{\frac{1}{2}\:,\:\frac{1}{3}\:,\frac{2}{9}\:,\:\frac{4}{27}\:,\:\frac{8}{81}\:,\:.......}$$

Evaluate the sequence formula of the above sequence. Also, find the $\mathrm{6^{th}}$ term of the sequence.

Answer βˆ’

In the first step, we have to check whether the sequence is an arithmetic or geometric sequence.

For geometric sequences, there exists a common ratio.

$$\mathrm{\frac{\frac{1}{3}}{\frac{1}{2}}\:=\:\frac{\frac{2}{9}}{\frac{1}{3}}\:=\:\frac{\frac{4}{27}}{\frac{9}{2}}\:=\:\frac{2}{3}}$$

Therefore, it is verified that the given sequence is a geometric sequence.

The common ratio of the sequence $\mathrm{=\:r\:=\:\frac{2}{3}}$

∴The recursive formula for the given function is $\mathrm{p_{n}\:=\:\frac{2}{3}\times\:p_{n\:-\:1}}$

∴ The $\mathrm{6^{th}}$ term of the sequence is $\mathrm{p_{s}\:=\:\frac{2}{3}\times\:p_{s}\:=\:\frac{2}{3}\times\:\frac{8}{81}\:=\:\frac{16}{243}}$

Points to Remember

  • The recursive function always repeats itself to obtain the other terms of the sequence.

  • It is necessary to check whether the sequence is an arithmetic or geometric sequence

  • In an arithmetic sequence, a common difference exists, whereas a common ratio exists in the case of a geometric sequence.

Conclusion

The present tutorial gives a brief introduction about recursive functions. The definition of the recursive function has been briefly described with an example. In addition, the recursive formulae for arithmetic and geometric sequences have been derived in this tutorial. Moreover, some solved examples have been provided for better clarity of this concept. In conclusion, the present tutorial may be useful for understanding the basic concept of the recursive function

FAQs

1. What are the basic properties of recursion functions?

The recursion function has three basic properties such as

  • It recalls itself.

  • It must have a base case.

  • It must change its state and move towards the base case.

2. How many terms should be there in a recursion function?

There is no such limit. A recursion function may have a finite or infinite number of terms.

3. What is the difference between recursion and iteration?

Recursion is the repetition of itself during execution, whereas iteration is the repeated execution of a particular instruction.

4. Can any function be a recursive function?

No. If the function calls itself, it can only be called a recursive function.

5. What are the disadvantages of a recursive function?

The recursive logic is usually harder to follow and occupies more memory during the coding.

Updated on: 08-Apr-2024

8 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements