SlideShare a Scribd company logo
FUNCTIONS IN C++
9/22/2017By MRI 1
Functions in C++
Functions are used to provide modularity to a
program. Creating an application using function
makes it easier to understand, edit, check errors
etc.
Functions allow to structure programs in segments
of code to perform individual tasks.
In C++, a function is a group of statements that is
given a name, and which can be called from some
point of the program.
9/22/2017By MRI 2
Functions in C++
Depending on whether a function is predefined or
created by programmer; there are two types of
function:
1. Library Function
2. User-defined Function
9/22/2017By MRI 3
User Defined Functions in C++
Syntax of Function
return-type function-name (parameters)
{
// function-body
}
return-type : suggests what the function will return. It can be int,
char, some pointer or even a class object. There can be functions
which does not return anything, they are mentioned with void.
Function Name : is the name of the function, using the function
name it is called.
Parameters : are variables to hold values of arguments passed
while function is called. A function may or may not contain
parameter list.
9/22/2017By MRI 4
Function prototype (declaration)
If a user-defined function is defined after main()
function, compiler will show error.
It is because compiler is unaware of user-defined
function, types of argument passed to function and
return type.
In C++, function prototype is a declaration of
function without its body to give compiler
information about user-defined function.
9/22/2017By MRI 5
Declaring, Defining and Calling Function
#include < iostream>
using namespace std;
int sum (int x, int y); //declaring function
int main()
{
int a = 10;
int b = 20;
int c = sum (a, b); //calling function
cout << c;
}
int sum (int x, int y) //defining function
{
return (X + y);
}
9/22/2017By MRI 6
Declaring, Defining and Calling Function
Here, initially the function is declared, without body.
Then inside main() function it is called, as the function
returns summation of two values, hence c is their to
store the value of sum.
Then, at last, function is defined, where the body of
function is mentioned. We can also, declare & define the
function together, but then it should be done before it is
called.
9/22/2017By MRI 7
Calling a Function
Functions are called by their names. If the function is
without argument, it can be called directly using its
name. But for functions with arguments, we have two
ways to call them:
1. Call by Value
2. Call by Reference
9/22/2017By MRI 8
Call by Value
In this calling technique we pass the values of
arguments which are stored or copied into the formal
parameters of functions. Hence, the original values are
unchanged only the parameters inside function changes.
void calc(int x);
int main()
{
int x = 10;
calc(x);
printf("%d", x);
}
void calc(int x)
{
x = x + 10 ;
}
Output : 10 9/22/2017By MRI 9
Call by Value
In this case the actual variable x is not changed,
because we pass argument by value, hence a
copy of x is passed, which is changed, and that
copied value is destroyed as the function
ends(goes out of scope).
So the variable x inside main() still has a value 10.
9/22/2017By MRI 10
Call by Value
But we can change this program to modify the
original x, by making the function calc() return a
value, and storing that value in x.
void calc(int x);
int main()
{
int x = 10;
calc(x);
printf("%d", x);
}
void calc(int x)
{
x = x + 10 ;
return x;
}
Output : 20 9/22/2017By MRI 11
Call by Reference
In this we pass the address of the variable as arguments. In this
case the formal parameter can be taken as a reference or a pointer,
in both the case they will change the values of the original
variable.
void calc(int *p);
int main()
{
int x = 10;
calc(&x); // passing address of x as argument
printf("%d", x);
}
void calc(int *p)
{
*p = *p + 10;
}
Output : 20
9/22/2017By MRI 12
Types of User-defined Functions in C++
9/22/2017By MRI 13
9/22/2017By MRI 14
Example 1: No arguments passed and no return value
# include <iostream>
using namespace std;
void prime();
int main()
{
prime(); // No argument is passed to prime()
return 0;
}
// Return type of function is void because value is not returned.
void prime()
{
int num, i, flag = 0;
cout << "Enter a positive integer enter to check: ";
cin >> num;
for(i = 2; i <= num/2; ++i)
{
if(num % i == 0)
{
flag = 1;
break;
}
}
if (flag == 1)
{
cout << num << " is not a prime number.";
}
else { cout << num << " is a prime number."; .
}
Inline function
Calling a function generally causes a certain
overhead (stacking arguments, jumps, etc...),
and thus for very short functions, it may be more
efficient to simply insert the code of the function
where it is called, instead of performing the
process of formally calling a function.
9/22/2017By MRI 15
Inline function
Preceding a function declaration with the inline
specifier informs the compiler that inline
expansion is preferred over the usual function
call mechanism for a specific function.
This does not change at all the behavior of a
function, but is merely used to suggest the
compiler that the code generated by the function
body shall be inserted at each point the function
is called, instead of being invoked with a regular
function call.
9/22/2017By MRI 16
Inline function
C++ inline function is powerful concept that is
commonly used with classes. If a function is
inline, the compiler places a copy of the code of
that function at each point where the function is
called at compile time.
To inline a function, place the
keyword inline before the function name and
define the function before any calls are made to
the function.
The compiler can ignore the inline qualifier in case
defined function is more than a line.
9/22/2017By MRI 17
Inline function
Following is an example, which makes use of
inline function to return max of two numbers:
#include <iostream>
using namespace std;
inline int Max(int x, int y)
{ return (x > y)? x : y; }
// Main function for the program
int main( ) {
cout << "Max (20,10): " << Max(20,10) << endl; cout <<
"Max (0,200): " << Max(0,200) << endl; cout << "Max
(100,1010): " << Max(100,1010) << endl; return 0;
}
9/22/2017By MRI 18
Friend function
A friend function of a class is defined outside that
class' scope but it has the right to access all
private and protected members of the class.
Even though the prototypes for friend functions
appear in the class definition, friends are not
member functions.
A friend can be a function, function template, or
member function, or a class or class template, in
which case the entire class and all of its
members are friends.
9/22/2017By MRI 19
Friend function
To declare a function as a friend of a class,
precede the function prototype in the class
definition with keyword friend as follows:
class Box {
double width;
public:
double length;
friend void printWidth( Box box );
void setWidth( double wid );
};
9/22/2017By MRI 20
Friend function
#include <iostream>
using namespace std;
// forward declaration
class B;
class A
{
private:
int numA;
public:
A(): numA(12) { } // friend function declaration
friend int add(A, B);
};
class B {
private:
int numB;
public:
B(): numB(1) { } // friend function declaration
friend int add(A , B);
};
// Function add() is the friend function of classes A and B
// that accesses the member variables numA and numB
int add(A objectA, B objectB)
{
return (objectA.numA + objectB.numB);
}
int main()
{ A objectA;
B objectB;
cout<<"Sum: "<< add(objectA, objectB);
return 0;
}
9/22/2017By MRI 21
Friend function
In this program, classes A and B have declared
add() as a friend function.
Thus, this function can access private data of both
class.
Here, add() function adds the private data numA
and numB of two objects objectA and objectB, and
returns it to the main function.
To make this program work properly, a forward
declaration of a class class B should be made as
shown in the above example.
This is because class B is referenced within the
class A using code: friend int add(A , B);.
9/22/2017By MRI 22
Recursive function
A function that calls itself is known as recursive
function. And, this technique is known as
recursion.
void recurse()
{
... .. ...
recurse();
... .. ...
}
int main()
{
... .. ...
recurse();
... .. ...
} 9/22/2017By MRI 23
9/22/2017By MRI 24
Examples of Recursive Function
// Factorial of n = 1*2*3*...*n
#include <iostream>
using namespace std;
int factorial(int);
int main()
{
int n;
cout<<"Enter a number to find factorial: ";
cin >> n;
cout << "Factorial of " << n <<" = " << factorial(n);
return 0;
}
int factorial(int n)
{
if (n > 1)
{
return n*factorial(n-1);
}
else
{
return 1;
}
}
9/22/2017By MRI 25
Ad

More Related Content

What's hot (20)

Functions in c language
Functions in c language Functions in c language
Functions in c language
tanmaymodi4
 
Inline function
Inline functionInline function
Inline function
Tech_MX
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of Constructors
Dhrumil Panchal
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
Vineeta Garg
 
Object oriented programming c++
Object oriented programming c++Object oriented programming c++
Object oriented programming c++
Ankur Pandey
 
Operators and expressions in C++
Operators and expressions in C++Operators and expressions in C++
Operators and expressions in C++
Neeru Mittal
 
Data types in c++
Data types in c++Data types in c++
Data types in c++
Venkata.Manish Reddy
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
Pranali Chaudhari
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
MOHIT AGARWAL
 
Functions in C
Functions in CFunctions in C
Functions in C
Kamal Acharya
 
Function overloading ppt
Function overloading pptFunction overloading ppt
Function overloading ppt
Prof. Dr. K. Adisesha
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
Nilesh Dalvi
 
Call by value
Call by valueCall by value
Call by value
Dharani G
 
Function in c
Function in cFunction in c
Function in c
savitamhaske
 
Polymorphism in c++(ppt)
Polymorphism in c++(ppt)Polymorphism in c++(ppt)
Polymorphism in c++(ppt)
Sanjit Shaw
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
Kamal Acharya
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
Vineeta Garg
 
Union in C programming
Union in C programmingUnion in C programming
Union in C programming
Kamal Acharya
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
Abhilash Nair
 
C functions
C functionsC functions
C functions
University of Potsdam
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
tanmaymodi4
 
Inline function
Inline functionInline function
Inline function
Tech_MX
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of Constructors
Dhrumil Panchal
 
Object oriented programming c++
Object oriented programming c++Object oriented programming c++
Object oriented programming c++
Ankur Pandey
 
Operators and expressions in C++
Operators and expressions in C++Operators and expressions in C++
Operators and expressions in C++
Neeru Mittal
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
MOHIT AGARWAL
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
Nilesh Dalvi
 
Call by value
Call by valueCall by value
Call by value
Dharani G
 
Polymorphism in c++(ppt)
Polymorphism in c++(ppt)Polymorphism in c++(ppt)
Polymorphism in c++(ppt)
Sanjit Shaw
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
Kamal Acharya
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
Vineeta Garg
 
Union in C programming
Union in C programmingUnion in C programming
Union in C programming
Kamal Acharya
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
Abhilash Nair
 

Similar to Functions in c++ (20)

functIONS PROGRAMMING CIII II DJDJKASDJKJASD.pptx
functIONS PROGRAMMING CIII II DJDJKASDJKJASD.pptxfunctIONS PROGRAMMING CIII II DJDJKASDJKJASD.pptx
functIONS PROGRAMMING CIII II DJDJKASDJKJASD.pptx
nandemprasanna
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
Shuvongkor Barman
 
6. Functions in C ++ programming object oriented programming
6. Functions in C ++ programming object oriented programming6. Functions in C ++ programming object oriented programming
6. Functions in C ++ programming object oriented programming
Ahmad177077
 
unit3 part2 pcds function notes.pdf
unit3 part2 pcds function notes.pdfunit3 part2 pcds function notes.pdf
unit3 part2 pcds function notes.pdf
JAVVAJI VENKATA RAO
 
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptxCH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
SangeetaBorde3
 
unit_2.pptx
unit_2.pptxunit_2.pptx
unit_2.pptx
Venkatesh Goud
 
Detailed concept of function in c programming
Detailed concept of function  in c programmingDetailed concept of function  in c programming
Detailed concept of function in c programming
anjanasharma77573
 
Unit 3 (1)
Unit 3 (1)Unit 3 (1)
Unit 3 (1)
Sowri Rajan
 
Unit-III.pptx
Unit-III.pptxUnit-III.pptx
Unit-III.pptx
Mehul Desai
 
USER DEFINED FUNCTIONS IN C.pdf
USER DEFINED FUNCTIONS IN C.pdfUSER DEFINED FUNCTIONS IN C.pdf
USER DEFINED FUNCTIONS IN C.pdf
BoomBoomers
 
Unit 4.pdf
Unit 4.pdfUnit 4.pdf
Unit 4.pdf
thenmozhip8
 
Preprocessor directives
Preprocessor directivesPreprocessor directives
Preprocessor directives
Vikash Dhal
 
Silde of the cse fundamentals a deep analysis
Silde of the cse fundamentals a deep analysisSilde of the cse fundamentals a deep analysis
Silde of the cse fundamentals a deep analysis
Rayhan331
 
C functions by ranjan call by value and reference.pptx
C functions by ranjan call by value and reference.pptxC functions by ranjan call by value and reference.pptx
C functions by ranjan call by value and reference.pptx
ranjan317165
 
Fundamental of programming Fundamental of programming
Fundamental of programming Fundamental of programmingFundamental of programming Fundamental of programming
Fundamental of programming Fundamental of programming
LidetAdmassu
 
Function
FunctionFunction
Function
Rajat Patel
 
cp Module4(1)
cp Module4(1)cp Module4(1)
cp Module4(1)
Amarjith C K
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
Mohammed Saleh
 
Chap 9(functions)
Chap 9(functions)Chap 9(functions)
Chap 9(functions)
Bangabandhu Sheikh Mujibur Rahman Science and Technology University
 
Functionincprogram
FunctionincprogramFunctionincprogram
Functionincprogram
Sampath Kumar
 
functIONS PROGRAMMING CIII II DJDJKASDJKJASD.pptx
functIONS PROGRAMMING CIII II DJDJKASDJKJASD.pptxfunctIONS PROGRAMMING CIII II DJDJKASDJKJASD.pptx
functIONS PROGRAMMING CIII II DJDJKASDJKJASD.pptx
nandemprasanna
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
Shuvongkor Barman
 
6. Functions in C ++ programming object oriented programming
6. Functions in C ++ programming object oriented programming6. Functions in C ++ programming object oriented programming
6. Functions in C ++ programming object oriented programming
Ahmad177077
 
unit3 part2 pcds function notes.pdf
unit3 part2 pcds function notes.pdfunit3 part2 pcds function notes.pdf
unit3 part2 pcds function notes.pdf
JAVVAJI VENKATA RAO
 
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptxCH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
SangeetaBorde3
 
Detailed concept of function in c programming
Detailed concept of function  in c programmingDetailed concept of function  in c programming
Detailed concept of function in c programming
anjanasharma77573
 
USER DEFINED FUNCTIONS IN C.pdf
USER DEFINED FUNCTIONS IN C.pdfUSER DEFINED FUNCTIONS IN C.pdf
USER DEFINED FUNCTIONS IN C.pdf
BoomBoomers
 
Preprocessor directives
Preprocessor directivesPreprocessor directives
Preprocessor directives
Vikash Dhal
 
Silde of the cse fundamentals a deep analysis
Silde of the cse fundamentals a deep analysisSilde of the cse fundamentals a deep analysis
Silde of the cse fundamentals a deep analysis
Rayhan331
 
C functions by ranjan call by value and reference.pptx
C functions by ranjan call by value and reference.pptxC functions by ranjan call by value and reference.pptx
C functions by ranjan call by value and reference.pptx
ranjan317165
 
Fundamental of programming Fundamental of programming
Fundamental of programming Fundamental of programmingFundamental of programming Fundamental of programming
Fundamental of programming Fundamental of programming
LidetAdmassu
 
Ad

More from Rokonuzzaman Rony (20)

Course outline for c programming
Course outline for c  programming Course outline for c  programming
Course outline for c programming
Rokonuzzaman Rony
 
Pointer
PointerPointer
Pointer
Rokonuzzaman Rony
 
Operator Overloading & Type Conversions
Operator Overloading & Type ConversionsOperator Overloading & Type Conversions
Operator Overloading & Type Conversions
Rokonuzzaman Rony
 
Constructors & Destructors
Constructors  & DestructorsConstructors  & Destructors
Constructors & Destructors
Rokonuzzaman Rony
 
Classes and objects in c++
Classes and objects in c++Classes and objects in c++
Classes and objects in c++
Rokonuzzaman Rony
 
Object Oriented Programming with C++
Object Oriented Programming with C++Object Oriented Programming with C++
Object Oriented Programming with C++
Rokonuzzaman Rony
 
Humanitarian task and its importance
Humanitarian task and its importanceHumanitarian task and its importance
Humanitarian task and its importance
Rokonuzzaman Rony
 
Structure
StructureStructure
Structure
Rokonuzzaman Rony
 
Pointers
 Pointers Pointers
Pointers
Rokonuzzaman Rony
 
Loops
LoopsLoops
Loops
Rokonuzzaman Rony
 
Introduction to C programming
Introduction to C programmingIntroduction to C programming
Introduction to C programming
Rokonuzzaman Rony
 
Array
ArrayArray
Array
Rokonuzzaman Rony
 
Constants, Variables, and Data Types
Constants, Variables, and Data TypesConstants, Variables, and Data Types
Constants, Variables, and Data Types
Rokonuzzaman Rony
 
C Programming language
C Programming languageC Programming language
C Programming language
Rokonuzzaman Rony
 
User defined functions
User defined functionsUser defined functions
User defined functions
Rokonuzzaman Rony
 
Numerical Method 2
Numerical Method 2Numerical Method 2
Numerical Method 2
Rokonuzzaman Rony
 
Numerical Method
Numerical Method Numerical Method
Numerical Method
Rokonuzzaman Rony
 
Data structures
Data structuresData structures
Data structures
Rokonuzzaman Rony
 
Data structures
Data structures Data structures
Data structures
Rokonuzzaman Rony
 
Data structures
Data structures Data structures
Data structures
Rokonuzzaman Rony
 
Ad

Recently uploaded (20)

2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx
contactwilliamm2546
 
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar RabbiPresentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Md Shaifullar Rabbi
 
One Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learningOne Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learning
momer9505
 
GDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptxGDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptx
azeenhodekar
 
How to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 WebsiteHow to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 Website
Celine George
 
Operations Management (Dr. Abdulfatah Salem).pdf
Operations Management (Dr. Abdulfatah Salem).pdfOperations Management (Dr. Abdulfatah Salem).pdf
Operations Management (Dr. Abdulfatah Salem).pdf
Arab Academy for Science, Technology and Maritime Transport
 
Presentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem KayaPresentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
Political History of Pala dynasty Pala Rulers NEP.pptx
Political History of Pala dynasty Pala Rulers NEP.pptxPolitical History of Pala dynasty Pala Rulers NEP.pptx
Political History of Pala dynasty Pala Rulers NEP.pptx
Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
Quality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdfQuality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdf
Dr. Bindiya Chauhan
 
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
Celine George
 
Metamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative JourneyMetamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative Journey
Arshad Shaikh
 
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Library Association of Ireland
 
Sinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_NameSinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_Name
keshanf79
 
Understanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s GuideUnderstanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s Guide
GS Virdi
 
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Library Association of Ireland
 
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Library Association of Ireland
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
SPRING FESTIVITIES - UK AND USA -
SPRING FESTIVITIES - UK AND USA            -SPRING FESTIVITIES - UK AND USA            -
SPRING FESTIVITIES - UK AND USA -
Colégio Santa Teresinha
 
Odoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo SlidesOdoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo Slides
Celine George
 
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 AccountingHow to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
Celine George
 
2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx
contactwilliamm2546
 
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar RabbiPresentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Md Shaifullar Rabbi
 
One Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learningOne Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learning
momer9505
 
GDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptxGDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptx
azeenhodekar
 
How to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 WebsiteHow to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 Website
Celine George
 
Presentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem KayaPresentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
Quality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdfQuality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdf
Dr. Bindiya Chauhan
 
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
Celine George
 
Metamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative JourneyMetamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative Journey
Arshad Shaikh
 
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Library Association of Ireland
 
Sinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_NameSinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_Name
keshanf79
 
Understanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s GuideUnderstanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s Guide
GS Virdi
 
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Library Association of Ireland
 
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Library Association of Ireland
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
Odoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo SlidesOdoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo Slides
Celine George
 
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 AccountingHow to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
Celine George
 

Functions in c++

  • 2. Functions in C++ Functions are used to provide modularity to a program. Creating an application using function makes it easier to understand, edit, check errors etc. Functions allow to structure programs in segments of code to perform individual tasks. In C++, a function is a group of statements that is given a name, and which can be called from some point of the program. 9/22/2017By MRI 2
  • 3. Functions in C++ Depending on whether a function is predefined or created by programmer; there are two types of function: 1. Library Function 2. User-defined Function 9/22/2017By MRI 3
  • 4. User Defined Functions in C++ Syntax of Function return-type function-name (parameters) { // function-body } return-type : suggests what the function will return. It can be int, char, some pointer or even a class object. There can be functions which does not return anything, they are mentioned with void. Function Name : is the name of the function, using the function name it is called. Parameters : are variables to hold values of arguments passed while function is called. A function may or may not contain parameter list. 9/22/2017By MRI 4
  • 5. Function prototype (declaration) If a user-defined function is defined after main() function, compiler will show error. It is because compiler is unaware of user-defined function, types of argument passed to function and return type. In C++, function prototype is a declaration of function without its body to give compiler information about user-defined function. 9/22/2017By MRI 5
  • 6. Declaring, Defining and Calling Function #include < iostream> using namespace std; int sum (int x, int y); //declaring function int main() { int a = 10; int b = 20; int c = sum (a, b); //calling function cout << c; } int sum (int x, int y) //defining function { return (X + y); } 9/22/2017By MRI 6
  • 7. Declaring, Defining and Calling Function Here, initially the function is declared, without body. Then inside main() function it is called, as the function returns summation of two values, hence c is their to store the value of sum. Then, at last, function is defined, where the body of function is mentioned. We can also, declare & define the function together, but then it should be done before it is called. 9/22/2017By MRI 7
  • 8. Calling a Function Functions are called by their names. If the function is without argument, it can be called directly using its name. But for functions with arguments, we have two ways to call them: 1. Call by Value 2. Call by Reference 9/22/2017By MRI 8
  • 9. Call by Value In this calling technique we pass the values of arguments which are stored or copied into the formal parameters of functions. Hence, the original values are unchanged only the parameters inside function changes. void calc(int x); int main() { int x = 10; calc(x); printf("%d", x); } void calc(int x) { x = x + 10 ; } Output : 10 9/22/2017By MRI 9
  • 10. Call by Value In this case the actual variable x is not changed, because we pass argument by value, hence a copy of x is passed, which is changed, and that copied value is destroyed as the function ends(goes out of scope). So the variable x inside main() still has a value 10. 9/22/2017By MRI 10
  • 11. Call by Value But we can change this program to modify the original x, by making the function calc() return a value, and storing that value in x. void calc(int x); int main() { int x = 10; calc(x); printf("%d", x); } void calc(int x) { x = x + 10 ; return x; } Output : 20 9/22/2017By MRI 11
  • 12. Call by Reference In this we pass the address of the variable as arguments. In this case the formal parameter can be taken as a reference or a pointer, in both the case they will change the values of the original variable. void calc(int *p); int main() { int x = 10; calc(&x); // passing address of x as argument printf("%d", x); } void calc(int *p) { *p = *p + 10; } Output : 20 9/22/2017By MRI 12
  • 13. Types of User-defined Functions in C++ 9/22/2017By MRI 13
  • 14. 9/22/2017By MRI 14 Example 1: No arguments passed and no return value # include <iostream> using namespace std; void prime(); int main() { prime(); // No argument is passed to prime() return 0; } // Return type of function is void because value is not returned. void prime() { int num, i, flag = 0; cout << "Enter a positive integer enter to check: "; cin >> num; for(i = 2; i <= num/2; ++i) { if(num % i == 0) { flag = 1; break; } } if (flag == 1) { cout << num << " is not a prime number."; } else { cout << num << " is a prime number."; . }
  • 15. Inline function Calling a function generally causes a certain overhead (stacking arguments, jumps, etc...), and thus for very short functions, it may be more efficient to simply insert the code of the function where it is called, instead of performing the process of formally calling a function. 9/22/2017By MRI 15
  • 16. Inline function Preceding a function declaration with the inline specifier informs the compiler that inline expansion is preferred over the usual function call mechanism for a specific function. This does not change at all the behavior of a function, but is merely used to suggest the compiler that the code generated by the function body shall be inserted at each point the function is called, instead of being invoked with a regular function call. 9/22/2017By MRI 16
  • 17. Inline function C++ inline function is powerful concept that is commonly used with classes. If a function is inline, the compiler places a copy of the code of that function at each point where the function is called at compile time. To inline a function, place the keyword inline before the function name and define the function before any calls are made to the function. The compiler can ignore the inline qualifier in case defined function is more than a line. 9/22/2017By MRI 17
  • 18. Inline function Following is an example, which makes use of inline function to return max of two numbers: #include <iostream> using namespace std; inline int Max(int x, int y) { return (x > y)? x : y; } // Main function for the program int main( ) { cout << "Max (20,10): " << Max(20,10) << endl; cout << "Max (0,200): " << Max(0,200) << endl; cout << "Max (100,1010): " << Max(100,1010) << endl; return 0; } 9/22/2017By MRI 18
  • 19. Friend function A friend function of a class is defined outside that class' scope but it has the right to access all private and protected members of the class. Even though the prototypes for friend functions appear in the class definition, friends are not member functions. A friend can be a function, function template, or member function, or a class or class template, in which case the entire class and all of its members are friends. 9/22/2017By MRI 19
  • 20. Friend function To declare a function as a friend of a class, precede the function prototype in the class definition with keyword friend as follows: class Box { double width; public: double length; friend void printWidth( Box box ); void setWidth( double wid ); }; 9/22/2017By MRI 20
  • 21. Friend function #include <iostream> using namespace std; // forward declaration class B; class A { private: int numA; public: A(): numA(12) { } // friend function declaration friend int add(A, B); }; class B { private: int numB; public: B(): numB(1) { } // friend function declaration friend int add(A , B); }; // Function add() is the friend function of classes A and B // that accesses the member variables numA and numB int add(A objectA, B objectB) { return (objectA.numA + objectB.numB); } int main() { A objectA; B objectB; cout<<"Sum: "<< add(objectA, objectB); return 0; } 9/22/2017By MRI 21
  • 22. Friend function In this program, classes A and B have declared add() as a friend function. Thus, this function can access private data of both class. Here, add() function adds the private data numA and numB of two objects objectA and objectB, and returns it to the main function. To make this program work properly, a forward declaration of a class class B should be made as shown in the above example. This is because class B is referenced within the class A using code: friend int add(A , B);. 9/22/2017By MRI 22
  • 23. Recursive function A function that calls itself is known as recursive function. And, this technique is known as recursion. void recurse() { ... .. ... recurse(); ... .. ... } int main() { ... .. ... recurse(); ... .. ... } 9/22/2017By MRI 23
  • 25. Examples of Recursive Function // Factorial of n = 1*2*3*...*n #include <iostream> using namespace std; int factorial(int); int main() { int n; cout<<"Enter a number to find factorial: "; cin >> n; cout << "Factorial of " << n <<" = " << factorial(n); return 0; } int factorial(int n) { if (n > 1) { return n*factorial(n-1); } else { return 1; } } 9/22/2017By MRI 25