SlideShare a Scribd company logo
1 of 8
Function Templates and
Multiple Parameters
Name: Bhushan Lilhare
Roll No: 59
Sub: OOPS C++
Introduction to Function Templates
Function templates are a powerful feature in C++ that allow you to define generic functions that can work with
different types of data. They provide a flexible and reusable solution to avoid code duplication and increase
productivity.
A function template is a blueprint for creating functions that can operate on multiple types. It allows you to
define a generic function once and use it with different data types without having to rewrite the function for
each specific type.
Purpose of Function Templates
The main purpose of function templates is to improve code reusability and maintainability. By using function
templates, you can write a single function that can be used with different types, eliminating the need to write
multiple versions of the same function for each type.
Function templates also provide a way to implement generic algorithms that can work with different data
types. This allows you to write more generic and flexible code that can be used in a wide range of scenarios.
Overall, function templates are a powerful tool in C++ that help you write generic, reusable, and flexible code,
making your programs more efficient and easier to maintain.
Syntax of Function Templates
To declare a function template, you use the template keyword, followed by
the function declaration with a placeholder type. The placeholder type
represents the generic type the function will work with.
When creating a function template, you need to follow a specific syntax. Here's how it looks:
template <typename T>
return_type function_name(parameters) {
// function body
}
As you can see, the add function template works with different types, providing code reusability and flexibility.
Now, let's see an example of a function template:
template <typename T>
T add(T a, T b) {
return a + b;
}
In this example, we have defined a function template named add that takes two parameters of
type T and returns the sum of those parameters. The type T can be any valid C++ type, such as
int, double, or even a user-defined class.
Here's how you can use the add function template:
int result1 = add(5, 10); // result1 = 15
double result2 = add(3.14, 2.71); // result2 = 5.85
Example of Function Templates
Creating a Function Template with Multiple
Parameters
Function templates can also have multiple parameters. By specifying multiple placeholder types, you can
create functions that operate on different types simultaneously.
The syntax a function template with multiple parameters is as follows:
template <typename T1, typename T2, ...>
return_type function_name(parameter1, parameter2, ...) {
// function body
}
Example of a Function Template with Multiple
Parameters
Here's an example that demonstrates how to create a function template with multiple parameters:
template <typename T1, typename T2>
void printValues(T1 value1, T2 value2) {
std::cout << "Value 1: " << value1 << std::endl;
std::cout << "Value 2: " << value2 << std::endl;
}
Here's how you can use the printValues function template:
printValues(10, 3.14); // Output: Value 1: 10 Value 2: 3.14
printValues("Hello", std::string("world")); // Output: Value 1: Hello Value 2:
world
Advantages of Using Function
Templates
Reusability and Flexibility
Function templates allow you to write
code that can be reused with different
data types, improving code flexibility and
reducing redundancy.
Reduced Code Duplication
With function templates, you can avoid
writing similar functions for every data
type. This leads to cleaner and more
maintainable code.
Conclusion
1 Recap of Key Points
Function templates offer flexibility and
reusability in code, leading to more
efficient programming practices.
2 Importance of Function
Templates in Programming
By utilizing function templates,
developers can save time, enhance
code clarity, and increase overall
productivity.

More Related Content

Similar to 59_OOP_Function Template and multiple parameters.pptx

TEMPLATES IN JAVA
TEMPLATES IN JAVATEMPLATES IN JAVA
TEMPLATES IN JAVAMuskanSony
 
Module 3-Functions
Module 3-FunctionsModule 3-Functions
Module 3-Functionsnikshaikh786
 
Visual Basic Review - ICA
Visual Basic Review - ICAVisual Basic Review - ICA
Visual Basic Review - ICAemtrajano
 
Functions in c++, presentation, short and sweet presentation, and details of ...
Functions in c++, presentation, short and sweet presentation, and details of ...Functions in c++, presentation, short and sweet presentation, and details of ...
Functions in c++, presentation, short and sweet presentation, and details of ...Sar
 
Functions assignment
Functions assignmentFunctions assignment
Functions assignmentAhmad Kamal
 
Functions in c language
Functions in c language Functions in c language
Functions in c language tanmaymodi4
 
Functions in c language
Functions in c languageFunctions in c language
Functions in c languageTanmay Modi
 
Advanced Programming C++
Advanced Programming C++Advanced Programming C++
Advanced Programming C++guestf0562b
 
VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1YOGESH SINGH
 
Chapter One Function.pptx
Chapter One Function.pptxChapter One Function.pptx
Chapter One Function.pptxmiki304759
 
Functional Paradigm.pptx
Functional Paradigm.pptxFunctional Paradigm.pptx
Functional Paradigm.pptxFurretMaster
 
Book management system
Book management systemBook management system
Book management systemSHARDA SHARAN
 

Similar to 59_OOP_Function Template and multiple parameters.pptx (20)

TEMPLATES IN JAVA
TEMPLATES IN JAVATEMPLATES IN JAVA
TEMPLATES IN JAVA
 
