Presentation is loading. Please wait.

Presentation is loading. Please wait.

Return function Random function Recursion function Function in C 1.

Similar presentations


Presentation on theme: "Return function Random function Recursion function Function in C 1."— Presentation transcript:

1 Return function Random function Recursion function Function in C 1

2 Syntax Return_type fun_name( arguments) { scope of function; return value; } 2

3 Example Write a program by using function to add two numbers and return the result. #include int SUM( ) { int a,b; a=2; b=4; return( a+b); } void main( ) { int S; S = SUM( ); printf (“ \n sum = %d ”, SUM( ) ); printf (“ \n sum = %d ”, S); } Local variable for SUM( ) Calling return function 3

4 Example #include int SUM( ) ; void main( ) { int S; S = SUM( ); printf (“ \n sum = %d ”, SUM( ) ); printf (“ \n sum = %d ”, S); } int SUM( ) { int a,b; a=2; b=4; return( a+b); } 4

5 Example #include int SUM( int a, int b) { return( a+b); } void main( ) { int S, x, y; S = SUM(4, 5 ); printf (“ \n sum = %d ”, S); x = 6; y = 7; printf (“ \n sum = %d ”, SUM( x, y) ); } 5

6 Example #include int SUM( int a, int b); void main( ) { int S, x, y; S = SUM(4, 5 ); printf (“ \n sum = %d ”, S); x = 6; y = 7; printf (“ \n sum = %d ”, SUM( x, y) ); } int SUM( int a, int b) { return( a+b); } 6

7 Example #include int maximum( int x, int y, int z); void main( ) { int n1, n2, n3; scanf (“ %d %d %d”, &n1, &n2, &n3); printf (“ \n max = %d ”, maximum( n1, n2, n3) ); } int maximum( int x, int y, int z) { int max = x; // assume x is the maximum value if (y > max) {max = y;} if (z > max) {max = z;} return max; } 7

8 Rand () function The declaration of rand function is found in rand () is used to generate random numbers between 0 and 32767 You can call it by assignment (i.e. int i = rand ();) Or in any equation ( x = rand() * 2;) Or print the value of rand () ( printf (“r= %d”, rand());) 8

9 Example 9 #include void main( ) { int freq1=0; int freq2=0; int freq3=0; int freq4=0; int freq5=0; int freq6=0; int r, face; for (r=1; r<= 6000; r++) { face = 1+ rand() %6; switch (face) { case 1: freq1++; break; case 2: freq2++; break; case 3: freq3++; break; case 4: freq4++; break; case 5: freq5++; break; case 6: freq6++; break; } // end switch }// end for printf (“ \n frequency1 = %d”, freq1); printf (“ \n frequency2 = %d”, freq2); printf (“ \n frequency3 = %d”, freq3); printf (“ \n frequency4 = %d”, freq4); printf (“ \n frequency5 = %d”, freq5); printf (“ \n frequency6 = %d”, freq6); } //end main

10 Recursion function 10 A recursive function is a function that calls itself either directly or in directly through another function. The recursion step executes while the original call to the function is still open. For example( n! ) You can written like this: int fact = 1; for( i = number; i>=1; i--) fact= fact * i;

11 Continue 11 But you can written in a recursive function like this: #include long factorial (long number); void main() { int i; for( i = 0; i <= 10; i++) printf(“ \n %d! = %d”, i, factorial(i)); } long factorial (long number) { if (number <= 1) return 1; else return (number* factorial(number-1) ); } long factorial (long number) { if (number <= 1) return 1; else return (number* factorial(number-1) ); } Direct recursion

12 Continue 12 #include int Sqr ( int x) { return (x*x); } int fun (int z) { return (z + Sqr(3)); } void main ( ) { int a; printf(“ \n a = “); scanf(“%d”, &a); printf(“\n resullt = %d”, fun(a)); } Indirect recursion

13 Ass 13


Download ppt "Return function Random function Recursion function Function in C 1."

Similar presentations


Ads by Google