58
Functions Programming Applications

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

Embed Size (px)

DESCRIPTION

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

Citation preview

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

Functions

Programming Applications

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

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

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

Functions

Functions are subprograms that can be called from another function(s) (such as the Main function).

printf and scanf are examples of functions.

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

Every C function has 2 parts

int main ( ) heading{

body block return 0;}

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

Program with Several Functions

main function

Square function

Cube function

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

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”

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

7

Example1: hello.c

Function definition

Function call

#include <stdio.h>

/* * Print a simple greeting. */

void sayHello ( void ){ printf(“Hello World!\n”);}

/* * Call a function which prints a * simple greeting. */

int main(){ sayHello();

return 0;}

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

8

Example1: hello.c

Function name

Function body

#include <stdio.h>

/* * Print a simple greeting. */

void sayHello ( void ){ printf(“Hello World!\n”);}

/* * Call a function which prints a * simple greeting. */

int main(){ sayHello();

return 0;}

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

9

Example1: hello.c

Return type

Formal Parameter List

#include <stdio.h>

/* * Print a simple greeting. */

void sayHello ( void ){ printf(“Hello World!\n”);}

/* * Call a function which prints a * simple greeting. */

int main(){ sayHello();

return 0;}

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

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

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

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

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

12

Example3: Average.c

Function definition

Function call

#include <stdio.h>

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);}

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

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

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

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 ;}

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

Example4

Write a C program to read two integer

numbers and Use function to find the

maximum of them

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

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.

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

Call by Value & Call by Reference

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

Call by Value & Call by Reference 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.

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

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.

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

Call By Value

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

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

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

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 55 3

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

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 55 33 5

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

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 55 3 3 5

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

Call By Reference

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

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

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

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 55 3

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

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 55 35 3

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

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 55 3 5 3

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

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

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

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:

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

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

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

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 */

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

Program with Three Functions

#include <stdio.h>

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;}

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

Rest of Program

int Square( int n ){ int s = n * n ; return s;}

int Cube( int n ){ int c = n * n * n ; return c;}

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

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); }

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

 #include <stdio.h>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++){

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

 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);}

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

Recursive Function

The recursive function is function calls itself

The function repeat to call itself with different

parameter values.

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

Trace#include<stdio.h>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);}

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

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)); }

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

Trace#include<stdio.h>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);}

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

Trace#include<stdio.h>

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);}

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

ExampleWhat 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));

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

Example

Write a recursive function MULT to perform

integer multiplication mn using addition

operator where n>0.

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

Exampleint MULT (int m, int n){

int ans;if( n==1 )

ans= m;else

ans = m + MULT (m, n-1);return ans;

}

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

SheetThe value of xy 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 xy

1*

1y

y

xxx

10

yy

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

Sheet

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

)!1(*1

!NN

N 10

NN

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

Organizing a Parade

P(1) = 2P(2) = 3P(n) = P(n-1) + P(n-2) for n > 2

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

Example

Implements the bisection method for

approximation a root of a function in interval

[Xl, Xu]. Approximation the root within epsilon

of a root.

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

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)

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

The Bisection programdouble 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;

}

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

Example

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

1

0

2n

ii

ii

v

vw

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

Example

Write a program contains function Mystrlen()

to find the length of given string

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

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.

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

Recursive SolutionsA binary search is recursive

Repeatedly halves the collection and determines which half could contain the item

Uses a divide and conquer strategy

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

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.

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

The End