Presentation is loading. Please wait.

Presentation is loading. Please wait.

CGS 3460 Function – I n What is a function lOne named program module that does one primary task lConsists of a set of statements n Why we need functions?

Similar presentations


Presentation on theme: "CGS 3460 Function – I n What is a function lOne named program module that does one primary task lConsists of a set of statements n Why we need functions?"— Presentation transcript:

1 CGS 3460 Function – I n What is a function lOne named program module that does one primary task lConsists of a set of statements n Why we need functions? lChop up a long program for Better organization of code Use same code repetitively lEach function acts as a building block

2 CGS 3460 Example: Area Calculation double CalcArea(double hight, double width) { return height * width; } int main() { double h, w, area; printf(“Please input height and width\n”); scanf(“%f, %f”, &h, &w); area = CalcArea(h, w); printf(“The area is %f”, area); return 0; }

3 CGS 3460 Example – II n Compute the factorials double factorial(int n) { double result = 1; int i; for( i = 2; i <= n; i++) result = result * i; return result ; }

4 CGS 3460 Example: factorials 5! = 5 * 4 * 3 * 2 * 1 lNotice that 5! = 5 * 4! 4! = 4 * 3!... lCan compute factorials recursively lSolve base case (1! = 0! = 1) then plug in 2! = 2 * 1! = 2 * 1 = 2; 3! = 3 * 2! = 3 * 2 = 6; n factorial(5) = 5 * factorial(4);

5 CGS 3460 Example: factorials n factorial(n) = n * factorial(n-1); double factorial(int n) { double result; if ( n <= 1 ) return 1; else { result = n * factorial(n – 1); return result ; } Does this code work?

6 CGS 3460 Recursive Function Call double factorial(int n) { if ( n <= 1 ) return 1; else return (n * factorial(n – 1)); } double factorial(int n) { if ( n <= 1 ) return 1; else return (n * factorial(n – 1)); } double factorial(int n) { if ( n <= 1 ) return 1; else return (n * factorial(n – 1)); } double factorial(int n) { if ( n <= 1 ) return 1; else return (n * factorial(n – 1)); } n=4 n=3 n=2 n=1 return (4 * factorial(4 – 1)); factorial( 3)); return (3 * factorial(3 – 1)); factorial( 2)); return (2 * factorial(2 – 1)); factorial( 1)); return 1; 1); 2); 6);

7 CGS 3460 Recursion n Recursive functions lFunctions that call themselves lCan solve a base case or base cases lDivide a problem up into Base case(s), which is what it can do Others The function launches a new copy of itself (recursion step), leading to base case(s) Eventually base case gets solved Gets plugged in, works its way up and solves whole problem

8 CGS 3460 Recursive Approaches n Recursive function is called to solve a problem lKnow how to solve only the simplest case(s), or so-called base case(s) lResemble a complex problem in a simpler or smaller version of the same problem. n Execution lThe recursion step execute while the original call to the function is still open, i.e., it has not yet finished. lMay be many recursive calls. lAll calls converge on the base case(s)

9 CGS 3460 Example n Function power(base, exponent) which returns base exponent lRecursion relationship base exponent = base * base exponent-1 ; lBase case: base 1 = base;

10 CGS 3460 Code double power(float base, int exponent) { if (exponent == 1) return base; else return( base * power(base, exponent-1)); }

11 CGS 3460 Fibonacci series n Fibonacci series: 0, 1, 1, 2, 3, 5, 8... lEach number is the sum of the previous two lCan be solved recursively: fib( n ) = fib( n - 1 ) + fib( n – 2 ) lCode for the fibaonacci function long fib( int n ) { if (n == 0 || n == 1) // base case return n; else return( fib( n - 1) + fib( n – 2 ) ); }


Download ppt "CGS 3460 Function – I n What is a function lOne named program module that does one primary task lConsists of a set of statements n Why we need functions?"

Similar presentations


Ads by Google