Worksheet #6 Pass By Value, Pass By Reference and Recursion 1) Start a new project and write these two functions (you will need to prototype them yourself): void value(int a) { a = 12; } void ref(int &a) { a = 12; } The purpose of this function is simply to initialize the value of some integer to 12. Which function is using pass by reference and which is using pass by value? How do you know? 2) Use this main in your project: int main() { int velocity = 0; cout << velocity << endl; value(velocity); cout << velocity << endl; ref(velocity); cout << velocity << endl; } 3) Compile and execute this program. What are the three outputs? What is happening here? 4) Due to the behavior you see in 3, these functions at this stage are useful for initialization, input or returning multiple values. However, we will use them sparringly. It is possible to use a function with multiple inputs in this way to initialize a group of variables. This group initialization sometimes happens when you have data that all belongs together in one group and you want to initalize them all at once. Write a pass by reference function called initializeDate that takes three integer parameters - each as pass by reference. The first integer is the month, the second integer is the day and the last integer is a year. For initialization purposes, your function, should set the month, day and year variables to 3, 24 and 2016 respectively. Check your answer using this main: int main() { int month, day, year; initializeDate(month,day,year); cout << month << "/" << day << "/" << year << endl; } If done correctly, the output should read: 3/24/2016 **Recursion** 5) A recursive function is a function that calls itself. It must have a base case and a recursive case. Sometimes, a recursive function can have multiple base and recursive cases. Today, you'll be writing a recursive function that has two base cases called the Fibonacci sequence. This sequence is usually defined as: F_0 = 1 F_1 = 1 F_n = F_{n-1} + F_{n-2} In specific, we are going to compute the 'nth' Fibonacci number in the Fibonacci sequence. Here is your function prototype: int fib(int); The first two fibonacci numbers are both 1. The first number is usually indexed as the 0th fibonacci number although sometimes that is subject to preference. This is denoted by F_0 = 1 and F_1 = 1 above. Let's start by adding the checks to our new recursive function. If the integer passed into the function fib is a 0 or a 1 (corresponding to F_0 and F_1), then the function fib should return the value of 1. For right now, if the integer passed in is not a 1 or a 0, then just return 0. Test this by trying this main with your function. It should output 1 twice, followed by a bunch of 0s. int main() { //a for loop that outputs the first 20 fibonacci numbers for(int i = 0; i < 20; i++) { cout << fib(i) << endl; } } 6) Now we want to complete the recursive case. If the number we're looking for is the third Fibonacci (F_2 or a value greater than 1), then we are not the base case. This happens when the passed in integer is bigger than 1. In this case, we want to add the previous two Fibonacci numbers. Luckily, we have a function to get the previous two! Namely, fib. So, write an additional if statement that checks if the passed in integer is bigger than 1 and if so, simply returns fib(x) + fib(y). Here x is the value one less than the passed in integer and y is value two less than the passed in integer. For example, fib(5) is called, then fib(5) will determine that 5 is not 0 and 5 is not 1. But 5 is larger than 1. Thus, fib(5) will return fib(4) + fib(3). 4 is one less than 5 and 3 is two less. This is a lot of description because I'd like you to get to point where you write the code yourself. The entire function is often only four lines of code. with this update, you should get the numbers: 1 1 2 3 5 8 ... and so on. Where each number that shows up is the addition of the previous two. 7) What is the highest fibonacci number that you can compute with this method within 30 seconds? What is the highest within integer limits?