SlideShare une entreprise Scribd logo
1  sur  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

Contenu connexe

Tendances

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
hassaanciit
 

Tendances (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]
 

Similaire à 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)
 

Dernier

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Dernier (20)

Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 

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)