Introduction
This assignment is to explain a relatively complex term to readers who do not have professional knowledge in the field. The goal is to make a clear and easy explanation for non-professional readers. For the detailed explanation, I will set a situation of explaining the term ‘recursion’ as a software engineer to the relevant departments who are not familiar with software engineering but willing to know about the basic knowledge of the field. As the expected readers do not have technical knowledge in the field, I will try to explain the term as understandable as possible.
Parenthetical Definition
Recursion(calling a function in its own body, so that the function executes multiple times inside itself until it gets to the specified condition) is used for software development when dealing with sequence data. (Merriam-Webster, n.d.)
Sentence Definition
Recursion is a way of solving problems in computer programming, by creating a function that repeats itself inside the function. (University of Utah, n.d.) This way of programming is usually used when a process is repeatedly applied to multiple elements of a single kind of data.
Expanded Definition
History
Recursion is not originated from computer scientists. In the mathematics field, recursion can be found even in ancient ages. One of the most famous mathematicians who used recursion was Leonardo of Pisa, who introduced the Fibonacci sequence. The methodology was adopted in computer science by the initial generation of computer scientists, who were extremely familiar with mathematics. (Stanford University, 2020)
Example
Here I will show an example. This is a function for adding up all the numbers in a given list of numbers.
Figure 1: The Code of Recursive Function ‘addNumbers’
If I run the example function and put a list containing 1, 2, 3, the process is as below:
First, the function with a specific list, [1, 2, 3] in this case, is executed.
Then, the list, [1,2,3], goes into the if conditional statement. The if statement means that if the length of the given list is equal to zero, the function will return zero. Otherwise, the function returns the addition of the first element of the list and the result of addNumbers([2,3]), the rest of the list. In this case, the length of [1,2,3] is 3, not zero, so the function addNumbers([1, 2, 3]) returns 1 + addNumbers([2,3]).
But 1 + addNumbers([2, 3]) is not the answer we wants. We want to get a clear, fixed integer. Therefore, we execute the new addNumbers function, addNumbers([2, 3]) again to make ‘1 + addNumbers([2, 3])’ an integer. Therefore, the function calculates addNumbers([2,3]) again.
In addNumbers([2, 3]), the process is same as the former process. As the length of [2, 3] is 2, not zero, addNumbers([2, 3]) return 2 + addNumbers([3]). It is important to remember that there is also 1 that came from the first execution of addNumbers. Therefore, in total, the answer is now 1+2+addNumbers([3]).
There is still addNumbers function in the answer, so the function does not end here. It calculates addNumbers([3]) again. As the length of [3] is 1, not zero, addNumbers([3]) returns 3+addNumbers(empty list). The answer is now 1+2+3+addNumbers(empty list).
The process goes on until there are no other functions to execute. So the function calculates addNumbers(empty list). But now, the function finally gets the list whose length is zero. Therefore, there are no functions anymore. The answer is now 1+2+3+0. The program can simply add up them and return 6.
In the example, you can see that there is a name of a function and the body, but unlike other usual functions, the name of the function appears again in the function’s body. This is how recursion works. By dividing the given list into two parts, the first element and the rest of the list, you can deal with the first element and do the same operation to the rest of the parts by putting the rest into the same function, just as I put the first element in adding process and the rest of the list in the addNumbers function again.
Analysis of Parts
As you can also see in the example, recursive code can be divided into three parts: base case, operation for the first element, and recursive call. First, you define the base case. It means you need to set when to stop recalling the function. Next, you choose what to do with the first element. In the example above, I added it to the next recursion. You can also try other operations, such as multiplying, adding words, or checking whether it is the element you are looking for. Finally, in the recursive call step, you call the same function with the rest of the list. Then, you can easily find that the recursion works.
Required Conditions
Recursion is a useful methodology, especially when dealing with complex data that contains multiple elements and it is hard to deal with the whole data. By using recursion, you can separate the data into pieces and concentrate only on a small piece. However, to use recursion, you need some conditions. First, of course, the data must be in sequence, such as lists. Otherwise, it should have the next sequences, such as a number that can go to the next sequence by subtracted or added, or words that can go to the next character. Also, if you are willing to make an efficient program, using recursion is not always the best choice. Sometimes, especially when the data is very large, it is not very efficient because calling a function repeatedly consumes larger memory and more time. If there are customers’ complaints that our program is too slow, inappropriate recursion can be one option to check.
Conclusion
In conclusion, recursion, a way of creating a function’s logic by repeating itself, is a powerful way of dealing with data with sequence based on the situation. Since it divides the complex data into parts, it makes the process deal with the elements one at a time, consequently making the process easier. I hope the definition was helpful to you.
Reference
Merriam-Webster. (n.d.). Recursion. The Merriam-Webster.Com Dictionary. Retrieved February 8, 2022, from https://www.merriam-webster.com/dictionary/recursion
Stanford University. (2020, April 23). Recursive Functions (Stanford Encyclopedia of Philosophy). Stanford Encyclopedia of Philosophy. Retrieved January 30, 2022, from https://plato.stanford.edu/entries/recursive-functions/
University of Utah. (n.d.). Programming – Recursion. Jim’s Computer Science Topics Area. Retrieved January 30, 2022, from https://www.cs.utah.edu/%7Egermain/PPS/Topics/recursion.html
Leave a Reply