## Recursion

p549-567(skip 558, 559)

### Recursive void fucntion

• In C++ a function definition may contain a call to the function being defined. In such cases the function is said to be recursive

Example
 ```#includeusing namespace std;void print(int x);int main() { print(3); return 0;}void print(int x) { if(x <= 0) { // base case cout << "Stop at x = 0\n"; } else { cout << "Calling print(" << x << ")\n"; print(x-1); // call the function again } } ```

Output
 `Calling print(3)Calling print(2)Calling print(1)Stop at x = 0`
Explanation

How recursion works
• When a function is called, the computer plugs in the arguments for the parameters and begin to execute the code.
• If it should encounter a recursive call, it temporarily stops its computation because it must know the result of the recursive call before it can proceed.
• It saves all the information it needs to continue the computation later on, and proceeds to evaluate the recursive call.
• When the recursive call is completed, the computer returns to finish the outer computation.
How recursion ends
• the function must be designed so that any call of the function must ultimately terminate with some piece of code that does not depend on recursion.
• The general outline of a successful recursive function definition is
• One or more cases in which the function accomplishes its task by using one or more recursive calls to accomplish one or more smaller version of the task
• One or more cases in which the function accomplishes its task without using any recursive calls. These cases without any recursive calls are called base cases or stopping cases.
• The most common way to ensure that a stopping case is eventually reached is to write the function so that some (positive) numeric quantity is decreased on each recursive call and to provide a stopping case for some "small" value.

What is the output of the following function
 `#includeusing namespace std;void print(int x);int main() { print(3); return 0;}void print(int x) { cout << "Calling print(" << x << ")\n"; print(x-1); // call the function again}`
What is wrong about the function?

Another example
 ```#includeusing namespace std;// print the digits of the number n verticallyvoid writeVertical(int n);int main() { writeVertical(2593); return 0;}void writeVertical(int n) { if(n < 10) { // base case cout << n << endl; } else { // n is two or more digits long writeVertical(n/10); cout << (n%10) << endl; } } ```

Output
 `2593`

### Recursive functions that return a value

• A recursive function can return a value of any type.

Example
 ```#include#include // for exitusing namespace std;// return x to the power nint power(int x, int n); int main() { cout << power(5,3) << endl; return 0; } int power(int x, int n) { if(n < 0) { cout << "Illegal argument to power.\n"; exit(1); } if(n > 0) { return (power(x,n-1)*x); } else { // n == 0, base case return 1; } } ```

### Binary Search

• Search if a key (a number) is in an array. If it is, find out the location.
Easiest method: search through every single element in the array.

Example
 ```#includeusing namespace std; // size: size of the array // key : the number to be searched // found : true if the key is found, false otherwise // location: location of the key in the array if the key is found void search(const int a[], int size, int key, bool& found, int& location); int main() { int a[] = {2,3,5,7,11,13,19,23,29,31,37,41,43,47}; int size = 14; int key, location; bool found; cout << "Enter a number to be located: "; cin >> key; search(a, 14, key, found, location); if(found) { cout << key << " is in index location " << location << endl; } else { cout << key << " is not in the array." << endl; } return 0; } void search(const int a[], int size, int key, bool& found, int& location) { found = false; for(int i = 0; i < size; i++) { if(key == a[i]) { found = true; location = i; return; } } } ```

Output
 `Enter a number to be located: 3232 is not in the array.`

Output
 `Enter a number to be located: 3131 is in index location 9`
• The above method is not very efficient. If you have 100 numbers in the array, it may take 100 comparisons to find the location of the key.
• A more efficient way is called binary search
 ```#includeusing namespace std; // find the key from a[first] to a[last] // a[] : array, in ascending order // first: index // last: index // key : the number to be searched // found : true if the key is found, false otherwise // location: location of the key in the array if the key is found void search(const int a[], int first, int last, int key, bool& found, int& location); int main() { int a[] = {2,3,5,7,11,13,19,23,29,31,37,41,43,47}; int size = 14; int key, location; bool found; cout << "Enter a number to be located: "; cin >> key; search(a, 0, 13, key, found, location); if(found) { cout << key << " is in index location " << location << endl; } else { cout << key << " is not in the array." << endl; } return 0; } void search(const int a[], int first, int last, int key, bool& found, int& location) { int mid; if(first > last) { found = false; } else { mid = (first + last)/2; if(key == a[mid]) { found = true; location = mid; } else if (key < a[mid]) { search(a, first, mid - 1, key, found, location); } else if (key > a[mid]) { search(a, mid + 1, last, key, found, location); } } } ```
• The array `a[]` must be in order.
• The method is more efficient, it takes at most (log 2 of size) comparisons.