ProDeveloperTutorial.com

Tutorials and Programming Solutions
Menu
  • Shell Scripting
  • System Design
  • Linux System Programming
  • 4g LTE
  • Coding questions
  • C
  • C++
  • DSA
  • GIT
  • 450 DSA Cracker
  • 5G NR
  • O-RAN

CPP chapter 41: Function Template in CPP

prodevelopertutorial February 26, 2020

In this tutorial we shall learn about below topics

1. Introduction to Function template

2. Syntax for function template:

3. Example for simple function template

4. Function Template with more arguments

5. Overloading function template

6. Recursion with function template

 

1. Introduction to Function template

 

The function defined outside a class is called as normal functions.

Generally normal function will work with only 1 data type.

But function template will work with any kind of data types.

 

2. Syntax for function template:

 

  1. “template” is the keyword used to declare a template.
  2. Followed by list of arguments.

Syntax:

template < class T>

void myFunc( T num)

{




}

 

So from the line “template <class T>”. “template” is the keyword used. “class T”, is the argument list. If you want to have more arguments, then you need to give “class T, class S”.

It is not mandatory to give capital letter for the argument name, but it is a common practice.

 

3. Example for simple function template

In the below program we shall see a simple function accepting one argument and checking if the value is greater than 10 or not.

 

We test this function by calling the function with different data type values.

 

#include <iostream>

using namespace std;


template <class T>

void compare(T a)

{

    if (a > 10)

    {

        cout<<a<<" is greater than 10"<<endl;

    }

    else

    {

        cout<<a<<" is lesser than 10"<<endl;

    }

}




int main(void)

{

    compare(4);

    compare(10.34);

    compare(-6);

}

 

Output:

 

4 is lesser than 10

10.34 is greater than 10

-6 is lesser than 10

 

4. Function Template with more arguments

 

It is also possible to create a function with more arguments. It can be done as shown below;

 

#include <iostream>

using namespace std;



template <class T, class S>

void compare(T a, S b)

{

    if (a > b)

    {

        cout<<a<<" is greater than "<<b<<endl;

    }

    else

    {

        cout<<a<<" is lesser than "<<b<<endl;

    }
}




int main(void)

{

    compare(4, 5);

    compare(10.34, 6);

    compare(-6, 10.34);

}

 

Output:

 

4 is lesser than 5

10.34 is greater than 6

-6 is lesser than 10.34

 

5. Overloading function template

 

We can also overload the function template.

Implicit conversion will not be carried out during overloading of template function.

If no accurate match is found, error will be returned.

Normal function match will be given higher precedence than template function match.

Example:

 

#include <iostream>
using namespace std;



template <class T>

void display(T a)

{
    cout<<"Template function is called for "<<a<<endl;
}


void display(int a)
{
    cout<<"Integer function is called for "<<a<<endl;
}



int main(void)
{

    display(4);

    display(10.34);

    display('A');

}

 

Output:

 

Integer function is called for 4

Template function is called for 10.34

Template function is called for A

 

6. Recursion with function template

 

Template function will also supports recursion.

In below example we shall see how to get factorial of a number using template and recursion.

#include <iostream>

using namespace std;

template <class T>

long int factorial(T n)

{

    if (n>=1)

        return n*factorial(n-1);

    else

        return 1;

}

int main(void)

{

    int num = 5;

    cout<<"The factorial of "<<num <<" using template recursion is "<<factorial(num)<<endl;

}

 

Output:

The factorial of 5 using template recursion is 120

 

List Of Tutorials available in this website:

C Programming 20+ ChaptersC++ Programming 80+ Chapters
100+ Solved Coding QuestionsData Structures and Algorithms 85+ Chapters
System design 20+ ChaptersShell Scripting 12 Chapters
4g LTE 60+ ChaptersMost Frequently asked Coding questions
5G NR 50+ ChaptersLinux System Programming 20+ chapters
Share
Email
Tweet
Linkedin
Reddit
Stumble
Pinterest
Prev Article
Next Article

About The Author

prodevelopertutorial

Follow this blog to learn more about C, C++, Linux, Competitive Programming concepts, Data Structures.

Leave a Reply Cancel Reply

You must be logged in to post a comment.

ProDeveloperTutorial.com

Tutorials and Programming Solutions
Copyright © 2023 ProDeveloperTutorial.com
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
Do not sell my personal information.
Cookie SettingsAccept
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT