SlideShare a Scribd company logo
By: Shobhit Upadhyay
Functions
 A function is a self contained block of code that performs a
particular task.
 Any C program can be seen as a collection/group of these
functions.
 A functions takes some data as input, perform some
operation on that data and then return a value.
 Any C program must contain at least one function, which is
main().
 There is no limit on the number of functions that might be
present in a C program.
 For ex.
main()
{
message();
printf(“I am in main”);
}
message()
{
printf(“I am in message”);
}
Function Prototype
 All Identifiers in C must be declared before they are used. This is true
for functions as well as variables.
 For functions, the declarations needs to be done before the first call of
the function.
 A function declaration specifies the name, return type, and arguments
of a function. This is also called the function prototype.
 To be a prototype, a function declaration must establish types for the
function’s arguments.
 Having the prototype available before the first use of the function
allows the compiler to check that the correct number and types of
arguments are used in the function call.
 The prototype has the same syntax as the function
definition, except that it is terminated by a semicolon
following the closing parenthesis and therefore has no
body.
 Although, functions that return int values do not require
prototypes, prototypes are recommended.
Function Definition
 General form of any function definition is:
return-type function-name(argument declarations)
{
declarations and statements
}
 Return-type refers to the data type of the value being
returned from the function. If the return type is omitted,
int is assumed.
 The values provided to a function for processing are the
arguments.
 The set of statements between the braces is called as the
function body.
Arguments – Call by Value
 In C, all functions are passed “by value” by default.
 This means that the called function is given the values of its
arguments in temporary variables rather than the originals.
 For ex. main()
{
int a=4,b=5;
sum(a,b);
printf(“Sum = %d”,a+b);
}
sum(int a,int b)
{
a++;
b++;
printf(“Sum = %d”,a+b);
}
Arguments – Call by Reference
 When necessary, it is possible to arrange a function which can modify a
variable in a calling routine.
 The caller must provide the address of the variable to be set (generally, a
pointer to a variable), and the called function must declare the parameter to
be a pointer and access the variable indirectly through it.
 For ex: main()
{
int a=4,b=5;
sum(&a,&b);
printf(“Sum = %d”,a+b);
}
sum(int *a,int *b)
{
(*a)++;
(*b)++;
printf(“Sum = %d”,(*a)+(*b));
}
Recursion
 Recursion defines a function in terms of itself.
 It is a programming technique in which a function calls
itself.
 A recursive function calls itself repeatedly, with different
argument values each time.
 Some argument values cause the recursive method to
return without calling itself. This is the base case.
 Either omitting the base case or writing the recursion step
incorrectly will cause infinite recursion (stack overflow
error).
 Recursion to find the factorial of any number:
int factorial(int x)
{
if(x<=1)
return 1;
else
return(x*factorial(x-1));
}
External Variables
 See the below example:
main()
{
extern int a;
printf(“Value of a = %d”,a);
}
int a=5;
Output: Value of a = 5
Scope Rules
 Example:
int num1 = 100; //Global Scope
static int num2 = 200; //File Scope
int main()
{
int num3 = 300; //Local Scope
if(num2>num1)
{
int i=0; //Block Scope
printf(“Value of i = %dn”,i++);
}
printf(“Value of num1 = %dn”,num1);
printf(“Value of num2 = %dn”,num2);
printf(“Value of num3 = %dn”,num3);
}
Static Variables
 Example:
static int min = 10;
int setmax()
{
static int max = 100;
return ++max;
}
main()
{
min++;
printf(“Value of max = %d”, setmax());
printf(“Value of max = %d”, setmax());
printf(“Value of min = %d”, min);
}
functionsinc-130108032745-phpapp01.pdf

More Related Content

Similar to functionsinc-130108032745-phpapp01.pdf

Similar to functionsinc-130108032745-phpapp01.pdf (20)

User defined function in C.pptx
User defined function in C.pptxUser defined function in C.pptx
User defined function in C.pptx
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
 
Recursion in C
Recursion in CRecursion in C
Recursion in C
 
FUNCTIONS IN C PROGRAMMING.pdf
FUNCTIONS IN C PROGRAMMING.pdfFUNCTIONS IN C PROGRAMMING.pdf
FUNCTIONS IN C PROGRAMMING.pdf
 
Unit 4.pdf
Unit 4.pdfUnit 4.pdf
Unit 4.pdf
 
Functions in C.pptx
Functions in C.pptxFunctions in C.pptx
Functions in C.pptx
 
Classes function overloading
Classes function overloadingClasses function overloading
Classes function overloading
 
unit3 part2 pcds function notes.pdf
unit3 part2 pcds function notes.pdfunit3 part2 pcds function notes.pdf
unit3 part2 pcds function notes.pdf
 
Chapter 5
Chapter 5Chapter 5
Chapter 5
 
