4. Functions
4. Functions
Functions
Slide 2
Recall Activity
If yes, then list down all the predefined functions you remember.
2
Slide 3
What is a Function?
A function is a set of statement(s) which are written to perform a same operation every time. A C program
is a collection of functions. A function may or may not get inputs from the user who calls it. Functions are
used to have our C programs more structured, compact and easy to understand.
To execute any function, user has to call that function in the program and after executing all the
appropriate statements in the function, a function may or may not return a value. But after successful
execution of a function control goes back to the calling function and continues the execution from next
line from where this function is being called. Any function can be called multiple times in any C program
so, this help us in avoiding writing the same statements again where ever required to perform same
operation.
A simple example of a function can be an addition operation on 2 integers. This addition function would
just take 2 integers as an input, adds the both an just return the resultant. So in our program when ever
we need to add 2 integers we can just call this function instead of writing multiple lines of similar code to
perform addition operation.
Slide 4
void printprime()
{
int j,i,flag=1;
j=num/2;
for (i=2;i<=j;i++){
if(num%i==0){
flag=0;
printf("Not prime");
break;
}
}
if (flag) printf("Prime number");
}
5
Slide 6
Return value
6
Slide 7
Function declaration
Where argument-list is
Data-type var-name1, Data-type var-name2,… Data-type var-nameN
7
Slide 8
return
8
Slide 9
Prototyping
If the function definition is before the call is made, then compiler has
the knowledge about the parameters that the function takes (signature)
and return type.
But if a function is called before it is defined, we need to inform the
compiler about the parameters and return type. This is done through
prototyping.
Prototyping is declaring the function that needs to be called .
Declaration of a function is different from definition of a function.
Prototype declaration can be either local or global.
In a large project that consists of several files, the prototypes are
shared using header files. (We will see how to work with this later)
Generally, C Compiler while compiling checks all the files and if it encounters any call to a function before
a declaration of that function it throws an error. To avoid this we can do following:
• Rework on your C program to make sure that function definition appears before that function is being
called in the program.
• Have function prototype (declaration) at the beginning of the c program to make sure that C compiler
read the declaration first before it‟s being called in the program later.
Note that some compilers complaint if the return type is not int. In such case, any function that returns
types other than int must inform the compiler by what is called prototyping.
Slide 10
int num;
int main(){ Declaring the called function in the calling
void pprime(); function since called function returns void.
while(1){
printf("enter an integer greater than 100 ");
scanf("%d",&num);
if(num<100) continue;
pprime();
break;
}
return 0}
10
Slide 11
for (i=2;i<=j;i++){
if(num%i==0){
flag=0;
printf("Not prime");
break;
}
}
if (flag) printf("Prime number");
return; Optional
}
11
Slide 12
Lab Exercise
Write a function to print the square of all the numbers from 1 to 100.
(10 mins)
Addition
Subtraction
Multiplication
Division
(20 mins)
12
Slide 13
It is possible for the calling function to pass values into the called
function through what is called parameters or arguments.
13
Slide 14
14
Slide 15
return gross;
} 15
Slide 16
This is because the value of count gets initialized each time the function
is called.
What we want is a way in which count value can be retained.
17
Slide 18
static variable
18
Slide 19
main(){
void callMe(); Prints:
you called me 1 times
callMe();
you called me 2 times
callMe(); you called me 3 times
callMe();
}
void callMe(){
static int count;
count++;
printf("you called me %d times\n" , count);
}
19
Slide 20
static local:
scope: variables declared and accessed only inside block
default:0
Retains the value between function calls
static keyword is specified with declaration
20
Call by value
void main(){
void f(int i,int j);
int i=0, j=0;
printf("Before calling f(): value of i=%d and value of
j=%d\n", i,j);
f(i,j);
printf("After calling f(): value of i=%d and value of
j=%d\n", i,j);
}
21
Slide 22
i++;
j++;
22
Slide 23
main()
i=0 1000
f() i=1
j=0 1001
Calls j=1
Value of i and j in
1000 and 1001 i=0 2000
remain the same!
j=0 2001
i++;
j++;
In memory
23
Slide 24
Observation
The parameter values changed in the called function code are not
reflected in the calling function.
This is because the parameters are passed by value to the function.
Both functions have their own copy of the variables. Only the values
of the variables of the calling function get copied to the variables of
the called function
24
Slide 25
void increment();
int main(){
increment(); Where should we insert the following
statement in the program to make it
printf("%d\n",i); compile successfully and get the output
} value of i as 11?
25
Slide 26
Lab Exercise
Write a function pow(a,b) where a and b are numbers and the function
returns the value of ab.
(10 mins)
Write a function which takes a number as an argument and returns the
sum of digits in the number.
Example:
1. Input = 234
Output = 9
2. Input 3484
Output = 19
(20 mins)
26
Slide 27
Lab Exercise
27
Slide 28
Call by reference
Introduction to pointers
29
Slide 30
30
Slide 31
Tell me why?
31
Slide 32
32
Slide 33
*i++;
*j++;
33
Recursion
A function that calls itself is a recursive function.
It is very important that a recursive function has an exit point. Otherwise it
will get into infinite loop.
int i; int i;
main(){ main(){
f();} f();}
int f(){ int f(){
f(); i++;
} if(i>5) return ;
f();}
34
Slide 35
35
Slide 36
36
Slide 37
Assume num=9
prime(2) 1 {if(num%i==0) return 0; 9%2!=0
i++;2++ 3
if(i>num/2) return 1;3>(9/2) No!
Return value: 0 else
4 prime(i); prime(3)
}
2 3 Return value: 0
37
Slide 38
Fibonacci numbers
Non-recursive Recursive
main(){
main(){
void fibo(int f1, int f2);
int f1=0,f2=1,f3,i;
int f1=0,f2=1,f3,i;
printf("%d, %d,",
printf("%d, %d,", f1,f2);
f1,f2);
fibo(f1,f2);
for(i=0;i<8;i++)
}
{
void fibo(int f1, int f2){
f3=f1+f2;
static int count;
f1=f2;
int f3; Termination condition
f2=f3;
if(count>8) return;
printf("%d,", f2);
f3=f1+f2;
}
f1=f2; f2=f3;
}
printf("%d,", f2);
count++;
fib.c fibo(f1,f2);}
fibr.c
38
A Fibonacci sequence starts with a 0 and followed by 1. Then onwards each subsequent number is
actually the sum of previous two numbers.
0,1,1,2,3,5,8,13,21,...
Slide 39
Use of recursion
39
Slide 40
Recursive code
main(){
int num, res;
printf("enter a number");
scanf("%d",&num);
res=fact(num);
printf("%d",res);
}
int fact(int n){
int f;
if(n==1) return 1;
f=n*fact(n-1);
return f;
}
40
Slide 41
{int f;
if(n==1) return 1; n=3
f=n*fact(n-1); 3*fact(2) Return 2
return f;}
{int f;
if(n==1) return 1; n=2
f=n*fact(n-1); 2*fact(1)
return f;}
{
int f;
if(n==1) return 1; n=1 Return 1
f=n*fact(n-1);
return f;}
41
Slide 42
Word of caution!
42
Slide 43
Lab Exercise
Write a program that asks the user to type an integer N and computes the
sum of the cubes from 5 to the power 3 to N the power 3. Use recursive
function.
(10 mins)
Write a main program to estimate the value of the constant e (the base of
natural logarithms) using finite number of terms (the number of terms will
be given by the user) of the following series.
(15 mins)
Write a program to print the series: 0,1,4,9,16,.....625
(10 mins)
Write a program in to generate a Fibonacci series till 40.
(10 mins)
43