SlideShare a Scribd company logo
1 of 34
Download to read offline
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

More Related Content

What's hot (20)

User defined functions in C programmig
User defined functions in C programmigUser defined functions in C programmig
User defined functions in C programmig
 
C Recursion, Pointers, Dynamic memory management
C Recursion, Pointers, Dynamic memory managementC Recursion, Pointers, Dynamic memory management
C Recursion, Pointers, Dynamic memory management
 
File handling in c
File handling in cFile handling in c
File handling in c
 
Strings in c
Strings in cStrings in c
Strings in c
 
Structure in C
Structure in CStructure in C
Structure in C
 
C functions
C functionsC functions
C functions
 
Union in C programming
Union in C programmingUnion in C programming
Union in C programming
 
Arrays
ArraysArrays
Arrays
 
Structure in C language
Structure in C languageStructure in C language
Structure in C language
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
Pointers
PointersPointers
Pointers
 
Function in c
Function in cFunction in c
Function in c
 
C Structures And Unions
C  Structures And  UnionsC  Structures And  Unions
C Structures And Unions
 
Unit 9. Structure and Unions
Unit 9. Structure and UnionsUnit 9. Structure and Unions
Unit 9. Structure and Unions
 
Pointers in C Programming
Pointers in C ProgrammingPointers in C Programming
Pointers in C Programming
 
Types of function call
Types of function callTypes of function call
Types of function call
 
pointers
pointerspointers
pointers
 
Pointers C programming
Pointers  C programmingPointers  C programming
Pointers C programming
 
Multidimensional array in C
Multidimensional array in CMultidimensional array in C
Multidimensional array in C
 
Presentation on pointer.
Presentation on pointer.Presentation on pointer.
Presentation on pointer.
 

Similar to C Programming Storage classes, Recursion

Similar to C Programming Storage classes, Recursion (20)

presentation_functions_1443207686_140676.ppt
presentation_functions_1443207686_140676.pptpresentation_functions_1443207686_140676.ppt
presentation_functions_1443207686_140676.ppt
 
functions
functionsfunctions
functions
 
Functions struct&union
Functions struct&unionFunctions struct&union
Functions struct&union
 
Storage class
Storage classStorage class
Storage class
 
Fucntions & Pointers in C
Fucntions & Pointers in CFucntions & Pointers in C
Fucntions & Pointers in C
 
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptxCH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
 
Storage classes in C
Storage classes in C Storage classes in C
Storage classes in C
 
Unit 5 Foc
Unit 5 FocUnit 5 Foc
Unit 5 Foc
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
 
Presentation on function
Presentation on functionPresentation on function
Presentation on function
 
Storage classes arrays & functions in C Language
Storage classes arrays & functions in C LanguageStorage classes arrays & functions in C Language
Storage classes arrays & functions in C Language
 
Functions
FunctionsFunctions
Functions
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
Storage class
Storage classStorage class
Storage class
 
Functions in c
Functions in cFunctions in c
Functions in c
 
7 functions
7  functions7  functions
7 functions
 
46630497 fun-pointer-1
46630497 fun-pointer-146630497 fun-pointer-1
46630497 fun-pointer-1
 
function in c
function in cfunction in c
function in c
 
C functions list
C functions listC functions list
C functions list
 
Functions in C
Functions in CFunctions in C
Functions in C
 

More from Sreedhar Chowdam

Design and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture NotesDesign and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture NotesSreedhar Chowdam
 
Design and Analysis of Algorithms (Knapsack Problem)
Design and Analysis of Algorithms (Knapsack Problem)Design and Analysis of Algorithms (Knapsack Problem)
Design and Analysis of Algorithms (Knapsack Problem)Sreedhar Chowdam
 
DCCN Network Layer congestion control TCP
DCCN Network Layer congestion control TCPDCCN Network Layer congestion control TCP
DCCN Network Layer congestion control TCPSreedhar Chowdam
 
Data Communication and Computer Networks
Data Communication and Computer NetworksData Communication and Computer Networks
Data Communication and Computer NetworksSreedhar Chowdam
 
Data Communication & Computer Networks
Data Communication & Computer NetworksData Communication & Computer Networks
Data Communication & Computer NetworksSreedhar Chowdam
 
PPS Arrays Matrix operations
PPS Arrays Matrix operationsPPS Arrays Matrix operations
PPS Arrays Matrix operationsSreedhar Chowdam
 
Programming for Problem Solving
Programming for Problem SolvingProgramming for Problem Solving
Programming for Problem SolvingSreedhar Chowdam
 
Python Programming: Lists, Modules, Exceptions
Python Programming: Lists, Modules, ExceptionsPython Programming: Lists, Modules, Exceptions
Python Programming: Lists, Modules, ExceptionsSreedhar Chowdam
 
