SlideShare una empresa de Scribd logo
1 de 23
Function

A function is a set of
statements, written in a
particular language that
performs a particular
task ,when ever called.
Types of functions
1.Inbuilt or system defined or Library Functions

2.User defined Functions
Inbuilt functions
These are the functions whose defintions is
already defined and stored in a respective
header files of c library .
Example of this type of functions are
gets(),puts(),clrscr(),getche(),printf(),scanf().
Etc.
User defined functions
 The functions which are created by the user

to, perform the desired task,are called user
defined functions.
User defined functions provide the following
Benefits:1.By using user defined functions,we can divide
a large problem into small independence
modules,each having a defined purpose.the
process is called modulation.
benefits
2.Smaller modules are easy to design and
debug.
3.We can create own user defined functions,in
the way,a function can be created once and
used for many times.i.e sharing of the code.
4Modulation provide greater clarity to the
programs.
Defining a function
Syntax :<return type><name>(parameterlist)>
{
Statement 1;
Statement 2;
----------------Statement n;
Return (expression);
}
Elements of functions
1. Return type :- is datatype of the value ,returned
by the function .void is used when the function
does not return any value,int and float are used
when functions returns integer or floating point
values.
2.Name:-is the name of the function
3.Parameter list:-contain variables carrying
information from the main function.
4 Function call is represented by writing the name
of the function followed by braces and
semicolon;
Example describes a function to calc sum of two
no.
Int sum(int a,int b)
{
// function body
Printf(“the sum of no is being calculated”);
Return(a+b);
}
Void main()
{
Int j=0;
Int k=10;
Int m=12;
J=sum(k,m);
//call to sum function
Printf(“the sum is %d”,j);
}
Return type of the function is “int”.the name of
the function is “sum” .the parameter are” a”
and “b” of integer type.

C allows us to define user defined function in
various ways.we can define the functions in
following ways:
1. Functions with no argument and no return
type.
2. Function with argument and no return type.
3. Functions with argument and with return type
like int ,float.
Functions with no argument
and no return type.
These are the simple function which
do not take any data from calling
functions, in the form of parameters
and do not return anything to calling
functions, after execution.
The example describes the use of func.
with no parameter and no return type

Simple function.
#include<stdio.h>
#include<conio.h>
Void main()
{
void punjab();
//function prototyping
Void delhi();
//function prototyping
Printf(“n I m from main”);
Punjab();
// call to punjab()
Printf(“nt I m back in main”);
Himachal();
Print f(“nt t I m from main again”);
}
Void punjab()
{
Printf(“n I m from punjab”);
}
Void himachal()
{
Print(“n I m from himachal”);
}
output
I m from main
I m from punjab
I m back in main
I m from himchal
I m from main again
The example describes the use of func.
with no parameter and return type

Example to calculate the sum of two numbers
using
#include<stdio.h>
#include<conio.h>
Void main()
{
Int sum();
Int result();
Clrscr();
Printf(“calculate sum of two numbers “);
Getch();
Result=sum();
call to function sum
//result contains the value entered
Printf(“n the result after adding is number
%d”,result);
Getch();
}
Int sum()
body of the function
{
Int a,b;
Printf(“n enter 2 numbers”);
Scanf(“%d%d”,&a,&b);
Return(a+b);
}
Out put
Ready to calculate sum of two numbers-press
any key
Enter 2 number 12
23
The result after adding numbers is 35
Functions with argu and no return type
 To demonstrate the functions with parameter and

no return
#include<stdio.h>
#include<conio.h>
Void main()
{
Void sum(int,int);
Int a,b;
Printf(“enter 2 numbers”);
Scanf(“%d%”,&a,&b);
Sum(a,b);
Getch();
} //end of main
Void sum(int f1,int f2)
{
Int result;
//variable in function
Result=(f1+f2);
Printf(“n the result after addition is%d”,result);
}
Out put
Enter 2 numbers 10 10
The result after addition is 20
Func with argu and return type
#include<stdio.h>
#include<conio.h>
Void main()
{
Int sum(int,int);
Int a,b,ans;
Printf(“enter 2 numbers”);
Sanf(“%d%d”,&a,&b);
Ans=sum(a,b);
Printf(“n the result after addition is %d”,ans);
Getch();
}
Int sum(int f1,int f2);
{
Int result;
Result=f1+f2;
Return(result);
}
output
 Enter 2 numbers 12 12
 The result after addition is 24

