SlideShare a Scribd company logo
1 of 31
User Defined
Functions
Classifications of function
 Functions can be classified as
Pre defined function:
Function whose definitions have already been return
inside c(Ex:printf())
User Defined Functions:
Functions which are designed by the
user{Ex:disp(),show()}
Top- to – bottom modular
programming :

Main Program
Function 1 Function 2 Function 3
Function 2.1Function 1.1
Understanding the Flow
of Program
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf(“n” I am a Programmer ?”n");
getch();
}
Understanding the Flow
of Program

main()
{
…………..
………….. display();
…………
………..
return0;
}
display()
{
StatementBlock;
}
Class work
 Write a C program that calculates Volume of cylinder
using functions
 Your user defined function’s name should be
volume()
 Get the values of Radius and height inside the user
defined function
 volume=( Π x r2 x h )
Elements of user defined
function
A user defined function must have the following elements
 Function Declaration
 Function definition
 Function call
Defining a function
 A function definition otherwise called as function
implementation must have the following elements
 Function name
 Function type
 List of parameters
 Local variable declaration
 Function statements
 A return statement (for types other than void)
General Form of function
function_type function_name(parameter list)
{
local variable declaration;
executable statement 1;
executable statement 2;
.
.
.
return statement;
}
Function header
Function Body
Formal parameters/
Arguments
 Parameters are nothing but variables that are passed along
with the functions
 It is not mandatory to use parameters for every user defined
functions
Ex:
void add(int a, int b) //a&b –Formal parameter
{
…..
…..
}
Example using parameters
void add(int x, int y); //declaration
void main()
{
int x,y;
scanf(“%d%d”,&x,&y);
add(x,y); // function call//formal parameter
}
void add(int x,int y) //fn with arguments//actual
parameter
{
int z;
z=x+y;
printf(“sum of %d and %d is %d”,x,y,z);
}
11
variety of the function calls
add(20,5);
add(y,10);
add(10,z);
add(p,q);
add(p*5,q-10);
Classifications of function
 Based on arguments and return type, functions are
classified as four
1. Without argument without return value
2. with arguments and without return value
3. without argument and with return value
4. with value and with return value
No argument and no return
type
void main()
{
void add();
add();
}
void add()
{
…
…
}
With argument and without
return type
void main()
{
void add(int a, int b);
int p,q; scanf(“%d%d”,&p,&q);
add(p,q);
}
void add(int x, int y)
{
…
…
}
Without argument and with
return type
void main()
{
int add();
add();
}
int add()
{
…
…
return 0;
}
With argument and with
return type
void main()
{
float add(int a, int b);
int p,q; scanf(“%d%d”,&p,&q);
add(p,q);
}
float add(int x, int y)
{
…
…
return 1;
}
Example
Void main()
{
int a=15,b=20;
void add(int , int );
add(a,b); // a and b actual parameters
}
add(int p, int q) // p and q Formal Parameters
{
int c;
c=p,q;
printf(“The sum of %d and %d is %d”,p,q,c);
}
Nesting of functions
 C Permits nesting of function
Ex:
void main()
{
fn1();
}
void fn1()
{
fn2();
}
void fn2()
{
fn3();
}
Recursion
 When a function calls itself, then the process is known
as recursive function
 In other words, if the calling function and the called
function are same, then the process is called as
recursion and that function is called as recursive
function
Ex:
void main()
{
Printf(“Main Function is Executed “);
main();
}
Use of recursion
Ex:
factorial(int i)
{
int f;
if(i==1)
return 1;
else
f=i*factorial(i-1);
return(f);
}
Classification of variables-
Storage classes
 Previously variables are classified based on their
datatype, scope(Lifetime)
 Variables can also be classified based on their storage
classes
 The Four storage classes are
 Automatic
 Static
 External
 Register
Automatic Variable
 This variable is created when a function is created and
will be destroyed at the end of the function
 Automatic Variables are typically private(Local
Variable)
 If a storage class for a variable is not specified, the
compiler by default will consider it as Automatic
 The keyword used for specification of Automatic
