Computer Fundamentals: by Nauman Shamim Lecturer PIEAS
Computer Fundamentals: by Nauman Shamim Lecturer PIEAS
Computer Fundamentals
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
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
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 ( 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 ( 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
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
#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
Situation
Write a program that stores all the numbers entered by the
user .
Re-allocates memory
Data stored in allocated memory is not lost in re-
allocated memory
Pointer to old
Syntax memory block
[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);