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?
image

particularly that of line 6

1 Like

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.

5 Likes

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.
codecademyproblem

2 Likes

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) .