Presentation is loading. Please wait.

Presentation is loading. Please wait.

Functions Programming Applications. Functions every C program must have a function called main program execution always begins with function main any.

Similar presentations


Presentation on theme: "Functions Programming Applications. Functions every C program must have a function called main program execution always begins with function main any."— Presentation transcript:

1 Functions Programming Applications

2 Functions every C program must have a function called main program execution always begins with function main any other functions are subprograms and must be called

3 Functions Functions are subprograms that can be called from another function(s) (such as the Main function). printf and scanf are examples of functions.

4 Every C function has 2 parts int main ( )heading { body block return 0; }

5 Program with Several Functions main function Square function Cube function

6 6 Writing Functions Need to specify: the name of the function its parameters what it returns The block of statements is called the “function body”

7 7 Example1: hello.c Function definition Function call #include /* * Print a simple greeting. */ void sayHello ( void ) { printf(“Hello World!\n”); } /* * Call a function which prints a * simple greeting. */ int main() { sayHello(); return 0; }

8 8 Example1: hello.c Function name Function body #include /* * Print a simple greeting. */ void sayHello ( void ) { printf(“Hello World!\n”); } /* * Call a function which prints a * simple greeting. */ int main() { sayHello(); return 0; }

9 9 Example1: hello.c Return type Formal Parameter List #include /* * Print a simple greeting. */ void sayHello ( void ) { printf(“Hello World!\n”); } /* * Call a function which prints a * simple greeting. */ int main() { sayHello(); return 0; }

