SlideShare a Scribd company logo
1 of 17
Lecture 06
Function
Introduction
Functions allow a sequence of statement to be referred to by a
name and parameterized.
•Calling a function causes statement sequence to be executed and
a value may be returned when the function terminates.
•Parameterization allows the same function to be applied to many
different values without changing the statements.
•sqrt is function that computes square root of number, sqrt(4)
computes square root of 4, sqrt(9) computer square root of 9!
Same sequence of statements inside function, with different
parameter returns different value.
•
Declaration
Introduces the function name, function return type,
and function parameters to the program.
•The function body (statements)is not part of the
declaration
•A function must be declared before it is used
Format: return_type function_name(parameter_list);
parameter_list: type param1,type param2,type
param3, etc
•

Some function declarations:

•

double sqrt(double);
int func1();
void func2(char,int);
Function Definition
Introduces the function name, function return type, and function
parameters as well as the function body to the program Function
body (the implementation)is a compound statement
return_type function_name(parameter_list)
{
// Function body
}
• Function Definition Example:
•

void print_func()
{
cout << “hello world” << endl;
}

A function can be defined in any part of the program text or
within a library
•
Functions And Program
Structure
Functions must be declared before they are used, e.g:
l// preprocessor statements
lDefine function 1
lDefine function 2
lDefine function 3
l{ call function 2}
lmain()
l{
lCall function 1
lCall function 3
l}

•
Void Functions
These are functions that do not return a value
They can still cause a side effect by modifying a global variable
Example:
l int num = 1;
lvoid print_func()
l{
lcout << “hello world” << num << endl;
lnum = num + 1;
l}
•Note: It is not possible to declare variables of type void because
type void has no values, i.e. it has no structure
•
Parameterized Functions
A function declaration must include the list of parameter types
(with an optional name):
int func(int x)
•Each parameter type specifies the type of the value that should
appear as a function argument when the function is called.
•Therefore the function func takes one parameter which is an
integer func can be called using: int x =func(42);
•The function name is followed by a bracketed list of function
arguments
•Note: A function definition also requires a parameter list with in
the function body the parameters are accessible as variables
which are named in the parameter list with their types:
•
Int func(int x,int y)
{
// place function body here
// variables x and y can be used here
}

x and y are the parameter variables that can be used in the
function body
• Note that the types of the parameters must be the same in the
function declaration and definition
•Formal parameters: the parameters used in the function
definition, e.g. x and y in
int func(int x, int y) …
•
Parameterized Functions
Actual parameters or arguments: the parameters used in the
function call, e.g. 10 and 20 in
int x = func(10, 20);
•Note that the actual parameters are evaluated before being
passed to
the function, e.g.
•

int a = 10, b =20;
int x =func(a/2, a+b);
•

The actual parameters passed to func are 5 and 30
The order of evaluation of arguments is undefined.
Functions and Scope
Functions may only be referred to within the scope of the
declaration.
•Declarations made in a function body or compound statement
are local to that scope.
•Function parameters are variables in the scope of the function
body.
•A new variable is created for each parameter when ever the
function is called.
•Each parameter variable is destroyed when the function
terminates.
•Hence parameters are local to the function they are part of.
•
Default Parameters
Function parameters may be given default values

•

int func(int x = 10);

If func is called with an empty parameter list

•

func();

then the parameter variable x will be initialized to 10
•If func is called with a parameter
func(15);
then the default is NOT used and the parameter variable x is
initialized to the actual parameter value,I.e.15
Default Parameters
Note that default parameters must appear at the END of the
parameter list
•

int func1(int x = 1);
int func2(int x, int y = 1);
int func3(int x = 1, int y);
•

// this is ok
// this is ok
// this is not ok

Also several default parameters can be given

int func4(char c, int x = 1, int y = 1); this is ok
Parameter Passing In
Functions

Arguments/Parameters are a way to share data between two
functions.
•In all the programs we have seen so far, the arguments in a
function call provide data needed by the called function
•

they pass data in to the function
There are times when a function wants to use its parameters to
pass data back to the calling program.
•

examples are when functions must return more than one value or must
modify the arguments passed to them.

We need two types of parameter passing to handle these two
situations.
•
Call By Value
Call by value means the value of i is passed as a parameter to
func
The local variable I of func is initialized to that value
The local variable I of main that appears in the call is left
untouched -only a copy of its value is passed to the function
func.
•
Call By Reference
References allow a second parameter passing mechanism to be
used - call by reference
•If a formal parameter type is a reference type
•

