SlideShare ist ein Scribd-Unternehmen logo
1 von 13
Downloaden Sie, um offline zu lesen
Functions
FUNCTION OVERLOADING
2. PASSING DEFAULT ARGUMENTS
3. INLINE FUNCTION
1.

Compiled By: Kamal Acharya
Function Overloading
 When the function with the same name perform the

multiple task it is called as the function overloading.
 Example: If the function add() can add not only the
integers but also the floats and strings it is
overloaded.
 The functions differ in one of the following things




Number of parameters
Data types of parameters
Order of appearance

Compiled By: Kamal Acharya
Number Of Arguments
#include<iostream.h>
#include<conio.h>
void print(char);
void print(char, int);
int main()
{
clrscr();
print('X');
print('Z',12);
getch();
return 0;
}

Compiled By: Kamal Acharya

void print(char a)
{
cout<<"In the Single argument
function"<<endl;
cout<<a<<endl;
}
void print(char a, int b)
{
int i;
cout<<"In the double argument
function"<<endl;
for(i=0;i<b;i++)
{
cout<<a<<"t";
}
}
Data Types of Parameters
#include<iostream.h>
#include<conio.h>
void add(int , int);
void add(float, float);

int main()
{
clrscr();
add(2,6);
add(12.6,1.2);
getch();
return 0;
}
Compiled By: Kamal Acharya

void add(int a, int b)
{
cout<<"In the integer addition
function"<<endl;
cout<<a+b<<endl;
}
void add(float a, float b)
{
cout<<"In the float addition
function"<<endl;
cout<<a+b<<endl;
}
Oreder Of Appearance
#include<iostream.h>
#include<conio.h>
void fun(int , char);
void fun(char, int);

int main()
{
clrscr();
fun(2,'A');
fun('B',1);
getch();
return 0;
}
Compiled By: Kamal Acharya

void fun(int a, char b)
{
cout<<"In the function with first
argument integer and the second
character"<<endl;
cout<<"The first argument is"<<
a<<".The second argument is "<<
b<<endl;
}
void fun(char a, int b)
{
cout<<"In the function with first
argument character and the second
integer"<<endl;
cout<<"The first argument is"<<
a<<".The second argument is "<<
b<<endl;
}
Passing Default Arguments
•

Allows programmer to define a default
behavior
 A value for a parameter can be
implicitly passed
 Reduces need for similar functions
that differ only in the number of
parameters accepted

Compiled By: Kamal Acharya
 Default parameters must appear after any

mandatory parameters
 Bad example
void Trouble(int x = 5, double z, double y) {
...
}
Cannot come before
mandatory parameters

Compiled By: Kamal Acharya
Consider
void PrintChar(char c = '=', int n = 80)
{
for (int i = 0; i < n; ++i)
cout << c;
}
 What happens in the following invocations?
PrintChar('*', 20);
PrintChar('-');
PrintChar();
Compiled By: Kamal Acharya
Program to Illustrate Default Arguments
#include<iostream.h>
#include<conio.h>
void print(int a= 10 , char b='B');
int main()
{
clrscr();
print();
print(2);
print(1,'Z');
getch();
return 0;
}

Compiled By: Kamal Acharya

void print(int a, char b)
{
int i;
for(i=0;i<a;i++)
{
cout<<b<<"t";
}
}
Inline Function
 Reduce function call overhead—especially for small

functions.
 Qualifier inline before a function’s return type in the
function definition
 “Advises” the compiler to generate a copy of the
function’s code in place (when appropriate) to avoid
a function call.

Compiled By: Kamal Acharya
 Multiple copies of the function code are inserted in

the program (often making the program larger).
 The compiler can ignore the inline qualifier and
typically does so for all but the smallest functions.

Compiled By: Kamal Acharya
Syntax of Inline Function

inline returnType functionName(arguments);

Example:
inline int add(int a, int b);

Compiled By: Kamal Acharya
Program to Illustrate inline Function
#include<iostream.h>
#include<conio.h>
inline float mul(float x, float y)
{
return(x*y);
}
inline double div(double p, double
q)
{
return(p/q);
}
Compiled By: Kamal Acharya