Python Programming by Dr. C. Sreedhar.pdf
Python Programming by Dr. C. Sreedhar.pdfPython Programming by Dr. C. Sreedhar.pdf
Python Programming by Dr. C. Sreedhar.pdfSreedhar Chowdam
 
Python Programming Strings
Python Programming StringsPython Programming Strings
Python Programming StringsSreedhar Chowdam
 
Programming For Problem Solving Lecture Notes
Programming For Problem Solving Lecture NotesProgramming For Problem Solving Lecture Notes
Programming For Problem Solving Lecture NotesSreedhar Chowdam
 
Data Structures Notes 2021
Data Structures Notes 2021Data Structures Notes 2021
Data Structures Notes 2021Sreedhar Chowdam
 
Computer Networks Lecture Notes 01
Computer Networks Lecture Notes 01Computer Networks Lecture Notes 01
Computer Networks Lecture Notes 01Sreedhar Chowdam
 
Dbms university library database
Dbms university library databaseDbms university library database
Dbms university library databaseSreedhar Chowdam
 

More from Sreedhar Chowdam (20)

Design and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture NotesDesign and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture Notes
 
Design and Analysis of Algorithms (Knapsack Problem)
Design and Analysis of Algorithms (Knapsack Problem)Design and Analysis of Algorithms (Knapsack Problem)
Design and Analysis of Algorithms (Knapsack Problem)
 
DCCN Network Layer congestion control TCP
DCCN Network Layer congestion control TCPDCCN Network Layer congestion control TCP
DCCN Network Layer congestion control TCP
 
Data Communication and Computer Networks
Data Communication and Computer NetworksData Communication and Computer Networks
Data Communication and Computer Networks
 
DCCN Unit 1.pdf
DCCN Unit 1.pdfDCCN Unit 1.pdf
DCCN Unit 1.pdf
 
Data Communication & Computer Networks
Data Communication & Computer NetworksData Communication & Computer Networks
Data Communication & Computer Networks
 
PPS Notes Unit 5.pdf
PPS Notes Unit 5.pdfPPS Notes Unit 5.pdf
PPS Notes Unit 5.pdf
 
PPS Arrays Matrix operations
PPS Arrays Matrix operationsPPS Arrays Matrix operations
PPS Arrays Matrix operations
 
Programming for Problem Solving
Programming for Problem SolvingProgramming for Problem Solving
Programming for Problem Solving
 
Big Data Analytics Part2
Big Data Analytics Part2Big Data Analytics Part2
Big Data Analytics Part2
 
Python Programming: Lists, Modules, Exceptions
Python Programming: Lists, Modules, ExceptionsPython Programming: Lists, Modules, Exceptions
Python Programming: Lists, Modules, Exceptions
 
Python Programming by Dr. C. Sreedhar.pdf
Python Programming by Dr. C. Sreedhar.pdfPython Programming by Dr. C. Sreedhar.pdf
Python Programming by Dr. C. Sreedhar.pdf
 
Python Programming Strings
Python Programming StringsPython Programming Strings
Python Programming Strings
 
Python Programming
Python Programming Python Programming
Python Programming
 
Python Programming
Python ProgrammingPython Programming
Python Programming
 
Programming For Problem Solving Lecture Notes
Programming For Problem Solving Lecture NotesProgramming For Problem Solving Lecture Notes
Programming For Problem Solving Lecture Notes
 
Big Data Analytics
Big Data AnalyticsBig Data Analytics
Big Data Analytics
 
Data Structures Notes 2021
Data Structures Notes 2021Data Structures Notes 2021
Data Structures Notes 2021
 
Computer Networks Lecture Notes 01
Computer Networks Lecture Notes 01Computer Networks Lecture Notes 01
Computer Networks Lecture Notes 01
 
Dbms university library database
Dbms university library databaseDbms university library database
Dbms university library database
 

Recently uploaded

(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 

Recently uploaded (20)

9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 

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 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
  • 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 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
  • 6. 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.
  • 7. 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
  • 8. 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
  • 9. 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
  • 10. 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.
  • 11. 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
  • 12. 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
  • 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
  • 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() { 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
  • 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 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 …
  • 26. 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); }
  • 27. Recursion Ex: Factorial int factorial(int i) { if(i <= 1) { return 1; } return i * factorial(i - 1); }
  • 28. Sum of n natural no’s using recursion int sum(int n) { if(n!=0) return n+sum(n-1); else return n; }
  • 29. 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; }
  • 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.
  • 32. 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); }
  • 33. 10-40 A Classical Case: Towers of Hanoi The execution result of calling TOH(n, 'A','C','B');