08 Mar 2022: Call by Value/Reference; Storage classes
PROGRAMMING FOR
PROBLEM SOLVING (PPS)
B.Tech I Sem CST
Today’s Topics
 Functions:
 call by value
 call by reference
 Storage class specifiers:
 auto, extern, register, static
 Recursion
Passing arguments to a function
 There are two ways of passing arguments to a function:
 Call by value
 Call by reference
1. Call by value:
 In call by value method, the value of the variable is passed to the function as
parameter.
 The value of the actual parameter can not be modified by formal parameter.
 Different Memory is allocated for both actual and formal parameters.
Because, value of actual parameter is copied to formal parameter.
Note:
 Actual parameter – This is the argument which is used in function call.
 Formal parameter – This is the argument which is used in function definition
Call by reference
 In call by reference , the address of the variable is
passed to the function as parameter.
 The value of the actual parameter can be modified
by formal parameter.
 Same memory is used for both actual and formal
parameters since only address is used by both
parameters.
Call by Value
void swap(int a,int b);
swap(a,b);
void swap(int a,int b)
{ .... }
void swap(int *a, int *b);
swap(&a,&b);
void swap(int *a,int *b)
{ .... }
Call by Reference
Scope Rules
 The scope of an identifier is the portion of the program in
which the identifier can be referenced.
 Some identifiers can be referenced throughout program.
 Others can be referenced from only portions of a
program.
 Ex: When we declare a local variable in a block, it can
be referenced only in that block or in blocks nested within
that block.
Scope Rules
 Global variable
 declared very early stage
 available at all times from
anywhere
 created at the start of the
program, and lasts until
the end
 difficult to debug
 Local variables
 simply created when they
are needed,
 only available from within
the function in which they
were created
 memory requirement is less
 easy to debug
Global and Local declarations
void func( float );
const int a = 17;
int b=123;
int c=456;
int main()
{
b = 4;
c = 6;
printf("na=%d",a);
printf("nb=%d",b);
printf("nc=%d",c);
func(42.8);
return 0;
}
void func( float d)
{
float b;
b = 2.3;
printf("na=%d",a);
printf("nb=%f",b);
printf("nc=%d",c);
printf("nd=%f",d);
}
a=17
b=4
c=6
a=17
b=2.300000
c=6
d=42.799999
14
How global variables work?
int val;
int fun1(void)
{ val += 5;
return val;
}
int fun2(void)
{ int val=0;
return val;
}
int fun3(void)
{ val+=5;
return val;
}
void main()
{ val = 5;
printf(“val= %dn”,val);
printf(“val= %dn”,fun1());
printf(“val= %dn”,fun2());
printf(“val= %dn”,fun3());
}
Output:
val=5
val=10
val=0
val=15
Life time of a variable
• Local variables: variables declared inside a function or
variables declared as function parameter.
• When function is called, memory will be allocated for
all local variables defined inside the function.
• Finally memory will be deallocated when the function
exits.
• The period of time a variable will be “alive” while the
function is executing is called “lifetime” of this
variable.
Storage Classes
• Every variable in C programming has two properties: type and
storage class.
• Type refers to the data type of variable whether it is character or
integer or floating-point value etc.
• Storage class determines how long it stays in existence.
• There are 4 types of storage class:
– Automatic
– External
– Static
– Register
Storage Classes
 Set initial value of a variable or if not
specified then setting it to default value.
 Defining scope of a variable.
 To determine the life of a variable.
 Four types: auto, extern, register, static
Automatic Storage Class
 Keyword : auto
 Storage Location : Main memory
 Initial Value : Garbage Value
 Life : Control remains in a block where it is
defined.
 Scope : Local to the block in which variable is
declared.
Syntax : auto [data_type] [variable_name];
Example : auto int a;
void main()
{
auto int i=10;
{
auto int i=20;
printf("nt %d",i);
}
printf("nnt %d",i);
}
External Storage Class
 Keyword : extern
 Storage Location : Main memory
 Initial Value : Zero
 Life : Until the program ends.
 Scope : Global to the program.