variable declaration is auto
Example-Automatic Variable
void main()
{
int v=50;
printf(“%d”,v);
}
int fn1()
{
auto int v=700;
printf(“%d”,v);
}
int fn2()
{
int v=17000;
printf(“%d”,v);
}
External Variable
 External variables are nothing but global variables
which can be accessed throughout the program
 They are declared outside of all the functions
EX:
int n=500;
void main()
{
}
void fn1()
{
}
//global vs local variable
int x=50;
void main()
{
int x=100;
printf(“%d”,x);
}
void fn1()
{
int x=600;
printf(“%d”,x);
}
• When Global and variable and local
variable have the same name, local
Variable Will have precedence over
Global
Using extern keyword
Extern Keyword can be used in the following method
void main()
{
extern int y; //External Variables cant be initialized
printf(“%d”,y+50); //100
fn();
}
void fn()
{
extern int y;
printf(“%d”,y); //50
}
int y=50;
Static Variable
 Static variables are the variables whose value will
persist till the end of the program
 Keyword used is static
 In other words the life of a static variable will be up to
the end of the program not end of the function
Example
Void inc();
Void main()
{
int k;
for(k=0; k<5; k++)
inc();
}
Void inc()
{
static int x=5;
printf(“X:%dn”,x+5);
}
Output:
X:10
X:15
X:20
X:25
X:30
Register variable
 A variable prefixed with keyword register will be placed
in register memory instead of main memory
 Register variables can be accessed faster than the
normal variable that is stored in main memory
Ex: register int x;
 Most frequently used variables can be declared as
register(Loop control variable)
End of Module-9

More Related Content

What's hot

Decision making and branching
Decision making and branchingDecision making and branching
Decision making and branchingSaranya saran
 
C Programming Language Part 7
C Programming Language Part 7C Programming Language Part 7
C Programming Language Part 7Rumman Ansari
 
Expressions using operator in c
Expressions using operator in cExpressions using operator in c
Expressions using operator in cSaranya saran
 
Introduction to Computer and Programing - Lecture 04
Introduction to Computer and Programing - Lecture 04Introduction to Computer and Programing - Lecture 04
Introduction to Computer and Programing - Lecture 04hassaanciit
 
C Programming Language Part 6
C Programming Language Part 6C Programming Language Part 6
C Programming Language Part 6Rumman Ansari
 
Unit 5 Foc
Unit 5 FocUnit 5 Foc
Unit 5 FocJAYA
 
1 introducing c language
1  introducing c language1  introducing c language
1 introducing c languageMomenMostafa
 
Programming Fundamentals Decisions
Programming Fundamentals  Decisions Programming Fundamentals  Decisions
Programming Fundamentals Decisions imtiazalijoono
 
Programming Fundamentals Arrays and Strings
Programming Fundamentals   Arrays and Strings Programming Fundamentals   Arrays and Strings
Programming Fundamentals Arrays and Strings imtiazalijoono
 
C Programming Language Step by Step Part 2
C Programming Language Step by Step Part 2C Programming Language Step by Step Part 2
C Programming Language Step by Step Part 2Rumman Ansari
 
C programming pointer
C  programming pointerC  programming pointer
C programming pointerargusacademy
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4MKalpanaDevi
 
parameter passing in c#
parameter passing in c#parameter passing in c#
parameter passing in c#khush_boo31
 
Concepts of C [Module 2]
Concepts of C [Module 2]Concepts of C [Module 2]
Concepts of C [Module 2]Abhishek Sinha
 

What's hot (20)

Decision making and branching
Decision making and branchingDecision making and branching
Decision making and branching
 
Function in c
Function in cFunction in c
Function in c
 
C Programming Language Part 7
C Programming Language Part 7C Programming Language Part 7
C Programming Language Part 7
 
Expressions using operator in c
Expressions using operator in cExpressions using operator in c
Expressions using operator in c
 
Introduction to Computer and Programing - Lecture 04
Introduction to Computer and Programing - Lecture 04Introduction to Computer and Programing - Lecture 04
Introduction to Computer and Programing - Lecture 04
 
Unit 4 (1)
Unit 4 (1)Unit 4 (1)
Unit 4 (1)
 
