Extra Practice for Recursion Recursion l Recursion is

  • Slides: 8
Download presentation
Extra Practice for Recursion

Extra Practice for Recursion

Recursion l Recursion is simply the calling of a function by itself. l We

Recursion l Recursion is simply the calling of a function by itself. l We know that we can call a function from within a function , That function could the function itself. l Recursion is very powerful and very easy to understand but hard to program and debug.

Q? l /* CALCULATE FACTORIAL OF AN INTEGER NUMBER USING RECURSIVE FUNCTION */

Q? l /* CALCULATE FACTORIAL OF AN INTEGER NUMBER USING RECURSIVE FUNCTION */

/* PROGRAM # 42 */ l l l l l /* Recursion: A function

/* PROGRAM # 42 */ l l l l l /* Recursion: A function calling itself*/ /* CALCULATE FACTORIAL OF AN INTEGER NUMBER USING RECURSIVE FUNCTION */ #include <stdio. h> int factorial(int num); main( ){ int var, result; printf("Enter a number: "); scanf("%d", &var); result=factorial(var); printf("n%d! equals %5 d", var, result); } /* Recursive function gets an integer number, calls itself until num gets 0 , returns the result */ int factorial(int num){ if (num == 0) return 1; else return num * factorial(num - 1); }

How is the recursion working? l l l We discuss this by a way

How is the recursion working? l l l We discuss this by a way of an example. In the previous program, if the user inputs 4 as the value of the var then the following would happen: Main program calls factorial(4) calls factorial(3) calls factorial(2) calls factorial(1) calls factorial(0) returns 1 factorial(1) returns 1 1 factorial(2) returns 2 1 1 factorial(3) returns 3 2 1 1 factorial(4) returns 4 3 2 1 1

A recursive function -10 l l l l l int multiply (int m, int

A recursive function -10 l l l l l int multiply (int m, int n) { int answer; if (n == 1) answer = m; else answer = m + multiply (m, n-1); return (answer); }

/* PROGRAM #57 */ l l l l l /* num 3=num 1*num 2

/* PROGRAM #57 */ l l l l l /* num 3=num 1*num 2 -num 3 */ #include<stdio. h> void point(int var, int *ptr 2, int *ptr 3); main( ){ int num 1=1, num 2=2, num 3=3; int *pnum 2, *pnum 3; printf(“num 1, num 2 and num 3 are: %d %d %dn”, num 1, num 2, num 3); pnum 2=&num 2; pnum 3=&num 3; l l l l point(num 1, pnum 2, pnum 3); /* Call the function point and display the result */ printf(“num 1, num 2 and num 3 are: %d %d %dn”, num 1, num 2, num 3); } /* Function point, uses pointers to do the calculation*/ void point(int var, int *ptr 2, int *ptr 3){ int tmp 1, tmp 2; tmp 1=var* (*ptr 2); tmp 2=tmp 1 - (*ptr 3); *ptr 3=tmp 2; } RUN: num 1, num 2 and num 3 are: 1 2 3 num 1, num 2 and num 3 are: 1 2 -1

GAME? -8 l l l l l l /* Problem: This is just a

GAME? -8 l l l l l l /* Problem: This is just a silly program playing with pointers */ #include <stdio. h> int main (void) { /* a and e are integers */ int a, e; /* b is a pointer to an integer */ int* b; /* c is a pointer to an integer */ int** c; /* d is a pointer to an integer */ int*** d; a = 25; /* a contains the integer 25 */ b = &a; /* b contains the address of a */ c = &b; /* c contains the address of b */ d = &c; /* d contains the address of c */ /* Do you understand that ***d is actually a? */ e = ***d * 2; printf ("%d", e); return (0); }