functions programming applications. functions every c program must have a function called main...
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.TRANSCRIPT
Functions
Programming Applications
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
Functions
Functions are subprograms that can be called from another function(s) (such as the Main function).
printf and scanf are examples of functions.
Every C function has 2 parts
int main ( ) heading{
body block return 0;}
Program with Several Functions
main function
Square function
Cube function
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
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;}
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;}
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;}
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
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
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);}
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
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 ;}
Example4
Write a C program to read two integer
numbers and Use function to find the
maximum of them
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.
Call by Value & Call by Reference
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.
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.
Call By Value
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
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
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
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
Call By Reference
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
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
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
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
Remember !! void Average ( int x, y, z) Wrong void Average (int x, int y, int z) Correct
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
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
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 */
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;}
Rest of Program
int Square( int n ){ int s = n * n ; return s;}
int Cube( int n ){ int c = n * n * n ; return c;}
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); }
#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++){
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);}
Recursive Function
The recursive function is function calls itself
The function repeat to call itself with different
parameter values.
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);}
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)); }
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);}
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);}
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));
Example
Write a recursive function MULT to perform
integer multiplication mn using addition
operator where n>0.
Exampleint MULT (int m, int n){
int ans;if( n==1 )
ans= m;else
ans = m + MULT (m, n-1);return ans;
}
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
Sheet
Write recursive function called Fact() that applies the factorial of integer N as
)!1(*1
!NN
N 10
NN
Organizing a Parade
P(1) = 2P(2) = 3P(n) = P(n-1) + P(n-2) for n > 2
Example
Implements the bisection method for
approximation a root of a function in interval
[Xl, Xu]. Approximation the root within epsilon
of a root.
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)
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;
}
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
Example
Write a program contains function Mystrlen()
to find the length of given string
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.
Recursive SolutionsA binary search is recursive
Repeatedly halves the collection and determines which half could contain the item
Uses a divide and conquer strategy
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.
The End