Más contenido relacionado

La actualidad más candente

Dev Concepts: Functional Programming
Dev Concepts: Functional ProgrammingDev Concepts: Functional Programming
Dev Concepts: Functional ProgrammingSvetlin Nakov
 
Function in c language(defination and declaration)
Function in c language(defination and declaration)Function in c language(defination and declaration)
Function in c language(defination and declaration)VC Infotech
 
Functional programming
Functional programmingFunctional programming
Functional programmingKibru Demeke
 
Function overloading(C++)
Function overloading(C++)Function overloading(C++)
Function overloading(C++)Ritika Sharma
 
Basic structure of c programming
Basic structure of c programmingBasic structure of c programming
Basic structure of c programmingTejaswiB4
 
Functions in c language
Functions in c language Functions in c language
Functions in c language tanmaymodi4
 
Pointers and call by value, reference, address in C
Pointers and call by value, reference, address in CPointers and call by value, reference, address in C
Pointers and call by value, reference, address in CSyed Mustafa
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C v_jk
 
structured programming
structured programmingstructured programming
structured programmingAhmad54321
 
11 2. variable-scope rule,-storage_class
11 2. variable-scope rule,-storage_class11 2. variable-scope rule,-storage_class
11 2. variable-scope rule,-storage_class웅식 전
 
Amit user defined functions xi (2)
Amit  user defined functions xi (2)Amit  user defined functions xi (2)
Amit user defined functions xi (2)Arpit Meena
 

La actualidad más candente (20)

Dev Concepts: Functional Programming
Dev Concepts: Functional ProgrammingDev Concepts: Functional Programming
Dev Concepts: Functional Programming
 
Function in c language(defination and declaration)
Function in c language(defination and declaration)Function in c language(defination and declaration)
Function in c language(defination and declaration)
 
Functional programming
Functional programmingFunctional programming
Functional programming
 
Lecture 11 - Functions
Lecture 11 - FunctionsLecture 11 - Functions
Lecture 11 - Functions
 
Some basic FP concepts
Some basic FP conceptsSome basic FP concepts
Some basic FP concepts
 
Lecture 13 - Storage Classes
Lecture 13 - Storage ClassesLecture 13 - Storage Classes
Lecture 13 - Storage Classes
 
Function overloading(C++)
Function overloading(C++)Function overloading(C++)
Function overloading(C++)
 
Lecture 14 - Scope Rules
Lecture 14 - Scope RulesLecture 14 - Scope Rules
Lecture 14 - Scope Rules
 
C function presentation
C function presentationC function presentation
C function presentation
 
Basic structure of c programming
Basic structure of c programmingBasic structure of c programming
Basic structure of c programming
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
Pointers and call by value, reference, address in C
Pointers and call by value, reference, address in CPointers and call by value, reference, address in C
Pointers and call by value, reference, address in C
 
Storage classess of C progamming
Storage classess of C progamming Storage classess of C progamming
Storage classess of C progamming
 
Function in C
Function in CFunction in C
Function in C
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
 
structured programming
structured programmingstructured programming
structured programming
 
11 2. variable-scope rule,-storage_class
11 2. variable-scope rule,-storage_class11 2. variable-scope rule,-storage_class
11 2. variable-scope rule,-storage_class
 
Functions
FunctionsFunctions
Functions
 
Python functions
Python functionsPython functions
Python functions
 
Amit user defined functions xi (2)
Amit  user defined functions xi (2)Amit  user defined functions xi (2)
Amit user defined functions xi (2)
 

Destacado (20)

5 steps you have to do on social media before launching
5 steps you have to do on social media before launching5 steps you have to do on social media before launching
5 steps you have to do on social media before launching
 
Movie maker
Movie makerMovie maker
Movie maker
 
Famous Accounting Scandals
Famous Accounting ScandalsFamous Accounting Scandals
Famous Accounting Scandals
 
Slot waddin
Slot waddinSlot waddin
Slot waddin
 
Melanie Richard, RHIT 2016
Melanie Richard, RHIT 2016Melanie Richard, RHIT 2016
Melanie Richard, RHIT 2016
 
Resume Nov 2015
Resume Nov 2015Resume Nov 2015
Resume Nov 2015
 
Planos seriados
Planos seriadosPlanos seriados
Planos seriados
 
