0% found this document useful (0 votes)
9 views

Lecture 6.Pptx

This document provides an overview of functions in C programming, including their declaration, definition, and calling. It discusses the advantages of using functions, such as code organization and reusability, and includes examples of various functions, including recursive functions and specific cases like calculating the area of a circle and checking for palindrome numbers. Additionally, it emphasizes principles for function definition and parameter transfer in C.

Uploaded by

Sabbir Hossain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Lecture 6.Pptx

This document provides an overview of functions in C programming, including their declaration, definition, and calling. It discusses the advantages of using functions, such as code organization and reusability, and includes examples of various functions, including recursive functions and specific cases like calculating the area of a circle and checking for palindrome numbers. Additionally, it emphasizes principles for function definition and parameter transfer in C.

Uploaded by

Sabbir Hossain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

C Programming

Lecture 6: Functions

void main()
i
copy
{ 5
int a = 5, s =0;
s = func(a);
} int func(int i)
{ r = 1;
copy
12 return r;
}
Outlin
e

1 Functions: declaration, definition and


calling
2 Functions with Examples

3 Recursive Functions

C
Overview

• Functions we
know
1 i n t main ( . ) ;
2 int printf(..); • Functions in
3 int scanf(.); math
4 float sqrt(.) f (x ) = sin(x
5 ;float floor(
)
6 .);float fabs
(.); g (x ) = x
2

• They are actually comparable


• Function in C is more general
• We are going to learn to organize our codes into functions
(blocks)

C
Advantages of function (1)

• We are already familiar with


functions
1 i n t main ( . ) ; // e n t r a n c e o f the program
2 i n t p r i n t f ( . . ) ; // p r i n t t h i n g s onto s c r e
3 e n i n t s c a n f ( . ) ; // r e a d i n p u t from
4 keyboard f l o a t s q r t ( . ) ; // ta k e s q u a r e r o o
5 t
6 f l o a t f l o o r ( . ) ; // ta k e maximum number s m a l l e r than i n p u
t f l o a t f a b s ( . ) ; // ta k e a b s o l u t e v a l u e o f a f l o a t
number

• Advantages
• No need to repeat others work (reinvent the
wheel)
• No need to write things again and again
• Your codes become cleaner

C
Introdution of function (1)

• Let’s start with a simple


example
1 #i n c l u d e < s t d i o . h>
2 v o i d h i ( i n t i ) //<−−− d e c l a r a t i o n
of function”hi
3{ ”
4 p r i n t f ( ” H e l l o %d \n” , i ) ;
5}
6
7 i n t main ( )
8 {
9 int i=0;
10 f o r ( i = 0 ; i < 5 ; i ++)
11 h i ( i ) ; //<−− c a l l f u n c t i o n h i ( i n t i )
12 r e t u r n 0 ; // r e t u r n v a l u e to the one who c a l l s i t
13 }

• We call hi() inside main


• “main()” cannot be called by any other
function
C
Declaration of function (1)

• Declare a function for


n!
1 l o n g f a c t ( i n t i ) ; //<−− t h i s i s the d e c l a r a t i o n
2
3 i n t main ( )
4 {
5 int i=5, f=0;f
6 =fact(i);
7 r e t u r n 0 ; // r e t u r n v a l u e to the one who c a l l s i t
8 }

• The name should be unique


• There is should be input parameter(s) along with the
• types
There is should be output value
type

C
Declaration of function (2)

• Declare a function for


n!
long fact (int n);
input parameters: type name
function name
return value type
• The name should be unique
• There should be input parameter(s) along with the
types
•• There should be output value type
If there is nothing, the returning type is
int

C
Declaration of function (3)

• Declare a function for


n!
float mult (int n, float b);
input parameters: type name
function name
return value type
• The name should be unique
• There should be input parameter(s) along with the
types
• There should be output value type
• If there is nothing, the returning type is int by default

C
Define a function (1)

• Declare a function for


n!
1 l o n g f a c t ( i n t i ) ; //<−− t h i s i s the d e c l a r a t i o n
2
3 i n t main ( )
4 {
5 int i=5, f=0;f
6 =fact(i);
7 r e t u r n 0 ; // r e t u r n v a l u e to the one who c a l l s i t
8 }

error: undefined reference to ‘fact’


• “fact” has been declared, however not defined
(implemented)
• There is no function body
• When you compile it, above error comes out
C
Define a function (2)

• Declare a function for


