Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recursive Function Computer Programming. Recursive Function Recursive function is a function that calls itself There is nothing special about calling.

Similar presentations


Presentation on theme: "Recursive Function Computer Programming. Recursive Function Recursive function is a function that calls itself There is nothing special about calling."— Presentation transcript:

1 Recursive Function Computer Programming

2 Recursive Function Recursive function is a function that calls itself There is nothing special about calling a recursive function We call it just as the way we call other typical functions It is typically used in tree and linked list traversal Another common function is factorial calculation

3 Factorial Function int factorial(int n) { if(n>1) return n * factorial(n-1); else return 1; }

4 main() int main() { int x, fac; printf(“Enter number: “); scanf(“%d”, x); fac = factorial(x); printf(“factorial(%d) = %d, x, fac); return 0; }

5 Another Example int mult(int x, int y) { if(y>0) return x + mult(x, y-1); return 0; } This is how x * y can be written. Right!, this doesn’t make sense to write it this way. Anyway, it’s a good way to practice, isn’t it?

6 Try it yourself We can see that both recursive functions are fairly short They look quite easy to program but, at the same, could be difficult to write as well. Just try to write a binary search in terms of recursive function.


Download ppt "Recursive Function Computer Programming. Recursive Function Recursive function is a function that calls itself There is nothing special about calling."

Similar presentations


Ads by Google