Certificate of Management Consulting
Certificate of Management ConsultingCertificate of Management Consulting
Certificate of Management Consulting
 
Thank you letter from Syria tel
Thank you letter from Syria telThank you letter from Syria tel
Thank you letter from Syria tel
 
CadeConnerResume
CadeConnerResumeCadeConnerResume
CadeConnerResume
 
Induction address to sec 3 2014
Induction address to sec 3 2014Induction address to sec 3 2014
Induction address to sec 3 2014
 
Urbanismo como detonante de violencia
Urbanismo como detonante de violenciaUrbanismo como detonante de violencia
Urbanismo como detonante de violencia
 
Paduan agamanya ila
Paduan agamanya ilaPaduan agamanya ila
Paduan agamanya ila
 
Guia rapido gerenciamento de projetos
Guia rapido gerenciamento de projetos Guia rapido gerenciamento de projetos
Guia rapido gerenciamento de projetos
 
120262739 anc-fisiologis
120262739 anc-fisiologis120262739 anc-fisiologis
120262739 anc-fisiologis
 
Tugas pratikum 1
Tugas pratikum 1Tugas pratikum 1
Tugas pratikum 1
 
Tabuladores cg
Tabuladores cgTabuladores cg
Tabuladores cg
 
Hubungan antara infertilitas dengan tingkat depresi pada wanita infertilitas ...
Hubungan antara infertilitas dengan tingkat depresi pada wanita infertilitas ...Hubungan antara infertilitas dengan tingkat depresi pada wanita infertilitas ...
Hubungan antara infertilitas dengan tingkat depresi pada wanita infertilitas ...
 
autism wheel power point
autism wheel power pointautism wheel power point
autism wheel power point
 
Sangri cg
Sangri cgSangri cg
Sangri cg
 

Similar a Functions in C - Types, Defining, Calling and Examples

Similar a Functions in C - Types, Defining, Calling and Examples (20)

FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
 FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM) FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
 
Lecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxLecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptx
 
C and C++ functions
C and C++ functionsC and C++ functions
C and C++ functions
 
Unit 3 (1)
Unit 3 (1)Unit 3 (1)
Unit 3 (1)
 
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdfUSER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
 
User defined functions in C
User defined functions in CUser defined functions in C
User defined functions in C
 
Unit-III.pptx
Unit-III.pptxUnit-III.pptx
Unit-III.pptx
 
Functions in c mrs.sowmya jyothi
Functions in c mrs.sowmya jyothiFunctions in c mrs.sowmya jyothi
Functions in c mrs.sowmya jyothi
 
VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1
 
Functions assignment
Functions assignmentFunctions assignment
Functions assignment
 
[ITP - Lecture 12] Functions in C/C++
[ITP - Lecture 12] Functions in C/C++[ITP - Lecture 12] Functions in C/C++
[ITP - Lecture 12] Functions in C/C++
 
USER DEFINED FUNCTIONS IN C.pdf
USER DEFINED FUNCTIONS IN C.pdfUSER DEFINED FUNCTIONS IN C.pdf
USER DEFINED FUNCTIONS IN C.pdf
 
Ch4 functions
Ch4 functionsCh4 functions
Ch4 functions
 
Functions
FunctionsFunctions
Functions
 
C functions list
C functions listC functions list
C functions list
 
function of C.pptx
function of C.pptxfunction of C.pptx
function of C.pptx
 
Function
FunctionFunction
Function
 
D-38 vedant ICCPL.pptx
D-38 vedant ICCPL.pptxD-38 vedant ICCPL.pptx
D-38 vedant ICCPL.pptx
 
Functions in c
Functions in cFunctions in c
Functions in c
 
arrays.ppt
arrays.pptarrays.ppt
arrays.ppt
 

Más de jasscheema

Más de jasscheema (7)

Pipelinig hazardous
Pipelinig hazardousPipelinig hazardous
Pipelinig hazardous
 
Btree
BtreeBtree
Btree
 
B+tree
B+treeB+tree
B+tree
 
Multiole acccess
Multiole acccessMultiole acccess
Multiole acccess
 
Scheduling
SchedulingScheduling
Scheduling
 
Mobile number-portability
Mobile number-portabilityMobile number-portability
Mobile number-portability
 
Presentation1
Presentation1Presentation1
Presentation1
 

Último

Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 

