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

Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptxPoojaSen20
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
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
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
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
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
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
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 

Recently uploaded (20)

Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptx
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
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
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
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
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
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
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 

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.