Functions
FunctionsFunctions
Functions
 
C function
C functionC function
C function
 
Functions
FunctionsFunctions
Functions
 
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
 
C Programming Language Part 7
C Programming Language Part 7C Programming Language Part 7
C Programming Language Part 7
 
UNIT3.pptx
UNIT3.pptxUNIT3.pptx
UNIT3.pptx
 
unit_2.pptx
unit_2.pptxunit_2.pptx
unit_2.pptx
 
Function C programming
Function C programmingFunction C programming
Function C programming
 
Ch06
Ch06Ch06
Ch06
 
Amit user defined functions xi (2)
Amit  user defined functions xi (2)Amit  user defined functions xi (2)
Amit user defined functions xi (2)
 
Functions struct&union
Functions struct&unionFunctions struct&union
Functions struct&union
 

More from mounikanarra3

travelingsalesmanproblem-170122053648.pdf
travelingsalesmanproblem-170122053648.pdftravelingsalesmanproblem-170122053648.pdf
travelingsalesmanproblem-170122053648.pdfmounikanarra3
 
Space complexity-DAA.pptx
Space complexity-DAA.pptxSpace complexity-DAA.pptx
Space complexity-DAA.pptxmounikanarra3
 
(PAD_5)Dynamic_Programming.ppt
(PAD_5)Dynamic_Programming.ppt(PAD_5)Dynamic_Programming.ppt
(PAD_5)Dynamic_Programming.pptmounikanarra3
 
sequencediagram-150302224029-conversion-gate01 (1).pdf
sequencediagram-150302224029-conversion-gate01 (1).pdfsequencediagram-150302224029-conversion-gate01 (1).pdf
sequencediagram-150302224029-conversion-gate01 (1).pdfmounikanarra3
 
exp227-jan-170127160848 (3) (1).pdf
exp227-jan-170127160848 (3) (1).pdfexp227-jan-170127160848 (3) (1).pdf
exp227-jan-170127160848 (3) (1).pdfmounikanarra3
 
stephenhawkingppt-160402093003.pdf
stephenhawkingppt-160402093003.pdfstephenhawkingppt-160402093003.pdf
stephenhawkingppt-160402093003.pdfmounikanarra3
 

More from mounikanarra3 (15)

unit-2.pdf
unit-2.pdfunit-2.pdf
unit-2.pdf
 
Unit - 4.pptx
Unit - 4.pptxUnit - 4.pptx
Unit - 4.pptx
 
UNIT-1 (4).pdf
UNIT-1 (4).pdfUNIT-1 (4).pdf
UNIT-1 (4).pdf
 
travelingsalesmanproblem-170122053648.pdf
travelingsalesmanproblem-170122053648.pdftravelingsalesmanproblem-170122053648.pdf
travelingsalesmanproblem-170122053648.pdf
 
Space complexity-DAA.pptx
Space complexity-DAA.pptxSpace complexity-DAA.pptx
Space complexity-DAA.pptx
 
EEM MID2.PPT.pptx
EEM MID2.PPT.pptxEEM MID2.PPT.pptx
EEM MID2.PPT.pptx
 
MID2 UML (1).pptx
MID2 UML (1).pptxMID2 UML (1).pptx
MID2 UML (1).pptx
 
(PAD_5)Dynamic_Programming.ppt
(PAD_5)Dynamic_Programming.ppt(PAD_5)Dynamic_Programming.ppt
(PAD_5)Dynamic_Programming.ppt
 
sequencediagram-150302224029-conversion-gate01 (1).pdf
sequencediagram-150302224029-conversion-gate01 (1).pdfsequencediagram-150302224029-conversion-gate01 (1).pdf
sequencediagram-150302224029-conversion-gate01 (1).pdf
 
UML.PPT.pptx
UML.PPT.pptxUML.PPT.pptx
UML.PPT.pptx
 
exp227-jan-170127160848 (3) (1).pdf
exp227-jan-170127160848 (3) (1).pdfexp227-jan-170127160848 (3) (1).pdf
exp227-jan-170127160848 (3) (1).pdf
 
stephenhawkingppt-160402093003.pdf
stephenhawkingppt-160402093003.pdfstephenhawkingppt-160402093003.pdf
stephenhawkingppt-160402093003.pdf
 
CP-STRING (1).ppt
CP-STRING (1).pptCP-STRING (1).ppt
CP-STRING (1).ppt
 
Array.pdf
Array.pdfArray.pdf
Array.pdf
 
routing.pptx
routing.pptxrouting.pptx
routing.pptx
 

Recently uploaded

Scaling in conventional MOSFET for constant electric field and constant voltage
Scaling in conventional MOSFET for constant electric field and constant voltageScaling in conventional MOSFET for constant electric field and constant voltage
Scaling in conventional MOSFET for constant electric field and constant voltageRCC Institute of Information Technology
 
NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...
NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...
NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...Amil baba
 
Fruit shop management system project report.pdf
Fruit shop management system project report.pdfFruit shop management system project report.pdf
Fruit shop management system project report.pdfKamal Acharya
 
2024 DevOps Pro Europe - Growing at the edge
2024 DevOps Pro Europe - Growing at the edge2024 DevOps Pro Europe - Growing at the edge
2024 DevOps Pro Europe - Growing at the edgePaco Orozco
 
Introduction to Machine Learning Unit-4 Notes for II-II Mechanical Engineering
Introduction to Machine Learning Unit-4 Notes for II-II Mechanical EngineeringIntroduction to Machine Learning Unit-4 Notes for II-II Mechanical Engineering
Introduction to Machine Learning Unit-4 Notes for II-II Mechanical EngineeringC Sai Kiran
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationRobbie Edward Sayers
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Electivekarthi keyan
 
Halogenation process of chemical process industries
Halogenation process of chemical process industriesHalogenation process of chemical process industries
Halogenation process of chemical process industriesMuhammadTufail242431
 
RS Khurmi Machine Design Clutch and Brake Exercise Numerical Solutions
RS Khurmi Machine Design Clutch and Brake Exercise Numerical SolutionsRS Khurmi Machine Design Clutch and Brake Exercise Numerical Solutions
RS Khurmi Machine Design Clutch and Brake Exercise Numerical SolutionsAtif Razi
 
fundamentals of drawing and isometric and orthographic projection
fundamentals of drawing and isometric and orthographic projectionfundamentals of drawing and isometric and orthographic projection
fundamentals of drawing and isometric and orthographic projectionjeevanprasad8
 
Courier management system project report.pdf
Courier management system project report.pdfCourier management system project report.pdf
Courier management system project report.pdfKamal Acharya
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacksgerogepatton
 
fluid mechanics gate notes . gate all pyqs answer
fluid mechanics gate notes . gate all pyqs answerfluid mechanics gate notes . gate all pyqs answer
fluid mechanics gate notes . gate all pyqs answerapareshmondalnita
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwoodseandesed
 
LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptssuser9bd3ba
 
A case study of cinema management system project report..pdf
A case study of cinema management system project report..pdfA case study of cinema management system project report..pdf
A case study of cinema management system project report..pdfKamal Acharya
 
shape functions of 1D and 2 D rectangular elements.pptx
shape functions of 1D and 2 D rectangular elements.pptxshape functions of 1D and 2 D rectangular elements.pptx
shape functions of 1D and 2 D rectangular elements.pptxVishalDeshpande27
 
The Ultimate Guide to External Floating Roofs for Oil Storage Tanks.docx
The Ultimate Guide to External Floating Roofs for Oil Storage Tanks.docxThe Ultimate Guide to External Floating Roofs for Oil Storage Tanks.docx
The Ultimate Guide to External Floating Roofs for Oil Storage Tanks.docxCenterEnamel
 
Event Management System Vb Net Project Report.pdf
Event Management System Vb Net  Project Report.pdfEvent Management System Vb Net  Project Report.pdf
Event Management System Vb Net Project Report.pdfKamal Acharya
 

Recently uploaded (20)

Scaling in conventional MOSFET for constant electric field and constant voltage
Scaling in conventional MOSFET for constant electric field and constant voltageScaling in conventional MOSFET for constant electric field and constant voltage
Scaling in conventional MOSFET for constant electric field and constant voltage
 
NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...
NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...
NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...
 
Fruit shop management system project report.pdf
Fruit shop management system project report.pdfFruit shop management system project report.pdf
Fruit shop management system project report.pdf
 
2024 DevOps Pro Europe - Growing at the edge
2024 DevOps Pro Europe - Growing at the edge2024 DevOps Pro Europe - Growing at the edge
2024 DevOps Pro Europe - Growing at the edge
 
Introduction to Machine Learning Unit-4 Notes for II-II Mechanical Engineering
Introduction to Machine Learning Unit-4 Notes for II-II Mechanical EngineeringIntroduction to Machine Learning Unit-4 Notes for II-II Mechanical Engineering
Introduction to Machine Learning Unit-4 Notes for II-II Mechanical Engineering
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
 
Halogenation process of chemical process industries
Halogenation process of chemical process industriesHalogenation process of chemical process industries
Halogenation process of chemical process industries
 
RS Khurmi Machine Design Clutch and Brake Exercise Numerical Solutions
RS Khurmi Machine Design Clutch and Brake Exercise Numerical SolutionsRS Khurmi Machine Design Clutch and Brake Exercise Numerical Solutions
RS Khurmi Machine Design Clutch and Brake Exercise Numerical Solutions
 
fundamentals of drawing and isometric and orthographic projection
fundamentals of drawing and isometric and orthographic projectionfundamentals of drawing and isometric and orthographic projection
fundamentals of drawing and isometric and orthographic projection
 