Último (20)

Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 

Functions in C - Types, Defining, Calling and Examples

  • 1. Function A function is a set of statements, written in a particular language that performs a particular task ,when ever called.
  • 2. Types of functions 1.Inbuilt or system defined or Library Functions 2.User defined Functions
  • 3. Inbuilt functions These are the functions whose defintions is already defined and stored in a respective header files of c library . Example of this type of functions are gets(),puts(),clrscr(),getche(),printf(),scanf(). Etc.
  • 4. User defined functions  The functions which are created by the user to, perform the desired task,are called user defined functions. User defined functions provide the following Benefits:1.By using user defined functions,we can divide a large problem into small independence modules,each having a defined purpose.the process is called modulation.
  • 5. benefits 2.Smaller modules are easy to design and debug. 3.We can create own user defined functions,in the way,a function can be created once and used for many times.i.e sharing of the code. 4Modulation provide greater clarity to the programs.
  • 6. Defining a function Syntax :<return type><name>(parameterlist)> { Statement 1; Statement 2; ----------------Statement n; Return (expression); }
  • 7. Elements of functions 1. Return type :- is datatype of the value ,returned by the function .void is used when the function does not return any value,int and float are used when functions returns integer or floating point values. 2.Name:-is the name of the function 3.Parameter list:-contain variables carrying information from the main function. 4 Function call is represented by writing the name of the function followed by braces and semicolon;
  • 8. Example describes a function to calc sum of two no. Int sum(int a,int b) { // function body Printf(“the sum of no is being calculated”); Return(a+b); } Void main() { Int j=0; Int k=10; Int m=12; J=sum(k,m); //call to sum function Printf(“the sum is %d”,j); }
  • 9. Return type of the function is “int”.the name of the function is “sum” .the parameter are” a” and “b” of integer type. C allows us to define user defined function in various ways.we can define the functions in following ways: 1. Functions with no argument and no return type. 2. Function with argument and no return type. 3. Functions with argument and with return type like int ,float.
  • 10. Functions with no argument and no return type. These are the simple function which do not take any data from calling functions, in the form of parameters and do not return anything to calling functions, after execution.
  • 11. The example describes the use of func. with no parameter and no return type Simple function. #include<stdio.h> #include<conio.h> Void main() { void punjab(); //function prototyping Void delhi(); //function prototyping Printf(“n I m from main”); Punjab(); // call to punjab()
  • 12. Printf(“nt I m back in main”); Himachal(); Print f(“nt t I m from main again”); } Void punjab() { Printf(“n I m from punjab”); } Void himachal() { Print(“n I m from himachal”); }
  • 13. output I m from main I m from punjab I m back in main I m from himchal I m from main again
  • 14. The example describes the use of func. with no parameter and return type Example to calculate the sum of two numbers using #include<stdio.h> #include<conio.h> Void main() { Int sum(); Int result();
  • 15. Clrscr(); Printf(“calculate sum of two numbers “); Getch(); Result=sum(); call to function sum //result contains the value entered Printf(“n the result after adding is number %d”,result); Getch(); } Int sum() body of the function {
  • 16. Int a,b; Printf(“n enter 2 numbers”); Scanf(“%d%d”,&a,&b); Return(a+b); }
  • 17. Out put Ready to calculate sum of two numbers-press any key Enter 2 number 12 23 The result after adding numbers is 35
  • 18. Functions with argu and no return type  To demonstrate the functions with parameter and no return #include<stdio.h> #include<conio.h> Void main() { Void sum(int,int); Int a,b; Printf(“enter 2 numbers”); Scanf(“%d%”,&a,&b); Sum(a,b); Getch();
  • 19. } //end of main Void sum(int f1,int f2) { Int result; //variable in function Result=(f1+f2); Printf(“n the result after addition is%d”,result); }
  • 20. Out put Enter 2 numbers 10 10 The result after addition is 20
  • 21. Func with argu and return type #include<stdio.h> #include<conio.h> Void main() { Int sum(int,int); Int a,b,ans; Printf(“enter 2 numbers”); Sanf(“%d%d”,&a,&b); Ans=sum(a,b); Printf(“n the result after addition is %d”,ans);
  • 22. Getch(); } Int sum(int f1,int f2); { Int result; Result=f1+f2; Return(result); }
  • 23. output  Enter 2 numbers 12 12  The result after addition is 24