n!
1 l o n g f a c t ( i n t i ) ; //<−− t h i s i s the d e c l a r a t i o n
2
3 i n t main ( )
4 {
5 int i=5; lo
6 ng f=0;f=
7 fact(i);
8 r e t u r n 0 ; // r e t u r n v a l u e to the one who c a l l s i t
9 }

• Now, let’s think about how to implement


fact()

C
Define a function (3)

long
func1(int n, int i)
{

…....

return r;
}

• You need put function implementation inside the brackets


“{}”

C
Define a function (4)

• Now, let’s think about how to implement


fact()
1 For i from n to 1 do
2 r = r*i
3 i− −
4 End-for
5 return r

C
Define a function (4): separate declaration from
definition
1 long fact(int i)
;
2 i n t main ( )
16 else
3
4 { int i=5; lo
17 {
5 ng f=0;f=
18 w h i l e ( i >0)
6 fact(i);re
19 {
7 turn 0;
20 n = n∗ i ; i
8}
21 −−;
9l o n g f a c t ( i n t i
22 }
)
23 }
10 {
11 longn=1;
24 returnn;
12 if(i<0)
25 }
13 return 0;
14 e l s e i f ( i == 0 )
15 return 1;

C
Define a function (5): combine declaration with
definition
1 long fact(int i
)
2
3 { longn=1;
4 if(i<0)
5 return 0;
6 e l s e i f ( i == 0 ) 18 i n t main ( )
7 return 19 {
8 1;else 20 int i=5; lo
9 { 21 ng f=0;f=
10 while(i 22 fact(i);re
11 >0) 23 turn 0;
12 { 24 }
13 n = n∗ i ; i
14 −−;
15 }
16 }
17 } returnn;

C
Example-1

• Define a function to calculate the area of a


circle
• Arguments and Parameters should be
1 f l o a t a r e a r ( f l o a tr , f l o a t p i )
matched
2{
3 float a=0;
4 a=r∗r∗pi;
5}
6
7 i n t main ( )
8 {
9 float r=1.5;
10 const float pi=3.14159
11 26;r=area(r, pi);
12 return 0;
13 }

C
Example-2 (1)

• Define a function to check whether a number is Palindrome


number
• such as: 321123, 1221, 121

Think about this in 5 minutes ...

C
Example-2 (2)
1 int isPalindr(int n
)
2
3 { intb=n, r=0;w
4 hile(b>0){
5 r = r ∗10+b%10;
6 b=b/10;
7 }
8 i f ( n == r ) {
9 return 1;
10 }else{
11 return 0;
12 }
13 }
14 int main ( ) {
15 int a=515;
16 if(isPalindr(a
17 )){printf( Palindrome number \n” , a ) ;
18 ”%d i s
19 }
20 } return 0;

C
Example-3: perfect number (1)

• 1. Define a function to jugde whether an integer is a perfect


number
• Perfect number: number equals to the sum of all its factors
• 6=1+2+3
• 2. Call it to output all the perfect numbers in range [2, 300]

Think about this problem in 5 minutes...

C
Example-3: perfect number (2)

• 1. Define a function to jugde whether an integer is a perfect


number
• Perfect number: number equals to the sum of all its factors
• 6=1+2+3
• 2. Call it to output all the perfect numbers in range [2, 300]
1 Given a number
2 We should work out all its factors
3 Sum all the factors up
4 See whether it is equal to the number
5 We should use % operator a lot

C
Example-3: perfect number (3)

• 1. Define a function to jugde whether an integer is a perfect


number
• Perfect number: number equals to the sum of all its factors
• 6=1+2+3
• 2. Call it to output all the perfect numbers in range [2, 300]
• Steps:
1 Give n
2 For i from 2 to n do
3 check whether n is dividable by i
4 if yes, sum up
5 Check wether sum equals to n
6 Return 1 or 0
• Let’s do it now!!

C
Example-3: perfect number (4)

int isPerfect(int n
1
2 )
3 {
4 i n t i = 0 , sum = 1 ; i n t
5 up = c e i l ( n / 2 . 0 ) ;
1 Give n 6 f o r ( i = 2 ; i < up ; i ++)
7 {
2 For i from 2 to n
8 i f ( n%i == 0 )
3 do check whether n is dividable {
by i 9 if yes, sum up 10 sum += i ;
4
11 }
5 Check wether sum equals to 12 }
n 13 i f ( sum == n )
14 return 1
6 Return 1 or 0 15 ;
16 else
return 0;
}

