Functions: Declaration, Definition, Call and
return, Scope of variables, Storage classes,
Recursive functions, Recursion vs Iteration.
Declaration and definition
Function is a self-contained block of statements
that perform a logical task
Declaration or prototype: int add2(int,int);
Definition is actually writing the full function:
int add2(int a, int b){
return (a+b);
}
Two ways to write functions
int add2(int,int);
int add2(int a, int b){
return (a+b);
main(){
}
printf(“%d”,add2(10,20));
}
main(){
printf(“%d”,add2(10,20));
int add2(int a, int b){
}
return (a+b);
}
Why to use functions?
1) Avoids rewriting the same code
2) Makes the program easier to write and modify
How does
function work?
int add2(int,int); //prototype
int sub2(int,int);
int main(){
int i=1230,j=-100,sum,diff;
sum = add2(i,j); //function call with i,j parms
diff = sub2(i,j);
printf("sum = %d, diff = %d\n",sum,diff);
}
int add2(int a, int b){ //definition
return a+b; // a,b are formal params
}
int sub2(int a, int b){ return a-b;}
Any function can call any function
#include<stdio.h>
int message();
int main( ){
message( ) ;
}
int message( ){
printf ( "\nCan't imagine life without C" ) ;
main( ) ;
}
Recursion
Recursion – function calling itself
#include<stdio.h>
int fact(int);
int main(){
int n=5;
printf("Factorial = %d\n",fact(n));
}
int fact(int n){
if(n>1) return n*fact(n-1); //calling itself with n-1
else return 1;
}
Fibonacci tree from recursion fibo(n) = fibo(n-1) +
fibo(n-2). Example of fibo(4) below:
Recursion programs to try
●Finding summation of n numbers
●Fibonacci series in which a0=0, a1=1, a(n) =
a(n-2) + a(n-1) i.e. any term is sum of previous
two terms. So series = 0,1,1,2,3,5,8,13...
●Tower of Hanoi game
https://www.youtube.com/watch?v=7AUG7zXe-
KU
Tower of Hanoi
There are 3 towers named A, B and C. The task is to move the
required number of disks from A to C using B with two rules (i)
Small disk always above the bigger one (ii) Move one disk at a
time.
Some people say the universe will end when you can finish moving
64 disks of this game and thus it is also called the Game of
Brahma in the temple of city Hanoi in Vietnam. Here is a story -
https://ianparberry.com/TowersOfHanoi/index64.html
Tower of Hanoi
Tower of Hanoi - program
#include <stdio.h>
void tower(int n, char A, char C, char B) ;
int main() {
int n;
printf("\n Enter the number of disks: "); scanf("%d",&n);
tower(n, 'A', 'C', 'B'); // A, B and C are names of poles; A to C via B
return 0;
}
void tower(int n, char A, char C, char B) {
if (n == 0) return;
tower(n-1, A, B, C);
printf("\n Move disk %d from rod %c to rod %c", n, A,C);
tower(n-1, B,C,A);
}
Loops versus recursion
Iteration involves repetition of same structure.
Recursion involves repetition through calling the
function itself with different inputs.
Iteration needs less memory and time.
Recursion consumes more memory and is
slower but makes the program simpler.
Two types of functions
●Library functions Ex. printf( ), scanf( )
●User-defined functions Ex. add2( ), fact( )
Okay to have many return
statements but one return value
int fun(int n){
if(n>1)
return n;
else
return (-n);
}
// return (n,-n); is wrong
Valid returns
1) return;
2) return 23;
3) return (23); //brackets are optional
4) return -12.76;
5) return ‘m’;
Void means no return
void msg(){
printf("just print\n");
}
What is the output
void fun ( int b ){
b = 60 ;
printf ( "\n%d", b ) ;
}
main( ){
int a = 30 ;
fun ( a ) ;
printf ( "\n%d", a ) ;
}
Variables scope – local and global
●Local variables are defined and thus
alive within a function or a code
segment enclosed within {}
●Global variables are declared outside
all the functions
Local versus global
#include<stdio.h>
int g = 20;
int main () {
printf("g = %d\n",g); //global
int g = 10; //local
printf("g = %d\n",g); //local
}
Storage Classes in C
(a) Where the variable would be stored.
(b) What will be the initial value
(c) What is the scope of the variable
(d) How long would the variable exist.
Four Storage classes
1) Auto
2) Register
3) Static
4) External
AUTO
AUTO
Storage memory
Initial value random
Scope Local
Life Till within the scope
AUTO – example 1
main( ){
auto int i = 1 ;
{
{
{
printf ( "\n%d ", i ) ;
}
printf ( "%d ", i ) ;
}
printf ( "%d", i ) ;
}
}
AUTO – example 2
main( ){
auto int i = 1 ;
{
auto int i = 2 ;
{
auto int i = 3 ;
printf ( "\n%d ", i ) ;
}
printf ( "%d ", i ) ;
}
printf ( "%d", i ) ;
}
Register – fast but limited in number
REGISTER
Storage CPU
Initial value random
Scope Local
Life Till within the scope
REGISTER – example 1
main( ){
register int i ;
for ( i = 1 ; i <= 10 ; i++ )
printf ( "\n%d", i ) ;
}
STATIC – value persists and shared
among functions
STATIC
Storage memory
Initial value zero
Scope Local
Life value persists among
different functions
Extern - Global variables
EXTERN
Storage memory
Initial value zero
Scope global
Life as long as the
program
Global – example 1
int i ;
increment( ) {i++; printf ( "i = %d\n", i ) ; }
decrement( ){i--; printf ( "i = %d\n", i ) ;}
main( ){
printf ( "\n i = %d", i ) ;
increment( ) ; increment( ) ;
decrement( ) ; decrement( ) ;
}
What is the difference between
Auto versus Register variable
Static and Global variables