int main()
{
clrscr();
float a=12.12;
float b=2.2;
cout<<mul(a,b)<<"n";
cout<<div(a,b)<<"n";
getch();
return 0;
}

Weitere ähnliche Inhalte

Was ist angesagt?

Function recap
Function recapFunction recap
Function recap
alish sha
 
18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator
SAFFI Ud Din Ahmad
 

Was ist angesagt? (20)

C++ Programming - 3rd Study
C++ Programming - 3rd StudyC++ Programming - 3rd Study
C++ Programming - 3rd Study
 
C++ Programming - 4th Study
C++ Programming - 4th StudyC++ Programming - 4th Study
C++ Programming - 4th Study
 
Learning C++ - Functions in C++ 3
Learning C++ - Functions  in C++ 3Learning C++ - Functions  in C++ 3
Learning C++ - Functions in C++ 3
 
Unit 1 of c++ first program
Unit 1 of c++ first programUnit 1 of c++ first program
Unit 1 of c++ first program
 
Function recap
Function recapFunction recap
Function recap
 
C++ Programming - 11th Study
C++ Programming - 11th StudyC++ Programming - 11th Study
C++ Programming - 11th Study
 
54602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee0108310154602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee01083101
 
Expressions using operator in c
Expressions using operator in cExpressions using operator in c
Expressions using operator in c
 
Decision making and branching
Decision making and branchingDecision making and branching
Decision making and branching
 
Lecture#8 introduction to array with examples c++
Lecture#8 introduction to array with examples c++Lecture#8 introduction to array with examples c++
Lecture#8 introduction to array with examples c++
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
C++ programming function
C++ programming functionC++ programming function
C++ programming function
 
18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator
 
week-18x
week-18xweek-18x
week-18x
 
project report in C++ programming and SQL
project report in C++ programming and SQLproject report in C++ programming and SQL
project report in C++ programming and SQL
 
CPP Language Basics - Reference
CPP Language Basics - ReferenceCPP Language Basics - Reference
CPP Language Basics - Reference
 
C++ functions presentation by DHEERAJ KATARIA
C++ functions presentation by DHEERAJ KATARIAC++ functions presentation by DHEERAJ KATARIA
C++ functions presentation by DHEERAJ KATARIA
 
Practical basics on c++
Practical basics on c++Practical basics on c++
Practical basics on c++
 
Fun with Lambdas: C++14 Style (part 1)
Fun with Lambdas: C++14 Style (part 1)Fun with Lambdas: C++14 Style (part 1)
Fun with Lambdas: C++14 Style (part 1)
 
Practical Class 12th (c++programs+sql queries and output)
Practical Class 12th (c++programs+sql queries and output) Practical Class 12th (c++programs+sql queries and output)
Practical Class 12th (c++programs+sql queries and output)
 

Andere mochten auch (6)

Matrix transapose in c++
Matrix transapose in c++Matrix transapose in c++
Matrix transapose in c++
 
Inline function(oops)
Inline function(oops)Inline function(oops)
Inline function(oops)
 
Inline function in C++
Inline function in C++Inline function in C++
Inline function in C++
 
inline function
inline function inline function
inline function
 
Inline function in C++
Inline function in C++Inline function in C++
Inline function in C++
 
Inline function
Inline functionInline function
Inline function
 

Ähnlich wie Functions

Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
Kamal Acharya
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
Kamal Acharya
 
Modify this code to use multiple threads with the same data1.Modif.pdf
Modify this code to use multiple threads with the same data1.Modif.pdfModify this code to use multiple threads with the same data1.Modif.pdf
Modify this code to use multiple threads with the same data1.Modif.pdf
mallik3000
 

Ähnlich wie Functions (20)

Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Lecture 8- Data Input and Output
Lecture 8- Data Input and OutputLecture 8- Data Input and Output
Lecture 8- Data Input and Output
 
An imperative study of c
An imperative study of cAn imperative study of c
An imperative study of c
 