int func(int &x)

Then the parameter variable x will be a reference to the actual
parameter.
•

int y = 1;
func(y);
x will be initialized to a reference to y when the function func is called
So x and y will both denote the SAME object

Contrast this to call by value where x will be initialized to a copy
of the value of y.
•
Inline Function
Inline functions are functions where the call is made to
inline functions. The actual code then gets placed in the
calling program.
Once you define an inline function, using the 'inline'
keyword, whenever you call that function the compiler
will replace the function call with the actual code from
the function.
General form
inline datatype function_name(arguments)
Inline Function
#include <iostream>
using namespace std;
int func(int);
void main( )
{
int x;
cout << "n Enter the Input Value: ";
cin>>x;
cout << "n The Output is: " << func(x);
}
inline int func(int x1)
{
return 5*x1;
}
Note:-Inline functions will save time and are useful if the function is
very small. If the function is large, use of inline functions must be
avoided.

More Related Content

What's hot

Functions in C++
Functions in C++Functions in C++
Functions in C++Nikhil Pandit
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT03062679929
 
C++ Function
C++ FunctionC++ Function
C++ FunctionHajar
 
Prsentation on functions
Prsentation on functionsPrsentation on functions
Prsentation on functionsAlisha Korpal
 
Function in C Language
Function in C Language Function in C Language
Function in C Language programmings guru
 
predefined and user defined functions
predefined and user defined functionspredefined and user defined functions
predefined and user defined functionsSwapnil Yadav
 
Inline function
Inline functionInline function
Inline functionTech_MX
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C v_jk
 
Pre defined Functions in C
Pre defined Functions in CPre defined Functions in C
Pre defined Functions in CPrabhu Govind
 
Functions in C++
Functions in C++Functions in C++
Functions in C++home
 
user defined function
user defined functionuser defined function
user defined functionKing Kavin Patel
 
Lecture#7 Call by value and reference in c++
Lecture#7 Call by value and reference in c++Lecture#7 Call by value and reference in c++
Lecture#7 Call by value and reference in c++NUST Stuff
 
Function in c
Function in cFunction in c
Function in cRaj Tandukar
 
Functions in c++
Functions in c++Functions in c++
Functions in c++Maaz Hasan
 
User defined functions in C
User defined functions in CUser defined functions in C
User defined functions in CHarendra Singh
 

What's hot (19)

User Defined Functions
User Defined FunctionsUser Defined Functions
User Defined Functions
 
Function
FunctionFunction
Function
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
 
C++ Function
C++ FunctionC++ Function
C++ Function
 
Prsentation on functions
Prsentation on functionsPrsentation on functions
Prsentation on functions
 
Function in C Language
Function in C Language Function in C Language
Function in C Language
 
predefined and user defined functions
predefined and user defined functionspredefined and user defined functions
predefined and user defined functions
 
Inline function
Inline functionInline function
Inline function
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
 
Pre defined Functions in C
Pre defined Functions in CPre defined Functions in C
Pre defined Functions in C
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Built in function
Built in functionBuilt in function
Built in function
 
user defined function
user defined functionuser defined function
user defined function
 
Function in C program
Function in C programFunction in C program
Function in C program
 
Lecture#7 Call by value and reference in c++
Lecture#7 Call by value and reference in c++Lecture#7 Call by value and reference in c++
Lecture#7 Call by value and reference in c++
 
Function in c
Function in cFunction in c
Function in c
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
User defined functions in C
User defined functions in CUser defined functions in C
User defined functions in C
 

Viewers also liked

C++ programming function
C++ programming functionC++ programming function
C++ programming functionVishalini Mugunen
 
Functions in C++
Functions in C++Functions in C++
Functions in C++Sachin Sharma
 
Functions in C++
Functions in C++Functions in C++
Functions in C++home
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator OverloadingNilesh Dalvi
 
OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++Aabha Tiwari
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.pptTareq Hasan
 
operator overloading in c++
operator overloading in c++operator overloading in c++
operator overloading in c++harman kaur
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)Ritika Sharma
 
Operator overloading
Operator overloadingOperator overloading
Operator overloadingKumar
 
Presentation on overloading
Presentation on overloading Presentation on overloading
Presentation on overloading Charndeep Sekhon
 

Viewers also liked (11)

C++ programming function
C++ programming functionC++ programming function
C++ programming function
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
 
operator overloading in c++
operator overloading in c++operator overloading in c++
operator overloading in c++
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Presentation on overloading
Presentation on overloading Presentation on overloading
Presentation on overloading
 
