Recursive Function in Program
recursive Recursive Function in Program Recursive Function is a function that calls itself. A function a1() is also recursive if it calls a function a2(), which under some circumstances calls a1(), creating a cycle in the sequence of calls. This kind of ability to invoke itself allowing a recursive function to be repeated with any different parameter values. Mostly, recursive function is less efficient than iteration (looping) due to the overhead for the extra function calls. However, by using recursion method allowing us to specify a very natural, simple solution to a problem that would be very difficult to solve if using another method. so that’s why recursion is a powerfull method in problem solving and programming.
Using Recursive Function for Problem Solving
Problems that need a recursive solution mostly have the following characterstics :
- One or More simple problem (a problem that can be solved using simple method) have a straight forward, non recursive solution.
- The problem can be redefined into a simple problem that can be solved easily.
- By applying this redefinition process every time recursive function called, the problem eventually reduced entirely to a simple cases.
You can see the recursive algorithm that we will use generally from example below
1234If problemvar = simple case can be solvedsolve itelseredefine problemvar using recursion
That example illustrate how recursion works generally. Mostly recursive method used when you want to splitting a problem into a simpler problem.
Using recursion to splitting a problem into smaller problems
Another example of recursion, let’s consider how we will solve the problem of multiplying 12 by 2, assuming we only know addition tables not multiplication tables. Because we assume don’t know multiplication tables. Multiplying 12 by 2 straight forward can’t be solved. So we will split the problem into many problems we can solve. In this case, by using C programming language.
123456789int multiply(int a, int b){int output;if (b == 1)output = a;elseoutput = a + multiply (a, b-1); /* Recursive step by calling it's own function*/return (output);}
From example above we splitting the original problem into two simpler problems. The first problem solved by calling multiply function again (multiply (a, b-1)). After that we check if the new second problem is greater than 1. The program will call multiply function again till b = 1.
You May Want to See :
- How to Test and Debug A Program How to Test and Debug A Program
- String Functions PHP String Functions PHP
- User Defined Functions in MySQL User Defined Functions in MySQL
- Handling with Objects and Classes in PHP Handling with Objects and Classes in PHP
- How to program ghost notes for your MIDI drum tracks How to program ghost notes for your MIDI drum tracks
- Hardening MySQL Security Server Hardening MySQL Security Server
- Improve Security With VERIS Framework Improve Security With VERIS Framework
- Basic Guide of Interprocess Communication and Pipes Basic Guide of Interprocess Communication and Pipes
- 3 Common Programming Errors 3 Common Programming Errors
- R vs Python, Which one is better R vs Python, Which one is better
- Parallel Programming with Multiprocess and Threads Parallel Programming with Multiprocess and Threads
- Windows 8 Best Features Windows 8 Best Features
- How Botnets Infiltrate & Exploit Computer Systems How Botnets Infiltrate & Exploit Computer Systems
- Connecting Ruby to Java Programming Connecting Ruby to Java Programming
- Understanding the basics of HTML Understanding the basics of HTML
- What is CSS and What CSS can do ? What is CSS and What CSS can do ?
- How To Use Cloud-Based Storage Wisely How To Use Cloud-Based Storage Wisely
- What You Should Do If Computer Crash What You Should Do If Computer Crash
- Tools You Need For Virtualisation Tools You Need For Virtualisation
- Reading Files Without Filehandle PHP Reading Files Without Filehandle PHP
- How To Create a Simple Search Engine How To Create a Simple Search Engine
This site uses Akismet to reduce spam. Learn how your comment data is processed.