1 / 21

ACM TRAINING COURSE

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(); }

clare
Download Presentation

ACM TRAINING COURSE

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Class #2 Recursion ACM TRAINING COURSE

  2. Whatisrecursion ?

  3. Recursionis… Recursion ! • In functions wecan definerecursion as a functionthatcallsitself. • *Note: A recursionmusthave a base case and a induction case.

  4. Example int recursive(){ recursive(); } int main(){ recursive(); }

  5. What do we need to start ? • Knowledge of Return statement • Data types • Parameters • Induction • Recursivity • LOL :D (hope you get the joke)

  6. 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]

  7. Example • return 1 • return a • return “holamundo” • return a + 1 • return function(par1, par2)

  8. Parameters • We have 2 types of parameters • By value • By Reference

  9. 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.

  10. Example void recursion(int a){ a = 100; } int main(){ int x = 5; recursion(x); }

  11. 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’.

  12. 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.

  13. Example void recursion(int &a){ *a = 100; } int main(){ int x = 5; recursion(&x); }

  14. 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’.

  15. Induction

  16. 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

  17. 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.

  18. 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)

  19. 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.

  20. Exercises: • Magic Squares • Tic-Tac-Toe (gato) • Connect 4 • CD

  21. bibliography • www.google.com • http://cse.unl.edu/~dsadofs/RecursionTutorial/index.php • http://erwnerve.tripod.com/prog/recursion/

More Related