Function overloading
Function overloadingFunction overloading
Function overloading
 

Similar to Function in c++

User defined function in C.pptx
User defined function in C.pptxUser defined function in C.pptx
User defined function in C.pptxRhishav Poudyal
 
User Defined Functions in C
User Defined Functions in CUser Defined Functions in C
User Defined Functions in CRAJ KUMAR
 
CHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptxCHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptxGebruGetachew2
 
CH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptxCH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptxsangeeta borde
 
358 33 powerpoint-slides_2-functions_chapter-2
358 33 powerpoint-slides_2-functions_chapter-2358 33 powerpoint-slides_2-functions_chapter-2
358 33 powerpoint-slides_2-functions_chapter-2sumitbardhan
 
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)Mansi Tyagi
 
C language presentation
C language presentationC language presentation
C language presentationbainspreet
 
662213141-Tuxdoc-com-Programming-in-c-Reema-Thareja.pdf
662213141-Tuxdoc-com-Programming-in-c-Reema-Thareja.pdf662213141-Tuxdoc-com-Programming-in-c-Reema-Thareja.pdf
662213141-Tuxdoc-com-Programming-in-c-Reema-Thareja.pdfManiMala75
 
Functions (Computer programming and utilization)
Functions (Computer programming and utilization)Functions (Computer programming and utilization)
Functions (Computer programming and utilization)Digvijaysinh Gohil
 
Chapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdfChapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdfTeshaleSiyum
 
Chapter 1. Functions in C++.pdf
Chapter 1.  Functions in C++.pdfChapter 1.  Functions in C++.pdf
Chapter 1. Functions in C++.pdfTeshaleSiyum
 

Similar to Function in c++ (20)

User defined function in C.pptx
User defined function in C.pptxUser defined function in C.pptx
User defined function in C.pptx
 
User Defined Functions in C
User Defined Functions in CUser Defined Functions in C
User Defined Functions in C
 
CHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptxCHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptx
 
CH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptxCH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptx
 
PSPC-UNIT-4.pdf
PSPC-UNIT-4.pdfPSPC-UNIT-4.pdf
PSPC-UNIT-4.pdf
 
Lecture6
Lecture6Lecture6
Lecture6
 
358 33 powerpoint-slides_2-functions_chapter-2
358 33 powerpoint-slides_2-functions_chapter-2358 33 powerpoint-slides_2-functions_chapter-2
358 33 powerpoint-slides_2-functions_chapter-2
 
arrays.ppt
arrays.pptarrays.ppt
arrays.ppt
 
10. funtions and closures IN SWIFT PROGRAMMING
10. funtions and closures IN SWIFT PROGRAMMING10. funtions and closures IN SWIFT PROGRAMMING
10. funtions and closures IN SWIFT PROGRAMMING
 
Ch4 functions
Ch4 functionsCh4 functions
Ch4 functions
 
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)
 
C language presentation
C language presentationC language presentation
C language presentation
 
662213141-Tuxdoc-com-Programming-in-c-Reema-Thareja.pdf
662213141-Tuxdoc-com-Programming-in-c-Reema-Thareja.pdf662213141-Tuxdoc-com-Programming-in-c-Reema-Thareja.pdf
662213141-Tuxdoc-com-Programming-in-c-Reema-Thareja.pdf
 
Functions-.pdf
Functions-.pdfFunctions-.pdf
Functions-.pdf
 
functions- best.pdf
functions- best.pdffunctions- best.pdf
functions- best.pdf
 
Lecture 11 - Functions
Lecture 11 - FunctionsLecture 11 - Functions
Lecture 11 - Functions
 
Functions (Computer programming and utilization)
Functions (Computer programming and utilization)Functions (Computer programming and utilization)
Functions (Computer programming and utilization)
 
Functions
FunctionsFunctions
Functions
 
Chapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdfChapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdf
 
Chapter 1. Functions in C++.pdf
Chapter 1.  Functions in C++.pdfChapter 1.  Functions in C++.pdf
Chapter 1. Functions in C++.pdf
 

More from Kumar

Graphics devices
Graphics devicesGraphics devices
Graphics devicesKumar
 
Fill area algorithms
Fill area algorithmsFill area algorithms
Fill area algorithmsKumar
 
region-filling
region-fillingregion-filling
region-fillingKumar
 
Bresenham derivation
Bresenham derivationBresenham derivation
Bresenham derivationKumar
 