C Programming Language Part 6
C Programming Language Part 6C Programming Language Part 6
C Programming Language Part 6
 
Unit 5 Foc
Unit 5 FocUnit 5 Foc
Unit 5 Foc
 
Function
FunctionFunction
Function
 
1 introducing c language
1  introducing c language1  introducing c language
1 introducing c language
 
C function
C functionC function
C function
 
Programming Fundamentals Decisions
Programming Fundamentals  Decisions Programming Fundamentals  Decisions
Programming Fundamentals Decisions
 
Functions struct&union
Functions struct&unionFunctions struct&union
Functions struct&union
 
Programming Fundamentals Arrays and Strings
Programming Fundamentals   Arrays and Strings Programming Fundamentals   Arrays and Strings
Programming Fundamentals Arrays and Strings
 
C Programming Language Step by Step Part 2
C Programming Language Step by Step Part 2C Programming Language Step by Step Part 2
C Programming Language Step by Step Part 2
 
C programming pointer
C  programming pointerC  programming pointer
C programming pointer
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
parameter passing in c#
parameter passing in c#parameter passing in c#
parameter passing in c#
 
Function in c program
Function in c programFunction in c program
Function in c program
 
Concepts of C [Module 2]
Concepts of C [Module 2]Concepts of C [Module 2]
Concepts of C [Module 2]
 

Similar to function in c (20)

Presentation on function
Presentation on functionPresentation on function
Presentation on function
 
Dti2143 chapter 5
Dti2143 chapter 5Dti2143 chapter 5
Dti2143 chapter 5
 
Function in c program
Function in c programFunction in c program
Function in c program
 
Function in C program
Function in C programFunction in C program
Function in C program
 
Function in c
Function in cFunction in c
Function in c
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
 
Recursion in C
Recursion in CRecursion in C
Recursion in C
 
C and C++ functions
C and C++ functionsC and C++ functions
C and C++ functions
 
Fucntions & Pointers in C
Fucntions & Pointers in CFucntions & Pointers in C
Fucntions & Pointers in C
 
unit_2.pptx
unit_2.pptxunit_2.pptx
unit_2.pptx
 
functions
functionsfunctions
functions
 
6. function
6. function6. function
6. function
 
function_v1.ppt
function_v1.pptfunction_v1.ppt
function_v1.ppt
 
function_v1.ppt
function_v1.pptfunction_v1.ppt
function_v1.ppt
 
An imperative study of c
An imperative study of cAn imperative study of c
An imperative study of c
 
Functions in C.pptx
Functions in C.pptxFunctions in C.pptx
Functions in C.pptx
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Functions
FunctionsFunctions
Functions
 
Functionincprogram
FunctionincprogramFunctionincprogram
Functionincprogram
 
Unit 3 (1)
Unit 3 (1)Unit 3 (1)
Unit 3 (1)
 

Recently uploaded

ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 

Recently uploaded (20)

ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 

