FAQ: Functions: Scope & Flexibility - How to Build Function Templates

This community-built FAQ covers the “How to Build Function Templates” exercise from the lesson “Functions: Scope & Flexibility”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn C++

FAQs on the exercise How to Build Function Templates

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Learn more about how to use this guide.

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Can we Use templates to declare functions in the header file and STILL define them in the cpp file?

Can someone please explain the syntax of the following?

particularly that of line 6


Line 6 is a ternary operator.

A ternary operator is an operator that takes three arguments. The first argument is a comparison argument, the second is the result upon a true comparison, and the third is the result upon a false comparison.

So in this example:
First argument: is num2 smaller than num1?
2nd arument: if 1st argument is true then return num2.
3rd argument: if 1st argument is false then return num1.


In the hint it says if the function is flexible it can be type T as it is below. However, in their example at the very beginning the function isn’t type T and they say it’s flexible. Wondering why the lesson only worked when I made my function type T but their original example isn’t that way.
template typename T>
T some_function_name(T item1, T item2) {
// do stuff with item1 and item2
If you can help I’d really appreciate it. Thanks!

I couldn’t figure out how to do it. Well, I’m pretty sure I got it right because I have basic knowledge and know it’ll work, but it just doesn’t want to work. I wrote down the exact same thing that’s in the view solution part and that doesn’t work when I input it, it only works when the computer does it itself.


Same my solution was verbatim. I even tested a copy and paste of the solution and it wouldn’t work. At least we know.

1 Like

I think it should be made clearer that templates are inline by default.
@llamalead0409476024 It should take arguments (T num1, T num2) .

1 Like

Why is this wrong?

T get_smallest(T num1,T num2) {

return num2 < num1? num2 : num1;


I typed this in and then when it said it was wrong I typed show solution and got this exact same result.

The instruction says to remove everything from numbers.cpp and I removed everything except what was needed in the numbers.hpp. Then, it worked.

What is the point of the .cpp file if we remove everything to condense it down to the .hpp file? I understand the .hpp file serves as a point of reference for the program to pull functions into the main body, but if you’re removing everything from the .cpp file and putting the function directly in the .hpp file then there doesn’t seem to be any reason for one file or the other.

1 Like

The code below is what I entered and when I ran it from numbers.hpp screen there was an error but when I ran the code from main.cpp the code ran without an error. Also, I noticed that in your example you did not have the typename T which may have just been a typo.
Hope this helps,

// Replace these declarations with a template
template typename T
T get_smallest(T num1, T num2){
return num2 < num1? num2 : num1;

Hi,guys.I just wondered why this time the “inline” don’t be needed in the numbers.hpp