2014 computer science_question_paper
2014 computer science_question_paper2014 computer science_question_paper
2014 computer science_question_paper
 
c++ practical Digvajiya collage Rajnandgaon
c++ practical  Digvajiya collage Rajnandgaonc++ practical  Digvajiya collage Rajnandgaon
c++ practical Digvajiya collage Rajnandgaon
 
C++ Functions.ppt
C++ Functions.pptC++ Functions.ppt
C++ Functions.ppt
 
12th CBSE Practical File
12th CBSE Practical File12th CBSE Practical File
12th CBSE Practical File
 
Lecture 3 c++
Lecture 3 c++Lecture 3 c++
Lecture 3 c++
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
 
Dynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Dynamic Objects,Pointer to function,Array & Pointer,Character String ProcessingDynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Dynamic Objects,Pointer to function,Array & Pointer,Character String Processing
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
operator overloading
operator overloadingoperator overloading
operator overloading
 
C multiple choice questions and answers pdf
C multiple choice questions and answers pdfC multiple choice questions and answers pdf
C multiple choice questions and answers pdf
 
Operator overloading2
Operator overloading2Operator overloading2
Operator overloading2
 
Modify this code to use multiple threads with the same data1.Modif.pdf
Modify this code to use multiple threads with the same data1.Modif.pdfModify this code to use multiple threads with the same data1.Modif.pdf
Modify this code to use multiple threads with the same data1.Modif.pdf
 
C++ functions
C++ functionsC++ functions
C++ functions
 
C++ functions
C++ functionsC++ functions
C++ functions
 
C++ Functions.ppt
C++ Functions.pptC++ Functions.ppt
C++ Functions.ppt
 
power point presentation on object oriented programming functions concepts
power point presentation on object oriented programming functions conceptspower point presentation on object oriented programming functions concepts
power point presentation on object oriented programming functions concepts
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 

Mehr von Kamal Acharya

Mehr von Kamal Acharya (20)

Programming the basic computer
Programming the basic computerProgramming the basic computer
Programming the basic computer
 
Computer Arithmetic
Computer ArithmeticComputer Arithmetic
Computer Arithmetic
 
Introduction to Computer Security
Introduction to Computer SecurityIntroduction to Computer Security
Introduction to Computer Security
 
Session and Cookies
Session and CookiesSession and Cookies
Session and Cookies
 
Functions in php
Functions in phpFunctions in php
Functions in php
 
Web forms in php
Web forms in phpWeb forms in php
Web forms in php
 
Making decision and repeating in PHP
Making decision and repeating  in PHPMaking decision and repeating  in PHP
Making decision and repeating in PHP
 
Working with arrays in php
Working with arrays in phpWorking with arrays in php
Working with arrays in php
 
Text and Numbers (Data Types)in PHP
Text and Numbers (Data Types)in PHPText and Numbers (Data Types)in PHP
Text and Numbers (Data Types)in PHP
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
 
Capacity Planning of Data Warehousing
Capacity Planning of Data WarehousingCapacity Planning of Data Warehousing
Capacity Planning of Data Warehousing
 
Data Warehousing
Data WarehousingData Warehousing
Data Warehousing
 
Search Engines
Search EnginesSearch Engines
Search Engines
 
Web Mining
Web MiningWeb Mining
Web Mining
 
Information Privacy and Data Mining
Information Privacy and Data MiningInformation Privacy and Data Mining
Information Privacy and Data Mining
 
Cluster Analysis
Cluster AnalysisCluster Analysis
Cluster Analysis
 
Association Analysis in Data Mining
Association Analysis in Data MiningAssociation Analysis in Data Mining
Association Analysis in Data Mining
 
Classification techniques in data mining
Classification techniques in data miningClassification techniques in data mining
Classification techniques in data mining
 
Data Preprocessing
Data PreprocessingData Preprocessing
Data Preprocessing
 
Introduction to Data Mining and Data Warehousing
Introduction to Data Mining and Data WarehousingIntroduction to Data Mining and Data Warehousing
Introduction to Data Mining and Data Warehousing
 

