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

What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...JojoEDelaCruz
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptshraddhaparab530
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationRosabel UA
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)cama23
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 

Último (20)

What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.ppt
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translation
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 

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