Bresenham circles and polygons derication
Bresenham circles and polygons dericationBresenham circles and polygons derication
Bresenham circles and polygons dericationKumar
 
Introductionto xslt
Introductionto xsltIntroductionto xslt
Introductionto xsltKumar
 
Extracting data from xml
Extracting data from xmlExtracting data from xml
Extracting data from xmlKumar
 
Xml basics
Xml basicsXml basics
Xml basicsKumar
 
XML Schema
XML SchemaXML Schema
XML SchemaKumar
 
Publishing xml
Publishing xmlPublishing xml
Publishing xmlKumar
 
DTD
DTDDTD
DTDKumar
 
Applying xml
Applying xmlApplying xml
Applying xmlKumar
 
Introduction to XML
Introduction to XMLIntroduction to XML
Introduction to XMLKumar
 
How to deploy a j2ee application
How to deploy a j2ee applicationHow to deploy a j2ee application
How to deploy a j2ee applicationKumar
 
JNDI, JMS, JPA, XML
JNDI, JMS, JPA, XMLJNDI, JMS, JPA, XML
JNDI, JMS, JPA, XMLKumar
 
EJB Fundmentals
EJB FundmentalsEJB Fundmentals
EJB FundmentalsKumar
 
JSP and struts programming
JSP and struts programmingJSP and struts programming
JSP and struts programmingKumar
 
java servlet and servlet programming
java servlet and servlet programmingjava servlet and servlet programming
java servlet and servlet programmingKumar
 
Introduction to JDBC and JDBC Drivers
Introduction to JDBC and JDBC DriversIntroduction to JDBC and JDBC Drivers
Introduction to JDBC and JDBC DriversKumar
 
Introduction to J2EE
Introduction to J2EEIntroduction to J2EE
Introduction to J2EEKumar
 

More from Kumar (20)

Graphics devices
Graphics devicesGraphics devices
Graphics devices
 
Fill area algorithms
Fill area algorithmsFill area algorithms
Fill area algorithms
 
region-filling
region-fillingregion-filling
region-filling
 
Bresenham derivation
Bresenham derivationBresenham derivation
Bresenham derivation
 
Bresenham circles and polygons derication
Bresenham circles and polygons dericationBresenham circles and polygons derication
Bresenham circles and polygons derication
 
Introductionto xslt
Introductionto xsltIntroductionto xslt
Introductionto xslt
 
Extracting data from xml
Extracting data from xmlExtracting data from xml
Extracting data from xml
 
Xml basics
Xml basicsXml basics
Xml basics
 
XML Schema
XML SchemaXML Schema
XML Schema
 
Publishing xml
Publishing xmlPublishing xml
Publishing xml
 
DTD
DTDDTD
DTD
 
Applying xml
Applying xmlApplying xml
Applying xml
 
Introduction to XML
Introduction to XMLIntroduction to XML
Introduction to XML
 
How to deploy a j2ee application
How to deploy a j2ee applicationHow to deploy a j2ee application
How to deploy a j2ee application
 
JNDI, JMS, JPA, XML
JNDI, JMS, JPA, XMLJNDI, JMS, JPA, XML
JNDI, JMS, JPA, XML
 
EJB Fundmentals
EJB FundmentalsEJB Fundmentals
EJB Fundmentals
 
JSP and struts programming
JSP and struts programmingJSP and struts programming
JSP and struts programming
 
java servlet and servlet programming
java servlet and servlet programmingjava servlet and servlet programming
java servlet and servlet programming
 
Introduction to JDBC and JDBC Drivers
Introduction to JDBC and JDBC DriversIntroduction to JDBC and JDBC Drivers
Introduction to JDBC and JDBC Drivers
 
Introduction to J2EE
Introduction to J2EEIntroduction to J2EE
Introduction to J2EE
 

Recently uploaded

Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
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
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
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
 
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
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
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
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
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
 
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
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 

Recently uploaded (20)

Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
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
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
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
 
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
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
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
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
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
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
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
 
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
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
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
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 