Syntax : extern [data_type] [variable_name];
Example : extern int a;
Program:
extern int i=10;
void main()
{
int i=20;
void show(void);
printf("nt %d",i);
show();
}
void show(void)
{
printf("nnt %d",i);
}
22
extern Example:
int num = 75 ;
void display();
void main()
{
extern int num ;
printf(“nNum : %d",num);
display();
}
void display()
{
extern int num ;
printf(“nNum : %d",num);
}
OUTPUT
Num : 75
Num : 75
File1.c
main()
{
extern int var;
var=200;
display();
}
File2.c
#include<File1.c>
int var;
void display()
{
printf("var=%d",var);
}
Register Storage Class
 Keyword : register
 Storage Location : CPU Register
 Initial Value : Garbage
 Life : Local to the block in which variable is
declared.
 Scope : Local to the block.
Syntax : register [data_type] [variable_name];
Example : register int a;
void main()
{
register int i=10;
{
register int i=20;
printf("nt %d",i);
}
printf("nnt %d",i);
}
Static Storage Class
 Keyword : static
 Storage Location : Main memory
 Initial Value : Zero and can be initialize once only.
 Life : depends on function calls and the whole
application or program.
 Scope : Local to the block.
Syntax : static [data_type] [variable_name];
Example : static int a;
void main()
{
int i;
void incre(void);
for (i=0; i<3; i++)
incre();
}
void incre(void)
{
int avar=1;
static int svar=1;
avar++;
svar++;
printf(“Auto var value : %d",avar);
printf("Static var value : %d",svar);
}
OUTPUT
Auto var value : 2
Static var value : 2
Auto var value : 2
Static var value : 3
Auto var value : 2
Static var value : 4
28
Examples static
int main()
{
int i;
for(i =1; i<=4; i++)
stat();
}
void stat()
{
static int x=0;
x = x+1;
printf(“x = %d”,x);
}
Output
x=1
x=2
x=3
x=4
static: Example
void test(); //Function declaration (discussed in next topic)
main()
{
test();
test();
test();
}
void test()
{
static int a = 0; //Static variable
a = a+1;
printf("%dt",a);
}
output :
1 2 3
Recursion
 The recursive function is
 a kind of function that calls itself, or
 a function that is part of a cycle in the sequence of
function calls.
f1 f1 f2 fn
…
Ex:
recursive Function
int multiply(int m,int n)
{
int ans;
if(n==1)
ans = m;
else
ans = m + multiply(m, n-1);
return (ans);
}
Recursion Ex: Factorial
int factorial(int i)
{
if(i <= 1)
{
return 1;
}
return i * factorial(i - 1);
}
Sum of n natural no’s using recursion
int sum(int n)
{
if(n!=0)
return n+sum(n-1);
else
return n;
}
Recursion Ex: Fibonacci
void fibonacci(int n)
{
static int n1=0,n2=1,n3;
if(n>0)
{
n3=n1+n2;
n1=n2;
n2=n3;
printf("%3d",n3);
fibonacci(n-1);
}
}
int main()
{
int n;
printf("Enter n:");
scanf("%d",&n);
printf("%3d %3d",0,1);
fibonacci(n-2);
return 0;
}
A Classical Case: Towers of Hanoi
• The towers of Hanoi problem involves moving a
number of disks (in different sizes) from one
tower (or called “peg”) to another.
– The constraint is that the larger disk can never be
placed on top of a smaller disk.
– Only one disk can be moved at each time
– Assume there are three towers available.
Towers of Hanoi
Towers of Hanoi
void TOH(int n, char s, char d, char i)
{
if (n == 1)
{
printf("n Move disk 1 from tower %c to %c", s, d);
return;
}
TOH(n-1, s, i, d);
printf("n Move disk %d from tower %c to %c", n, s, d);
TOH(n-1, i, d, s);
}
10-40
A Classical Case: Towers of Hanoi
The execution result of calling TOH(n, 'A','C','B');
C Programming Storage classes, Recursion

C Programming Storage classes, Recursion

  • 1.
    08 Mar 2022:Call by Value/Reference; Storage classes PROGRAMMING FOR PROBLEM SOLVING (PPS) B.Tech I Sem CST
  • 2.
    Today’s Topics  Functions: call by value  call by reference  Storage class specifiers:  auto, extern, register, static  Recursion
  • 3.
    Passing arguments toa function  There are two ways of passing arguments to a function:  Call by value  Call by reference 1. Call by value:  In call by value method, the value of the variable is passed to the function as parameter.  The value of the actual parameter can not be modified by formal parameter.  Different Memory is allocated for both actual and formal parameters. Because, value of actual parameter is copied to formal parameter. Note:  Actual parameter – This is the argument which is used in function call.  Formal parameter – This is the argument which is used in function definition
  • 4.
    Call by reference In call by reference , the address of the variable is passed to the function as parameter.  The value of the actual parameter can be modified by formal parameter.  Same memory is used for both actual and formal parameters since only address is used by both parameters.
  • 5.
    Call by Value voidswap(int a,int b); swap(a,b); void swap(int a,int b) { .... } void swap(int *a, int *b); swap(&a,&b); void swap(int *a,int *b) { .... } Call by Reference
  • 6.
    Scope Rules  Thescope of an identifier is the portion of the program in which the identifier can be referenced.  Some identifiers can be referenced throughout program.  Others can be referenced from only portions of a program.  Ex: When we declare a local variable in a block, it can be referenced only in that block or in blocks nested within that block.
  • 7.
    Scope Rules  Globalvariable  declared very early stage  available at all times from anywhere  created at the start of the program, and lasts until the end  difficult to debug  Local variables  simply created when they are needed,  only available from within the function in which they were created  memory requirement is less  easy to debug
  • 8.
    Global and Localdeclarations void func( float ); const int a = 17; int b=123; int c=456; int main() { b = 4; c = 6; printf("na=%d",a); printf("nb=%d",b); printf("nc=%d",c); func(42.8); return 0; } void func( float d) { float b; b = 2.3; printf("na=%d",a); printf("nb=%f",b); printf("nc=%d",c); printf("nd=%f",d); } a=17 b=4 c=6 a=17 b=2.300000 c=6 d=42.799999
  • 9.
    14 How global variableswork? int val; int fun1(void) { val += 5; return val; } int fun2(void) { int val=0; return val; } int fun3(void) { val+=5; return val; } void main() { val = 5; printf(“val= %dn”,val); printf(“val= %dn”,fun1()); printf(“val= %dn”,fun2()); printf(“val= %dn”,fun3()); } Output: val=5 val=10 val=0 val=15
  • 10.
    Life time ofa variable • Local variables: variables declared inside a function or variables declared as function parameter. • When function is called, memory will be allocated for all local variables defined inside the function. • Finally memory will be deallocated when the function exits. • The period of time a variable will be “alive” while the function is executing is called “lifetime” of this variable.
  • 11.
    Storage Classes • Everyvariable in C programming has two properties: type and storage class. • Type refers to the data type of variable whether it is character or integer or floating-point value etc. • Storage class determines how long it stays in existence. • There are 4 types of storage class: – Automatic – External – Static – Register
  • 12.
    Storage Classes  Setinitial value of a variable or if not specified then setting it to default value.  Defining scope of a variable.  To determine the life of a variable.  Four types: auto, extern, register, static
  • 13.
    Automatic Storage Class Keyword : auto  Storage Location : Main memory  Initial Value : Garbage Value  Life : Control remains in a block where it is defined.  Scope : Local to the block in which variable is declared.
  • 14.
    Syntax : auto[data_type] [variable_name]; Example : auto int a; void main() { auto int i=10; { auto int i=20; printf("nt %d",i); } printf("nnt %d",i); }
  • 15.
    External Storage Class Keyword : extern  Storage Location : Main memory  Initial Value : Zero  Life : Until the program ends.  Scope : Global to the program.
  • 16.
    Syntax : extern[data_type] [variable_name]; Example : extern int a; Program: extern int i=10; void main() { int i=20; void show(void); printf("nt %d",i); show(); } void show(void) { printf("nnt %d",i); }
  • 17.
    22 extern Example: int num= 75 ; void display(); void main() { extern int num ; printf(“nNum : %d",num); display(); } void display() { extern int num ; printf(“nNum : %d",num); } OUTPUT Num : 75 Num : 75
  • 18.
  • 19.
    Register Storage Class Keyword : register  Storage Location : CPU Register  Initial Value : Garbage  Life : Local to the block in which variable is declared.  Scope : Local to the block.
  • 20.
    Syntax : register[data_type] [variable_name]; Example : register int a; void main() { register int i=10; { register int i=20; printf("nt %d",i); } printf("nnt %d",i); }
  • 21.
    Static Storage Class Keyword : static  Storage Location : Main memory  Initial Value : Zero and can be initialize once only.  Life : depends on function calls and the whole application or program.  Scope : Local to the block.
  • 22.
    Syntax : static[data_type] [variable_name]; Example : static int a; void main() { int i; void incre(void); for (i=0; i<3; i++) incre(); } void incre(void) { int avar=1; static int svar=1; avar++; svar++; printf(“Auto var value : %d",avar); printf("Static var value : %d",svar); } OUTPUT Auto var value : 2 Static var value : 2 Auto var value : 2 Static var value : 3 Auto var value : 2 Static var value : 4
  • 23.
    28 Examples static int main() { inti; for(i =1; i<=4; i++) stat(); } void stat() { static int x=0; x = x+1; printf(“x = %d”,x); } Output x=1 x=2 x=3 x=4
  • 24.
    static: Example void test();//Function declaration (discussed in next topic) main() { test(); test(); test(); } void test() { static int a = 0; //Static variable a = a+1; printf("%dt",a); } output : 1 2 3
  • 25.
    Recursion  The recursivefunction is  a kind of function that calls itself, or  a function that is part of a cycle in the sequence of function calls. f1 f1 f2 fn …
  • 26.
    Ex: recursive Function int multiply(intm,int n) { int ans; if(n==1) ans = m; else ans = m + multiply(m, n-1); return (ans); }
  • 27.
    Recursion Ex: Factorial intfactorial(int i) { if(i <= 1) { return 1; } return i * factorial(i - 1); }
  • 28.
    Sum of nnatural no’s using recursion int sum(int n) { if(n!=0) return n+sum(n-1); else return n; }
  • 29.
    Recursion Ex: Fibonacci voidfibonacci(int n) { static int n1=0,n2=1,n3; if(n>0) { n3=n1+n2; n1=n2; n2=n3; printf("%3d",n3); fibonacci(n-1); } } int main() { int n; printf("Enter n:"); scanf("%d",&n); printf("%3d %3d",0,1); fibonacci(n-2); return 0; }
  • 30.
    A Classical Case:Towers of Hanoi • The towers of Hanoi problem involves moving a number of disks (in different sizes) from one tower (or called “peg”) to another. – The constraint is that the larger disk can never be placed on top of a smaller disk. – Only one disk can be moved at each time – Assume there are three towers available.
  • 31.
  • 32.
    Towers of Hanoi voidTOH(int n, char s, char d, char i) { if (n == 1) { printf("n Move disk 1 from tower %c to %c", s, d); return; } TOH(n-1, s, i, d); printf("n Move disk %d from tower %c to %c", n, s, d); TOH(n-1, i, d, s); }
  • 33.
    10-40 A Classical Case:Towers of Hanoi The execution result of calling TOH(n, 'A','C','B');