Recursive Function

A recursive function is a function that calls itself during its execution. The process may repeat several times, outputting the result and the end of each iteration.

The function Count() below uses recursion to count from any number between 1 and 9, to the number 10. For example, Count(1) would return 2,3,4,5,6,7,8,9,10. Count(7) would return 8,9,10. The result could be used as a roundabout way to subtract the number from 10.

function Count (integer N)
    if (N <= 0) return "Must be a Positive Integer";
    if (N > 9) return "Counting Completed";
    else return Count (N+1);
end function

Recursive functions allow programmers to write efficient programs using a minimal amount of code. The downside is that they can cause infinite loops and other unexpected results if not written properly. For example, in the example above, the function is terminated if the number is 0 or less or greater than 9. If proper cases are not included in a recursive function to stop the execution, it will repeat forever, causing the program to crash or become unresponsive.

Updated September 21, 2020 by Per C.

quizTest Your Knowledge

AND, OR, and XOR are types of what?

A
Boolean values
0%
B
String constants
0%
C
Logic gates
0%
D
Data types
0%
Correct! Incorrect!     View the Logic Gate definition.
More Quizzes →

The Tech Terms Computer Dictionary

The definition of Recursive Function on this page is an original definition written by the TechTerms.com team. If you would like to reference this page or cite this definition, please use the green citation links above.

The goal of TechTerms.com is to explain computer terminology in a way that is easy to understand. We strive for simplicity and accuracy with every definition we publish. If you have feedback about this definition or would like to suggest a new technical term, please contact us.

Sign up for the free TechTerms Newsletter

How often would you like to receive an email?

You can unsubscribe or change your frequency setting at any time using the links available in each email.

Questions? Please contact us.