Function in c++

  • 2. Introduction Functions allow a sequence of statement to be referred to by a name and parameterized. •Calling a function causes statement sequence to be executed and a value may be returned when the function terminates. •Parameterization allows the same function to be applied to many different values without changing the statements. •sqrt is function that computes square root of number, sqrt(4) computes square root of 4, sqrt(9) computer square root of 9! Same sequence of statements inside function, with different parameter returns different value. •
  • 3. Declaration Introduces the function name, function return type, and function parameters to the program. •The function body (statements)is not part of the declaration •A function must be declared before it is used Format: return_type function_name(parameter_list); parameter_list: type param1,type param2,type param3, etc • Some function declarations: • double sqrt(double); int func1(); void func2(char,int);
  • 4. Function Definition Introduces the function name, function return type, and function parameters as well as the function body to the program Function body (the implementation)is a compound statement return_type function_name(parameter_list) { // Function body } • Function Definition Example: • void print_func() { cout << “hello world” << endl; } A function can be defined in any part of the program text or within a library •
  • 5. Functions And Program Structure Functions must be declared before they are used, e.g: l// preprocessor statements lDefine function 1 lDefine function 2 lDefine function 3 l{ call function 2} lmain() l{ lCall function 1 lCall function 3 l} •
  • 6. Void Functions These are functions that do not return a value They can still cause a side effect by modifying a global variable Example: l int num = 1; lvoid print_func() l{ lcout << “hello world” << num << endl; lnum = num + 1; l} •Note: It is not possible to declare variables of type void because type void has no values, i.e. it has no structure •
  • 7. Parameterized Functions A function declaration must include the list of parameter types (with an optional name): int func(int x) •Each parameter type specifies the type of the value that should appear as a function argument when the function is called. •Therefore the function func takes one parameter which is an integer func can be called using: int x =func(42); •The function name is followed by a bracketed list of function arguments •Note: A function definition also requires a parameter list with in the function body the parameters are accessible as variables which are named in the parameter list with their types: •
  • 8. Int func(int x,int y) { // place function body here // variables x and y can be used here } x and y are the parameter variables that can be used in the function body • Note that the types of the parameters must be the same in the function declaration and definition •Formal parameters: the parameters used in the function definition, e.g. x and y in int func(int x, int y) … •
  • 9. Parameterized Functions Actual parameters or arguments: the parameters used in the function call, e.g. 10 and 20 in int x = func(10, 20); •Note that the actual parameters are evaluated before being passed to the function, e.g. • int a = 10, b =20; int x =func(a/2, a+b); • The actual parameters passed to func are 5 and 30 The order of evaluation of arguments is undefined.
  • 10. Functions and Scope Functions may only be referred to within the scope of the declaration. •Declarations made in a function body or compound statement are local to that scope. •Function parameters are variables in the scope of the function body. •A new variable is created for each parameter when ever the function is called. •Each parameter variable is destroyed when the function terminates. •Hence parameters are local to the function they are part of. •
  • 11. Default Parameters Function parameters may be given default values • int func(int x = 10); If func is called with an empty parameter list • func(); then the parameter variable x will be initialized to 10 •If func is called with a parameter func(15); then the default is NOT used and the parameter variable x is initialized to the actual parameter value,I.e.15
  • 12. Default Parameters Note that default parameters must appear at the END of the parameter list • int func1(int x = 1); int func2(int x, int y = 1); int func3(int x = 1, int y); • // this is ok // this is ok // this is not ok Also several default parameters can be given int func4(char c, int x = 1, int y = 1); this is ok
  • 13. Parameter Passing In Functions Arguments/Parameters are a way to share data between two functions. •In all the programs we have seen so far, the arguments in a function call provide data needed by the called function • they pass data in to the function There are times when a function wants to use its parameters to pass data back to the calling program. • examples are when functions must return more than one value or must modify the arguments passed to them. We need two types of parameter passing to handle these two situations. •
  • 14. Call By Value Call by value means the value of i is passed as a parameter to func The local variable I of func is initialized to that value The local variable I of main that appears in the call is left untouched -only a copy of its value is passed to the function func. •
  • 15. Call By Reference References allow a second parameter passing mechanism to be used - call by reference •If a formal parameter type is a reference type • int func(int &x) Then the parameter variable x will be a reference to the actual parameter. • int y = 1; func(y); x will be initialized to a reference to y when the function func is called So x and y will both denote the SAME object Contrast this to call by value where x will be initialized to a copy of the value of y. •
  • 16. Inline Function Inline functions are functions where the call is made to inline functions. The actual code then gets placed in the calling program. Once you define an inline function, using the 'inline' keyword, whenever you call that function the compiler will replace the function call with the actual code from the function. General form inline datatype function_name(arguments)
  • 17. Inline Function #include <iostream> using namespace std; int func(int); void main( ) { int x; cout << "n Enter the Input Value: "; cin>>x; cout << "n The Output is: " << func(x); } inline int func(int x1) { return 5*x1; } Note:-Inline functions will save time and are useful if the function is very small. If the function is large, use of inline functions must be avoided.