10 10 Example2: sort.c /* Print two numbers in order. */ void Sort ( int a, int b ) { int temp; if ( a > b ) { printf("%d %d\n", b, a); } else { printf("%d %d\n", a, b); } Parameters

11 11 int main() { int x = 3, y = 5; Sort ( 10, 9 ); Sort ( y, x + 4 ); return 0; } Example2: sort.c /* Print two numbers in order. */ void Sort ( int a, int b ) { int temp; if ( a > b ) { printf("%d %d\n", b, a); } else { printf("%d %d\n", a, b); } Formal parameters Actual parameters

12 12 Example3: Average.c Function definition Function call #include float Average ( int X, int Y, int Z ) { float AVG; AVG = (X+Y+Z)/ 3.0; return AVG; } void main(void) { int a,b,c; float AVG; a = 5; b = 6; c = 2; AVG = Average (a, b, c); printf (“Average = %6.3f \n”, AVG); }

13 Two Kinds of Functions Always returns a single value to its caller and is called from within an expression. Never returns a value to its caller, and is called as a separate statement. Value-Returning Void

14 Trace void main() { int a = 2; int b = 2; int c = 3; b = mult( a, c ) ; printf(“ a = %d b = %d c = %d \n “, a, b, c ) ; c = squares( a, b ) ; printf(“ a = %d b = %d c = %d \n “, a, b, c ) ; } int mult (int x, int y ) { return x * y; } int squares ( int p, int q ) { int r = p * p + q * q ; return r ; }

15 Example4 Write a C program to read two integer numbers and Use function to find the maximum of them

16 Example5 Write a complete program that asks for radius of a circle and calculates its area using a function with return value of float type.

17 Call by Value & Call by Reference

18 Call by Value: The Formal Parameters are not the Actual Parameters. Only Values from the Actual Parameters are passed to the Formal Parameters. Call by Reference: The Formal Parameters are referring to the Actual Parameters.

19 Call by Value & Call by Reference Call by Value: Changes in the Formal Parameters Will not affect the Actual Parameters. Call by Reference: Changes in the Formal Parameters Will affect the Actual Parameters.

20 Call By Value

21 21 int main() { int a = 3, b = 5; printf("%d %d\n", a, b); badSwap ( a, b ); printf("%d %d\n", a, b); return 0; } Example6: bad_swap.c /* Swap the values of two variables. */ void badSwap ( int a, int b ) { int temp; temp = a; a = b; b = temp; printf("%d %d\n", a, b); } Output: 3 5

22 22 int main() { int a = 3, b = 5; printf("%d %d\n", a, b); badSwap ( a, b ); printf("%d %d\n", a, b); return 0; } Example6: bad_swap.c /* Swap the values of two variables. */ void badSwap ( int a, int b ) { int temp; temp = a; a = b; b = temp; printf("%d %d\n", a, b); } Output: 3 5 5 3

23 23 int main() { int a = 3, b = 5; printf("%d %d\n", a, b); badSwap ( a, b ); printf("%d %d\n", a, b); return 0; } Example6: bad_swap.c /* Swap the values of two variables. */ void badSwap ( int a, int b ) { int temp; temp = a; a = b; b = temp; printf("%d %d\n", a, b); } Output: 3 5 5 3 3 5

24 24 int main() { int a = 3, b = 5; printf("%d %d\n", a, b); badSwap ( a, b ); printf("%d %d\n", a, b); return 0; } Example6: bad_swap.c /* Swap the values of two variables. */ void badSwap ( int a, int b ) { int temp; temp = a; a = b; b = temp; printf("%d %d\n", a, b); } Output: 3 5 5 3 3 5

25 Call By Reference

26 26 int main() { int a = 3, b = 5; printf("%d %d\n", a, b); Swap ( &a, &b ); printf("%d %d\n", a, b); return 0; } Example7: swap.c /* Swap the values of two variables. */ void Swap ( int *a, int *b ) { int temp; temp = *a; *a = *b; *b = temp; printf("%d %d\n", *a, *b); } Output: 3 5

27 27 int main() { int a = 3, b = 5; printf("%d %d\n", a, b); Swap ( &a, &b ); printf("%d %d\n", a, b); return 0; } Example7: swap.c /* Swap the values of two variables. */ void Swap ( int *a, int *b ) { int temp; temp = *a; *a = *b; *b = temp; printf("%d %d\n", *a, *b); } Output: 3 5 5 3

28 28 int main() { int a = 3, b = 5; printf("%d %d\n", a, b); Swap ( &a, &b ); printf("%d %d\n", a, b); return 0; } Example7: swap.c /* Swap the values of two variables. */ void Swap ( int *a, int *b ) { int temp; temp = *a; *a = *b; *b = temp; printf("%d %d\n", *a, *b); } Output: 3 5 5 3

29 29 int main() { int a = 3, b = 5; printf("%d %d\n", a, b); Swap ( &a, &b ); printf("%d %d\n", a, b); return 0; } Example7: swap.c /* Swap the values of two variables. */ void Swap ( int *a, int *b ) { int temp; temp = *a; *a = *b; *b = temp; printf("%d %d\n", *a, *b); } Output: 3 5 5 3

30 Remember !! void Average ( int x, y, z)  Wrong void Average (int x, int y, int z)  Correct

31 31 void main(void) { int x = 5, y = 3, z = 7; int Max, Min; MaxMin(x,y,z,&Max, &Min); printf("%d %d\n", Max, Min); } Example8: Max_Min.c void MaxMin ( int a,int b,int c, int *MX, int *MN) { if ((a>b)&&(a>c)) *MX = a; else if (b>c) *MX = b; else *MX = c; if ((a<b)&&(a<c)) *MN = a; else if (b<c) *MN = b; else *MN = c; } Output:

32 32 void main(void) { int x = 5, y = 3, z = 7; int Max, Min; MaxMin(x,y,z,&Max, &Min); printf("%d %d\n", Max, Min); } Example8: Max_Min.c void MaxMin ( int a,int b,int c, int *MX, int *MN) { if ((a>b)&&(a>c)) *MX = a; else if (b>c) *MX = b; else *MX = c; if ((a<b)&&(a<c)) *MN = a; else if (b<c) *MN = b; else *MN = c; } Output: 7 3

33 What is in a prototype? A prototype looks like the function heading but must end with a semicolon”;” It has no body. Usually located at the beginning of the program to declare all the functions to be used. int Cube( int n); /* prototype */

34 Program with Three Functions #include int Square( int n); /* declares these functions */ int Cube( int n); int main( ) { int num = 3 ; int sq, cb ; sq = Square(num); printf(“ The square of 3 is %d \n “, sq ); cb = Cube(num); printf(“ The cube of 3 is %d \n “, cb ); return 0; }

35 Rest of Program int Square( int n ) { int s = n * n ; return s; } int Cube( int n ) { int c = n * n * n ; return c; }

36 int main() { int a, b=1, c=2, d=3, e; a=d; e=c; a=1; printf(" %d \t %d \t %d \n", e, c, d); b=SomeFunction(&c); printf(" %d \t %d \t %d \n", b, c, e); return 0; } int SomeFunction(int *m) { *m= (*m)+1; return (*m); }

37 #include void duplicate (int a, int *b, int *c) { *b=a; a=1; *c=2; } int Star(int a, int b) { int c=0; int i; for(i=0 ; i<2 ; i++) {

38 duplicate ( c, &a, &b); a++; b++; c++; printf(“%d %d %d\n”, a, b, c); } return a; } void main (void) { int x=1, y=3, z=7; x = Star(y, z); printf("x=%d\t y=%d\n z=%d", x, y, z); }