Courier management system project report.pdf
Courier management system project report.pdfCourier management system project report.pdf
Courier management system project report.pdf
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
 
fluid mechanics gate notes . gate all pyqs answer
fluid mechanics gate notes . gate all pyqs answerfluid mechanics gate notes . gate all pyqs answer
fluid mechanics gate notes . gate all pyqs answer
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
 
LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.ppt
 
A case study of cinema management system project report..pdf
A case study of cinema management system project report..pdfA case study of cinema management system project report..pdf
A case study of cinema management system project report..pdf
 
shape functions of 1D and 2 D rectangular elements.pptx
shape functions of 1D and 2 D rectangular elements.pptxshape functions of 1D and 2 D rectangular elements.pptx
shape functions of 1D and 2 D rectangular elements.pptx
 
The Ultimate Guide to External Floating Roofs for Oil Storage Tanks.docx
The Ultimate Guide to External Floating Roofs for Oil Storage Tanks.docxThe Ultimate Guide to External Floating Roofs for Oil Storage Tanks.docx
The Ultimate Guide to External Floating Roofs for Oil Storage Tanks.docx
 
Event Management System Vb Net Project Report.pdf
Event Management System Vb Net  Project Report.pdfEvent Management System Vb Net  Project Report.pdf
Event Management System Vb Net Project Report.pdf
 

functionsinc-130108032745-phpapp01.pdf

  • 2. Functions  A function is a self contained block of code that performs a particular task.  Any C program can be seen as a collection/group of these functions.  A functions takes some data as input, perform some operation on that data and then return a value.  Any C program must contain at least one function, which is main().  There is no limit on the number of functions that might be present in a C program.
  • 3.  For ex. main() { message(); printf(“I am in main”); } message() { printf(“I am in message”); }
  • 4. Function Prototype  All Identifiers in C must be declared before they are used. This is true for functions as well as variables.  For functions, the declarations needs to be done before the first call of the function.  A function declaration specifies the name, return type, and arguments of a function. This is also called the function prototype.  To be a prototype, a function declaration must establish types for the function’s arguments.  Having the prototype available before the first use of the function allows the compiler to check that the correct number and types of arguments are used in the function call.
  • 5.  The prototype has the same syntax as the function definition, except that it is terminated by a semicolon following the closing parenthesis and therefore has no body.  Although, functions that return int values do not require prototypes, prototypes are recommended.
  • 6. Function Definition  General form of any function definition is: return-type function-name(argument declarations) { declarations and statements }  Return-type refers to the data type of the value being returned from the function. If the return type is omitted, int is assumed.  The values provided to a function for processing are the arguments.  The set of statements between the braces is called as the function body.
  • 7. Arguments – Call by Value  In C, all functions are passed “by value” by default.  This means that the called function is given the values of its arguments in temporary variables rather than the originals.  For ex. main() { int a=4,b=5; sum(a,b); printf(“Sum = %d”,a+b); } sum(int a,int b) { a++; b++; printf(“Sum = %d”,a+b); }
  • 8. Arguments – Call by Reference  When necessary, it is possible to arrange a function which can modify a variable in a calling routine.  The caller must provide the address of the variable to be set (generally, a pointer to a variable), and the called function must declare the parameter to be a pointer and access the variable indirectly through it.  For ex: main() { int a=4,b=5; sum(&a,&b); printf(“Sum = %d”,a+b); } sum(int *a,int *b) { (*a)++; (*b)++; printf(“Sum = %d”,(*a)+(*b)); }
  • 9. Recursion  Recursion defines a function in terms of itself.  It is a programming technique in which a function calls itself.  A recursive function calls itself repeatedly, with different argument values each time.  Some argument values cause the recursive method to return without calling itself. This is the base case.  Either omitting the base case or writing the recursion step incorrectly will cause infinite recursion (stack overflow error).
  • 10.  Recursion to find the factorial of any number: int factorial(int x) { if(x<=1) return 1; else return(x*factorial(x-1)); }
  • 11. External Variables  See the below example: main() { extern int a; printf(“Value of a = %d”,a); } int a=5; Output: Value of a = 5
  • 12. Scope Rules  Example: int num1 = 100; //Global Scope static int num2 = 200; //File Scope int main() { int num3 = 300; //Local Scope if(num2>num1) { int i=0; //Block Scope printf(“Value of i = %dn”,i++); } printf(“Value of num1 = %dn”,num1); printf(“Value of num2 = %dn”,num2); printf(“Value of num3 = %dn”,num3); }
  • 13. Static Variables  Example: static int min = 10; int setmax() { static int max = 100; return ++max; } main() { min++; printf(“Value of max = %d”, setmax()); printf(“Value of max = %d”, setmax()); printf(“Value of min = %d”, min); }