Presentation 2.pptx
Presentation 2.pptxPresentation 2.pptx
Presentation 2.pptx
 
Templates
TemplatesTemplates
Templates
 
Module 3-Functions
Module 3-FunctionsModule 3-Functions
Module 3-Functions
 
Visual Basic Review - ICA
Visual Basic Review - ICAVisual Basic Review - ICA
Visual Basic Review - ICA
 
Functions in c++, presentation, short and sweet presentation, and details of ...
Functions in c++, presentation, short and sweet presentation, and details of ...Functions in c++, presentation, short and sweet presentation, and details of ...
Functions in c++, presentation, short and sweet presentation, and details of ...
 
Functions assignment
Functions assignmentFunctions assignment
Functions assignment
 
Function
FunctionFunction
Function
 
Template C++ OOP
Template C++ OOPTemplate C++ OOP
Template C++ OOP
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
Functions in c language
Functions in c languageFunctions in c language
Functions in c language
 
Advanced Programming C++
Advanced Programming C++Advanced Programming C++
Advanced Programming C++
 
VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1
 
14 operator overloading
14 operator overloading14 operator overloading
14 operator overloading
 
cp Module4(1)
cp Module4(1)cp Module4(1)
cp Module4(1)
 
Function
FunctionFunction
Function
 
Chapter One Function.pptx
Chapter One Function.pptxChapter One Function.pptx
Chapter One Function.pptx
 
Lecture 11 - Functions
Lecture 11 - FunctionsLecture 11 - Functions
Lecture 11 - Functions
 
Functional Paradigm.pptx
Functional Paradigm.pptxFunctional Paradigm.pptx
Functional Paradigm.pptx
 
Book management system
Book management systemBook management system
Book management system
 

Recently uploaded

Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
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
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 

Recently uploaded (20)

Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
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
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
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🔝
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 

59_OOP_Function Template and multiple parameters.pptx

  • 1. Function Templates and Multiple Parameters Name: Bhushan Lilhare Roll No: 59 Sub: OOPS C++
  • 2. Introduction to Function Templates Function templates are a powerful feature in C++ that allow you to define generic functions that can work with different types of data. They provide a flexible and reusable solution to avoid code duplication and increase productivity. A function template is a blueprint for creating functions that can operate on multiple types. It allows you to define a generic function once and use it with different data types without having to rewrite the function for each specific type. Purpose of Function Templates The main purpose of function templates is to improve code reusability and maintainability. By using function templates, you can write a single function that can be used with different types, eliminating the need to write multiple versions of the same function for each type. Function templates also provide a way to implement generic algorithms that can work with different data types. This allows you to write more generic and flexible code that can be used in a wide range of scenarios. Overall, function templates are a powerful tool in C++ that help you write generic, reusable, and flexible code, making your programs more efficient and easier to maintain.
  • 3. Syntax of Function Templates To declare a function template, you use the template keyword, followed by the function declaration with a placeholder type. The placeholder type represents the generic type the function will work with. When creating a function template, you need to follow a specific syntax. Here's how it looks: template <typename T> return_type function_name(parameters) { // function body }
  • 4. As you can see, the add function template works with different types, providing code reusability and flexibility. Now, let's see an example of a function template: template <typename T> T add(T a, T b) { return a + b; } In this example, we have defined a function template named add that takes two parameters of type T and returns the sum of those parameters. The type T can be any valid C++ type, such as int, double, or even a user-defined class. Here's how you can use the add function template: int result1 = add(5, 10); // result1 = 15 double result2 = add(3.14, 2.71); // result2 = 5.85 Example of Function Templates
  • 5. Creating a Function Template with Multiple Parameters Function templates can also have multiple parameters. By specifying multiple placeholder types, you can create functions that operate on different types simultaneously. The syntax a function template with multiple parameters is as follows: template <typename T1, typename T2, ...> return_type function_name(parameter1, parameter2, ...) { // function body }
  • 6. Example of a Function Template with Multiple Parameters Here's an example that demonstrates how to create a function template with multiple parameters: template <typename T1, typename T2> void printValues(T1 value1, T2 value2) { std::cout << "Value 1: " << value1 << std::endl; std::cout << "Value 2: " << value2 << std::endl; } Here's how you can use the printValues function template: printValues(10, 3.14); // Output: Value 1: 10 Value 2: 3.14 printValues("Hello", std::string("world")); // Output: Value 1: Hello Value 2: world
  • 7. Advantages of Using Function Templates Reusability and Flexibility Function templates allow you to write code that can be reused with different data types, improving code flexibility and reducing redundancy. Reduced Code Duplication With function templates, you can avoid writing similar functions for every data type. This leads to cleaner and more maintainable code.
  • 8. Conclusion 1 Recap of Key Points Function templates offer flexibility and reusability in code, leading to more efficient programming practices. 2 Importance of Function Templates in Programming By utilizing function templates, developers can save time, enhance code clarity, and increase overall productivity.