C
Example-3: perfect number
(5)
1 #i n c l u d e < s t d i o . h>
2 #i n c l u d e <math . h>
3i n t isPerfect(int n)
4{ 19 i n t main ( )
5 i n t i = 0 , sum = 1 ; 20 {
6 i n t up = c e i l ( s q r t ( n ) ) ; 21 int i=0;
7 f o r ( i = 2 ; i < up ; i ++) 22 f o r ( i = 2 ; i <= 3 0 0 ; i ++)
8 { 23 {
9 i f ( n%i == 0 ) 24 if(isPerfect(i)
)
10 { 25 {
11 sum += i ; 26 p r i n t f ( ”%d \n” i ) ;
,
12 } 27 }
13 } 28 }
14 i f ( sum == n ) 29 return 0;
15 return 1; 30 }
16 else
17 return 0;
18 }
C
Example-4: Armstrong number (1)

• Define a function to check whether a number is Amstrong


number
• For one digits: 11 = 1
• For three digits: 13 + 53+33 = 153
• For four digits: 14 + 64 + 34 + 44 = 1634

Think about this in 5 minutes ...

C
Example-4: Armstrong number (2)
1 #i n c l u d e < s t d i o . h>
20 i f ( s == n ) r
2 i n t i s Arms ( i n t n ) 21 eturn 1
3 { 22 ;
4 i n t nd = 0 , s = 0 , b = 0 ; 23 else
5 inta=n, i=0,t=0; 24 return 0;
6 while(a>0){ 25 }
7 a=a/10; 26
8 nd++; 27 i n t main ( )
9 } 28 {
10 a=n; 29 int i=1;
11 while(a>0){ 30 f o r ( i =1; i <100000; i ++)
12 b = a%10; 31 {
13 t=1; 32 i f ( i s Arms ( i ) == 1 )
14 f o r ( i = 0 ; i < nd ; i ++){ 33 {
15 t = t ∗b ; 34 p r i n t f ( ”%6d ” , i ) ;
16 } 35 }
17 s += t ; 36 }
18 a =a/10; 37 return 0;
19 } // end−w h i l e ( a ) }

C
Function definition: a summary

• Princeples in function definition


1 Remember return type, if there is no need, put void
2 Give a unique and self-telling name to your function
3 Define function first, then you can call it (just as variable in
C)
4 Parameters along with the type appear in pair
5 Parameters are transferred by value

C
Parameter Transfer (1)

a i
int main() copy
{ 5 5
int a = 5, r =0;
r = fact(a);
}
long fact(int i)
{ r = 1;
r copy
r while(i > 0)

120 120
} return r;

• Parameters are transferred in by value not by


address

C
Parameter Transfer (2)

• Let’s consider a simple coding problem


• Given integers a and b
• You are required to swap their values
• For example, a = 5, b = 8
• After swapping, it becomes a = 8, b =
5

C
Parameter Transfer (3)

• You are required to swap their values


• For example, a = 5, b = 8
• After swapping, it becomes a = 8, b =
5
1 i n t main ( )
2 {
3 inta=5,b=8;in
4 t tmp ;
5 p r i n t f ( ” a = %d , b = %d \n” , a , b ) ;
6 tmp = a ; a = b ;
7 b = tmp ;
8 p r i n t f ( ” a = %d , b = %d \n” , a , b ) ;
9 return 0;
10 }

C
Parameter Transfer (4)

• Now, let’s do it by a
function
1 #i n c l u d e < s t d i o . h>
2 v o i d swap ( i n t a , i n t b )
4
3 { i n t tmp = a ;
5 a = b ; b = tmp ; r
6 eturn ;
7}
8 i n t main ( )
9{
10 inta=5,b=8;
11 printf(”a= b = %d \n” , a , b ) ;
12 %d , swap ( a , b )
13 ; p r i n t f ( ” a = b = %d \n” , a , b ) ;
14 } %d ,

C
Parameter Transfer (5)

• The result is against our will,


why???
1 #i n c l u d e < s t d i o . h>
2 v o i d swap ( i n t a , i n t b )
3{
4 i n t tmp = a ;
5 a = b ; b = tmp ;
6 return ; [Output:
7}
]a = 5 , b = 8
8 i n t main ( ) 1

9{ 2 a=5,b=8
10 inta=5,b=8;
11 p r i n t f ( ” a = %d , b = %d \n” , a , b ) ;
12 swap ( a , b ) ;
13 p r i n t f ( ” a = %d , b = %d \n” , a , b ) ;
14 return 0;
15 }

C
Parameter Transfer (6)

int main() 8
{ 5 8
int a = 5, b = 8;
swap(a, b); 5
}
void swap(int a, int b)
{
a 5 b
8
b... 5 a 8
}

• Parameters are transferred in by value not by


address

C
Function Calling again (1)

main()

fun1() fun2() funN()

fun11() fun1N() fun11() fun1N()

• Function can be called in a cascaded manner


• ‘main’ cannot be called
• Functions are not necessarily called by ‘main’
directly

C
Function Calling again (2)

• Parameters are transferred in by value not by


address
• Arguments and Parameters should be matched
1 float calc(intn, float a, short int c
2{ )
3 r e t u r n ( a∗a∗n+c ) ;
4}
5 i n t main ( )
6{
7 intn=2;
8 short intw=4;
9 float x=4.12, r=0
10 ; r = 3∗ c a l c ( n , x , w) ; r
11 eturn 0;
12 }

C
Outlin
e

1 Functions: declaration, definition and


calling
2 Functions with Examples

3 Recursive Functions

C
Recursive Function (1)

• We already know that function is allowed to call any other


function
• Function is allowed to call itself, this is called recursive
• It looks like following
1 i n t func 2 ( i n t n )
2 ; i n t func 1 ( i n t n
3 )
4 {
5 i n t a = 2∗ func 1 ( n −2) ;
6 ...
7 i n t b = func 2 ( n −3) ;
8 r e t u r n ( a+b ) ;
}

• Noticed that “func1” has been called inside


“func1”
• The scale of the problem decreases in each
calling C
Recursive Function: how it works (1)
1 long fact(int n) 1 long fact(int n)
2 { 2 {
3 longa=0; 3 longa=1;
4 if (n<0) 4 int i=0;
5 a=0; 5 if(n<0) r
6 e l s e i f ( n == 1 | | n == 0 ) 6 eturn 0
7 a =1; 7 ;
8 else 8 f o r ( i = n ; i > 0 ; i −−)
9 a = n∗ f a c t ( n −1) ; 9 {
10 10 a = a∗ i ;
11 return a; 11 }
12 } 12 return a;
}
1 i n t main ( )
2 {
3 intn=4,b=0;
4 b=fact(n);
5 p r i n t f ( ” f a c t (%d ) = %d \n” , n , b ) ;
6 return 0;
7}

C
Recursive Function: how it works (2)

• “fact” calls itself until the bottom is reached


• Noticed that the scale of the problem decreases
gradually
• Advantage: simple
• Darkside: requires a lot of memory

main() fact(4) fact(3) fact(2) fact(1)


{ { { { {
return 1;
fact(4) 4*fact(3); 3*fact(2); 2*fact(1);
} } } }
}
4*6; 3*2; 2*1;

• Suggesion: try to avoid to use recursive


function
C
Recursive Function: Hanoi Tower Problem

• One is allowed to move one disc from one beam to another a


day
• Move all 64 discs from beam A to C

• It would not be fulfilled even till the end of this


world!!
C
Source code for Hanoi Tower (1)

1 #i n c l u d e < s t d i o . h>
2 v o i d h a n o i ( i n t n , c h a r b1 , c h a r b2 , c h a r b3
)
3
4 { i f ( n == 1 )
5 {
6 p r i n t f ( ”%c −−−−> %c \n” , b1 , b3 ) ;
7 } e l s e i f ( n == 2 )
8 {
9 p r i n t f ( ”%c −−−−> %c \n” , b2 )
10 b1 , p r i n t f ( ”%c −−−−> %c ;
11 \n” , b1 , p r i n t f ( ”%c −−−−> b3 )
12 %c \n” , b2 , ;
13 }else{ b3 )
14 h a n o i ( n −1 , b1 , b3 , b2 ) ; p ;b3 )
15 r i n t f ( ”%c −−−−> %c \n” , b1 , ;
16 h a n o i ( n −1 , b2 , b1 , b3 ) ;
17 } }

C
Source code for Hanoi Tower (2)

19 i n t main ( )
20 {
21 intn=20;
22 printf(”Inputn:”);
23 s c a n f ( ”%d” , &n ) ;
24 h a n o i ( n , ’A ’ , ’B ’ , ’C ’ ) ;
25 }

1 Move top n-1 plates from A to B via


C
2 Move the bottom one to C
3 Move n-1 plates from B to C via A

You might also like