function in c

  • 2. Classifications of function  Functions can be classified as Pre defined function: Function whose definitions have already been return inside c(Ex:printf()) User Defined Functions: Functions which are designed by the user{Ex:disp(),show()}
  • 3. Top- to – bottom modular programming :  Main Program Function 1 Function 2 Function 3 Function 2.1Function 1.1
  • 4. Understanding the Flow of Program #include<stdio.h> #include<conio.h> void main() { clrscr(); printf(“n” I am a Programmer ?”n"); getch(); }
  • 5. Understanding the Flow of Program  main() { ………….. ………….. display(); ………… ……….. return0; } display() { StatementBlock; }
  • 6. Class work  Write a C program that calculates Volume of cylinder using functions  Your user defined function’s name should be volume()  Get the values of Radius and height inside the user defined function  volume=( Π x r2 x h )
  • 7. Elements of user defined function A user defined function must have the following elements  Function Declaration  Function definition  Function call
  • 8. Defining a function  A function definition otherwise called as function implementation must have the following elements  Function name  Function type  List of parameters  Local variable declaration  Function statements  A return statement (for types other than void)
  • 9. General Form of function function_type function_name(parameter list) { local variable declaration; executable statement 1; executable statement 2; . . . return statement; } Function header Function Body
  • 10. Formal parameters/ Arguments  Parameters are nothing but variables that are passed along with the functions  It is not mandatory to use parameters for every user defined functions Ex: void add(int a, int b) //a&b –Formal parameter { ….. ….. }
  • 11. Example using parameters void add(int x, int y); //declaration void main() { int x,y; scanf(“%d%d”,&x,&y); add(x,y); // function call//formal parameter } void add(int x,int y) //fn with arguments//actual parameter { int z; z=x+y; printf(“sum of %d and %d is %d”,x,y,z); } 11
  • 12. variety of the function calls add(20,5); add(y,10); add(10,z); add(p,q); add(p*5,q-10);
  • 13. Classifications of function  Based on arguments and return type, functions are classified as four 1. Without argument without return value 2. with arguments and without return value 3. without argument and with return value 4. with value and with return value
  • 14. No argument and no return type void main() { void add(); add(); } void add() { … … }
  • 15. With argument and without return type void main() { void add(int a, int b); int p,q; scanf(“%d%d”,&p,&q); add(p,q); } void add(int x, int y) { … … }
  • 16. Without argument and with return type void main() { int add(); add(); } int add() { … … return 0; }
  • 17. With argument and with return type void main() { float add(int a, int b); int p,q; scanf(“%d%d”,&p,&q); add(p,q); } float add(int x, int y) { … … return 1; }
  • 18. Example Void main() { int a=15,b=20; void add(int , int ); add(a,b); // a and b actual parameters } add(int p, int q) // p and q Formal Parameters { int c; c=p,q; printf(“The sum of %d and %d is %d”,p,q,c); }
  • 19. Nesting of functions  C Permits nesting of function Ex: void main() { fn1(); } void fn1() { fn2(); } void fn2() { fn3(); }
  • 20. Recursion  When a function calls itself, then the process is known as recursive function  In other words, if the calling function and the called function are same, then the process is called as recursion and that function is called as recursive function Ex: void main() { Printf(“Main Function is Executed “); main(); }
  • 21. Use of recursion Ex: factorial(int i) { int f; if(i==1) return 1; else f=i*factorial(i-1); return(f); }
  • 22. Classification of variables- Storage classes  Previously variables are classified based on their datatype, scope(Lifetime)  Variables can also be classified based on their storage classes  The Four storage classes are  Automatic  Static  External  Register
  • 23. Automatic Variable  This variable is created when a function is created and will be destroyed at the end of the function  Automatic Variables are typically private(Local Variable)  If a storage class for a variable is not specified, the compiler by default will consider it as Automatic  The keyword used for specification of Automatic variable declaration is auto
  • 24. Example-Automatic Variable void main() { int v=50; printf(“%d”,v); } int fn1() { auto int v=700; printf(“%d”,v); } int fn2() { int v=17000; printf(“%d”,v); }
  • 25. External Variable  External variables are nothing but global variables which can be accessed throughout the program  They are declared outside of all the functions EX: int n=500; void main() { } void fn1() { }
  • 26. //global vs local variable int x=50; void main() { int x=100; printf(“%d”,x); } void fn1() { int x=600; printf(“%d”,x); } • When Global and variable and local variable have the same name, local Variable Will have precedence over Global
  • 27. Using extern keyword Extern Keyword can be used in the following method void main() { extern int y; //External Variables cant be initialized printf(“%d”,y+50); //100 fn(); } void fn() { extern int y; printf(“%d”,y); //50 } int y=50;
  • 28. Static Variable  Static variables are the variables whose value will persist till the end of the program  Keyword used is static  In other words the life of a static variable will be up to the end of the program not end of the function
  • 29. Example Void inc(); Void main() { int k; for(k=0; k<5; k++) inc(); } Void inc() { static int x=5; printf(“X:%dn”,x+5); } Output: X:10 X:15 X:20 X:25 X:30
  • 30. Register variable  A variable prefixed with keyword register will be placed in register memory instead of main memory  Register variables can be accessed faster than the normal variable that is stored in main memory Ex: register int x;  Most frequently used variables can be declared as register(Loop control variable)