FAQ: Functions: Scope & Flexibility - Getting a Header Yourself

This community-built FAQ covers the “Getting a Header Yourself” 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 Getting a Header Yourself

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 move the files from fns.cpp (in the exercise) to fns.hpp… So that we wouldn’t be required to compile 2 files at the time of execution?

or is it advised to just declare functions in hpp?

More generally speaking, Can we move the definitions from Example.cpp to Example.hpp Right below the declarations so we spare ourselves an additional compilation?

Why do we not just include “my_functions.cpp” at the top of the file in the first place? I tried running that and it worked without the declarations. Why do we need a header file with separate declarations? I read something on Stack Overflow about “Multiple Definitions” being an issue, but I’m not sure I fully grasp the idea of what that means.

1 Like

I am not enough experienced, but I think if we do that, sometimes the compiler may throw “Multiple Definition Error”. May be that we define a function one of a kind in the current file (with same number of parameters and data type) and that function is defined in a different kind in the included CPP file.

Consider the following:

int calc(int a, int b) {return a+b;} is defined in the current file and

int calc(int a, int b) {return a-b;} is defined in the #included file ?

Did it helped ?? @tatemauzy1668138550

Hmm, I think that helps, although, there’s one thing that still confuses me a little bit about this.

Let’s look at the example you just gave:

int calc(int a, int b) {return a+b;} [call this line A] is defined in our main file, calc.cpp, and

int calc(int a, int b) {return a-b;} [call this line B] is defined in some file attachment.cpp

Then, say you do this:

int calc(int a, int b) is delcared in some header file attachment.h , which is #included in calc.cpp

If this was the case, and you had the two cpp files linked when you ran the g++ command, would you still get a Multiple Definition Error? Or would the interpreter choose one definition over the other?


Have a look at the pic at the top, it throws a Multiple Definition error as
expected. Now let me go to your point. Below is the code you probably wanted.!

code
Now, calc is defined in another file as well as declared in the header file. It throws a similar Error, though in a different manner.

Hope you got it now :slight_smile: So, the final outcome is it’s always best to avoid multiple defintions.