39 Recursive Function The recursive function is function calls itself The function repeat to call itself with different parameter values.

40 Trace #include int f(int x) { if (x==0) return 0; else if (x>=1) return x+f(x-1); } void main() { int y=f(0); int z = f(4); printf (“ %d \t %d \n “, y, z); }

41 Example what are the value of fun(7, 1) and fun(6, 3) int fun (int m, int n) { if(n==1) return m; else return (m+fun(m, n-1)); }

42 Trace #include int f(int x){ if (x==0) return 0; else if (x>=1) return x+f(x-1); } void main() { int y=f(0); int z = f(4); printf (“ %d \t %d \n “, y, z); }

43 Trace #include int g(int a) {if (a==0) return 1; Else if(a==1) return 2; Else if (x>=2) return g(a-1)*g(a-2);} void main() {Int y=g(0); Int z = g(1); Int x = g(5) Printf (“ %d \t %d \t %d \n “, y, z, x);}

44 Example What are the values of gcd(7,21) and gcd(5,3) int gcd(int m, int n) { int r; if (m < n) return gcd(n,m); r = m%n; if (r = = 0) return(n); else return(gcd(n,r));

45 Example Write a recursive function MULT to perform integer multiplication m n using addition operator where n>0.

46 Example int MULT (int m, int n) { int ans; if( n==1 ) ans= m; else ans = m + MULT (m, n-1); return ans; }

47 Sheet The value of x y can be calculated as recursive function Write recursive function called power() that accept real number and integer number as arguments and return the value of x y

48 Sheet Write recursive function called Fact() that applies the factorial of integer N as

49 Organizing a Parade P(1) = 2 P(2) = 3 P(n) = P(n-1) + P(n-2) for n > 2

50 Example Implements the bisection method for approximation a root of a function in interval [Xl, Xu]. Approximation the root within epsilon of a root.

51 Bisection algorithm Compute x_mid = (xl + xu)/2 If f(xl)*f(x_mid) <0 Find root by Bisection (xl, x_mid) Find root by Bisection (x_mid, xu)

52 The Bisection program double Bis (double xl, double xu) { double root, x_mid; x_mid = (xl +xu)/2.0; if ((xu – xl) < epslion) || (f (x_mid)<0.0)) root = x_mid; else if ( f(xl) * f(x_mid)<0.0) root = Bis (xl, x_mid); else root = Bis(x_mid, xu); return root; }

53 Example Write a program that inputs a vector v and call function Normalize to display a corresponding unit vector W where

54 Example Write a program contains function Mystrlen() to find the length of given string

55 Example Write a program to read group of numbers from the user and then average them after stored them in an array and print the result.

56 Recursive Solutions A binary search is recursive Repeatedly halves the collection and determines which half could contain the item Uses a divide and conquer strategy

57 Example ( Hanoi Game) The object of game is to transfer the disks from the leftmost pole to rightmost pole, without ever placing a larger disk on top of a smaller disk. Only one disk may be moved at a time, and each disk must always be placed on one pole.

58 The End


Download ppt "Functions Programming Applications. Functions every C program must have a function called main program execution always begins with function main any."

Similar presentations


Ads by Google