CIS-104
Computer Fundamentals
Lecture by Nauman Shamim Lecturer PIEAS
CALLING FUNCTION INSIDE ANOTHER FUNCTION
Can we call a user defined function inside another function ?
Yes
#include<stdio.h> Output
#include<conio.h> Inside Main
Inside C
void C(void){ End of C
printf("\nInside C"); End of Main
printf("\nEnd of C");
}
void main(){
printf("Inside Main");
C();
printf("\nEnd of Main");
getch();
}
NESTED FUNCTION CALLS
#include<stdio.h>
#include<conio.h> Output
Inside Main
void C(void){ Inside B
printf("\nInside C");
Inside C
printf("\nEnd of C");
} End of C
End of B
void B(void){ End of Main
printf("\nInside B");
C();
printf("\nEnd of B");
}
void main(){
printf("Inside Main");
B();
printf("\nEnd of Main");
getch();
}
ORDER OF EXECUTION
#include<stdio.h>
#include<conio.h>
In which order the functions are called ?
void C(void){
printf("\nInside C"); 1-Main
printf("\nEnd of C");
2-B
}
3-C
void B(void){
printf("\nInside B");
C();
printf("\nEnd of B");
In which order the functions finished ?
} 1-C
2-B
void main(){
printf("Inside Main"); 3-Main
B();
printf("\nEnd of Main");
getch();
}
WHAT IS THE ORDER OF EXECUTION ?
#include<stdio.h>
#include<conio.h>
void C(void){
printf("\nInside C");
printf("\nEnd of C"); Inside Main
}
Inside Test
void B(void){
printf("\nInside B"); Inside A
C();
printf("\nEnd of B");
Inside B
} Inside C
void A(void){ End of C
printf("\nInside A");
B(); End of B
printf("\nEnd of A");
End of A
}
End of Test
void Test(void){ End of Main
printf("\nInside Test");
A();
printf("\nEnd of Test");
}
void main(){
printf("Inside Main");
Test();
printf("\nEnd of Main");
getch();
}
RECURSION
Recursive functions
Functions that call themselves
Can only solve a base case
The function launches a new copy of itself (recursion step) to
solve what it cannot do
Eventually base case gets solved
Gets plugged in, works its way up and solves whole problem
EXAMPLE USING RECURSION: FACTORIAL
Example: factorials
5! = 5 * 4 * 3 * 2 * 1
Notice that
5! = 5 * 4!
4! = 4 * 3! ...
Can compute factorials recursively
Solve base case (1! = 0! = 1) then plug in
2! = 2 * 1! = 2 * 1 = 2;
3! = 3 * 2! = 3 * 2 = 6;
FACTORIAL
Factorial (n)
1. if n=1
2. return 1
3. else
4. return n * Factorial(n-1)
120
Call Sequence
Factorial ( 5 )
if 5 = 1 return 1
else
return 5 * Factorial
24 (4)
Factorial ( 4 )
if 4 = 1 return 1
else
return 4 * Factorial
6 (3)
Factorial ( 3 )
if 3 = 1 return 1
else
return 3 * Factorial
2 (2)
Factorial ( 2 )
if 2 = 1 return 1
else
return 2 * Factorial
1 (1)
Factorial ( 1 )
if 1 = 1 return 1
else
return 5 * Factorial (4)
EXAMPLE USING RECURSION: THE FIBONACCI SERIES
Fibonacci series: 0, 1, 1, 2, 3, 5, 8...
Each number is the sum of the previous two
Can be solved recursively:
Fib( n ) = Fib( n - 1 ) + Fib( n 2 )
Code for the Fibonacci function
Fibonnaci (n)
1. if n 2
2. return n-1
3. else
4. return Fibonnaci (n-1) + Fibonacci (n-2)
THE CODE
Fibonnaci (n)
1. if n =1 or n = 2
2. return n-1
3. else
4. return Fibonnaci (n-1) + Fibonacci (n-2)
Fib(n) = Fib(n-1) + Fib(n-2)
3
Fib ( 5 )
if 5 = 1 or 5 = 2
return 5-1
2 + Fib1(3)
else return Fib (4)
Fib ( 4 ) Fib ( 3 )
if 4 = 1 or 4 = 2 if 3 = 1 or 3 = 2
return 4-1 return 3-1
else return Fib1(3) + Fib
1 (2) else return Fib1(2) + Fib
0 (1)
Fib ( 3 ) Fib ( 2 ) Fib ( 2 ) Fib ( 1 )
if 3 = 1 or 3 = 2 if 2 = 1 or 2 = 2 if 2 = 1 or 2 = 2 if 1 = 1 or 1 = 2
return 3-1 return 2-1 return 2-1 return 1-1
else return Fib1(2) + Fib else return Fib (1) + Fib (0) else return Fib (0) + Fib (-1)
0 (1) else return Fib (1) + Fib (0)
Fib ( 2 ) Fib ( 1 )
if 2 = 1 or 2 = 2 if 1 = 1 or 1 = 2
return 2-1 return 1-1
else return Fib (1) + Fib (0) else return Fib (0) + Fib (-1)
DYNAMIC MEMORY ALLOCATION
Normal Memory Allocation
All variables including are provided fixed size memory
according to the declaration
Example
int a ; provides 4 bytes of memory to variable a
int b[5] ; provides 20 bytes of memory to array b
char c;provides 1 byte of memory to variable c
Dynamic Memory Allocation
Allocation of memory to variables during program execution
WHY DYNAMIC MEMORY ALLOCATION
Sometimes memory requirement is not clear at compile
time
Creating matrices where size of matrix will be provided by
user
Storing all the numbers entered by user, where it is not
defined how many numbers will be entered by user
Finding primes from an array and storing it in another array
Large size array provide one solution but memory is
wasted in most cases
DYNAMIC MEMORY ALLOCATION IN C
Function: malloc()
Header File: stdlib.h
About malloc() :
void malloc(int)
It takes an integer parameter size, reserves a memory block of
that size and returns a pointer which do not have any type (void)
USING MALLOC
#include<stdlib.h>
Pointer type defined
int main(){
Call to malloc function
char *line;
Memory required in bytes
ptr=(char *)malloc(20);
42000
42001
42001 42002
.
line=Pakistan; .
.
Reserved
42015
return 0; 42016
42017
42018
42019
} 42020
42021
.
.
RETURN TYPE OF MALLOC
Returns a pointer to the reserved block,
The type of pointer is not defined
Returns null if it cannot reserve the required memory
Returns null if memory required is specified as zero (0)
A BETTER USE OF MALLOC
#include<stdlib.h>
20 blocks , each of size
int main(){ character
char *line;
int *set;
line=(char *)malloc( 20*sizeof(char) );
set=(int *)malloc( 20*sizeof(int) ) ;
return 0;
} 20 blocks , each of size
integer
FREEING MEMORY
If memory is no more required, the program should free it
Function : free(void *ptr)
The parameter to free is a pointer of no type which means any type of
pointer can be passed to it
#include<stdlib.h>
int main(){
int *set;
set=(int *)malloc(10*sizeof(int));
free(set);
}
EXAMPLE
#include<stdlib.h> printf(Size of set = %d,size);
#include<conio.h> printf(Set members are \n);
#include<stdio.h>
for(i=0;i<size;i++){
printf( %d ,set[i]);
int main(){ }
int *set;
int size,i; free(set);
printf(Enter size of the set \n);
getch();
scanf(%d,&size); return 0;
set=(int *)malloc(size); }
for(i=0;i<size;i++){
printf(Enter value for %d element:,i);
scanf(%d,&set[i]);
}
MALLOC() LIMITS
malloc allow to provide a fix size of memory
What if we have allocated memory using malloc and we
need little bit of more ?
Situation
Write a program that stores all the numbers entered by the
user .
Can we solve this problem using malloc ?
REALLOC()
Re-allocates memory
Data stored in allocated memory is not lost in re-
allocated memory
Pointer to old
Syntax memory block
void * realloc( void *block, int size)
A pointer to new Updated size of
memory block, the type memory block
of pointer is not set
EXAMPLE-REALLOC
[Header Files]
int main(){ printf(size of set = %d \n,i);
printf(Members are \n);
int num,i=0, *set,j;
for(j=0;j<I;j++)
//initial size of set is = 1 printf( %d ,set[j]);
set=(int*)malloc(1*sizeof(int));
printf(Enter Numbers, -1 to terminate\n ); free(set);
do{ getch();
return 0;
printf(Enter number = );
}
scanf(%d,&num);
set[i]=num;
i++;
//changing size of set to 2
set=(int *)realloc(set,(i+1)*sizeof(int));
}while(num>=0);