220 likes | 392 Views
Class #2 Recursion. ACM TRAINING COURSE. What is recursion ?. Recursion is … Recursion !. In functions we can define recursion as a function that calls itself . *Note: A recursion must have a base case an d a induction case. Example. int recursive(){ recursive(); }
E N D
Class #2 Recursion ACM TRAINING COURSE
Recursionis… Recursion ! • In functions wecan definerecursion as a functionthatcallsitself. • *Note: A recursionmusthave a base case and a induction case.
Example int recursive(){ recursive(); } int main(){ recursive(); }
What do we need to start ? • Knowledge of Return statement • Data types • Parameters • Induction • Recursivity • LOL :D (hope you get the joke)
Return Statement • Terminates the execution of a function and returns control to the calling function (or, in the case of the main function, transfers control back to the operating system). Execution resumes in the calling function at the point immediately following the call. • return [expression]
Example • return 1 • return a • return “holamundo” • return a + 1 • return function(par1, par2)
Parameters • We have 2 types of parameters • By value • By Reference
By value • We only pass the value of the parameter not the variable itself. • This means that if you modify the value inside the function the original variable is not modified.
Example void recursion(int a){ a = 100; } int main(){ int x = 5; recursion(x); }
Expliantion of the example • In this case the value of ‘x’ is always ‘5’ even after exiting the function. • Just the value of ‘a’ is the one that’s going to be modified from ‘5’ to ‘100’.
By reference • We pass the variable itself not just the value of it. • This means that if we modify the value of the parameter then the original variable is going to be modified.
Example void recursion(int &a){ *a = 100; } int main(){ int x = 5; recursion(&x); }
Expliantion of the example • In this case the value of ‘x’ is modified from ‘5’ to ‘100’ when the expression “*a = 100” is executed. • This means that ‘a’ can be treated as the original ‘x’ variable, and all the modifications that occur to ‘a’ are actually occurring in ‘x’.
What is? • Well we can see induction as something that have a base case and a general case. • In the picture of the dominos we can extract 2 things: • Base case: the hit to make the first domino fall • General case: one domino hitting another domino
Why we use it ? • The induction help us to discompose a BIG problem in a lot of little problems, and that make it easier to calculate it. • Is easier to watch one domino hitting another domino than seeing 100 dominos hitting another 100 dominos at the same time.
Example • The Fibonacci sequence • 0, 1, 1, 2, 3, 5, 8, ….. • Base cases: • Fib(0) returns 0 • Fib(1) returns 1 • General case: • Fib(x) returns Fib(x – 1) + Fib(x – 2)
Now what ? • With all this in mind we can start thinking about recursivity. • We can start seeing recursivity in a lot of things in our every-day life like walking, you can see it as a recursive function that moves one foot at a time or stops.
Exercises: • Magic Squares • Tic-Tac-Toe (gato) • Connect 4 • CD
bibliography • www.google.com • http://cse.unl.edu/~dsadofs/RecursionTutorial/index.php • http://erwnerve.tripod.com/prog/recursion/