Kürzlich hochgeladen

Kürzlich hochgeladen (20)

Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 

Functions

  • 1. Functions FUNCTION OVERLOADING 2. PASSING DEFAULT ARGUMENTS 3. INLINE FUNCTION 1. Compiled By: Kamal Acharya
  • 2. Function Overloading  When the function with the same name perform the multiple task it is called as the function overloading.  Example: If the function add() can add not only the integers but also the floats and strings it is overloaded.  The functions differ in one of the following things    Number of parameters Data types of parameters Order of appearance Compiled By: Kamal Acharya
  • 3. Number Of Arguments #include<iostream.h> #include<conio.h> void print(char); void print(char, int); int main() { clrscr(); print('X'); print('Z',12); getch(); return 0; } Compiled By: Kamal Acharya void print(char a) { cout<<"In the Single argument function"<<endl; cout<<a<<endl; } void print(char a, int b) { int i; cout<<"In the double argument function"<<endl; for(i=0;i<b;i++) { cout<<a<<"t"; } }
  • 4. Data Types of Parameters #include<iostream.h> #include<conio.h> void add(int , int); void add(float, float); int main() { clrscr(); add(2,6); add(12.6,1.2); getch(); return 0; } Compiled By: Kamal Acharya void add(int a, int b) { cout<<"In the integer addition function"<<endl; cout<<a+b<<endl; } void add(float a, float b) { cout<<"In the float addition function"<<endl; cout<<a+b<<endl; }
  • 5. Oreder Of Appearance #include<iostream.h> #include<conio.h> void fun(int , char); void fun(char, int); int main() { clrscr(); fun(2,'A'); fun('B',1); getch(); return 0; } Compiled By: Kamal Acharya void fun(int a, char b) { cout<<"In the function with first argument integer and the second character"<<endl; cout<<"The first argument is"<< a<<".The second argument is "<< b<<endl; } void fun(char a, int b) { cout<<"In the function with first argument character and the second integer"<<endl; cout<<"The first argument is"<< a<<".The second argument is "<< b<<endl; }
  • 6. Passing Default Arguments • Allows programmer to define a default behavior  A value for a parameter can be implicitly passed  Reduces need for similar functions that differ only in the number of parameters accepted Compiled By: Kamal Acharya
  • 7.  Default parameters must appear after any mandatory parameters  Bad example void Trouble(int x = 5, double z, double y) { ... } Cannot come before mandatory parameters Compiled By: Kamal Acharya
  • 8. Consider void PrintChar(char c = '=', int n = 80) { for (int i = 0; i < n; ++i) cout << c; }  What happens in the following invocations? PrintChar('*', 20); PrintChar('-'); PrintChar(); Compiled By: Kamal Acharya
  • 9. Program to Illustrate Default Arguments #include<iostream.h> #include<conio.h> void print(int a= 10 , char b='B'); int main() { clrscr(); print(); print(2); print(1,'Z'); getch(); return 0; } Compiled By: Kamal Acharya void print(int a, char b) { int i; for(i=0;i<a;i++) { cout<<b<<"t"; } }
  • 10. Inline Function  Reduce function call overhead—especially for small functions.  Qualifier inline before a function’s return type in the function definition  “Advises” the compiler to generate a copy of the function’s code in place (when appropriate) to avoid a function call. Compiled By: Kamal Acharya
  • 11.  Multiple copies of the function code are inserted in the program (often making the program larger).  The compiler can ignore the inline qualifier and typically does so for all but the smallest functions. Compiled By: Kamal Acharya
  • 12. Syntax of Inline Function inline returnType functionName(arguments); Example: inline int add(int a, int b); Compiled By: Kamal Acharya
  • 13. Program to Illustrate inline Function #include<iostream.h> #include<conio.h> inline float mul(float x, float y) { return(x*y); } inline double div(double p, double q) { return(p/q); } Compiled By: Kamal Acharya int main() { clrscr(); float a=12.12; float b=2.2; cout<<mul(a,b)<<"n"; cout<<div(a,b)<<"n"; getch(); return 0; }