c0dehunter
c0dehunter

Reputation: 6150

Calling function with no declaration in header

Say I have two header and implementation files, A and B.

B:

#include "B.h"

void funcFromB(); //prototype
...
void funcFromB()
{
  ...
}

A:

#include "B.h"

void funcFromB(); //prototype
...
funcFromB(); //will this work correctly?

Will calling funcFromB() from A work correctly if the function is not defined in header of B (B.h)?

Upvotes: 0

Views: 2426

Answers (4)

Kraken
Kraken

Reputation: 24213

The question is clearly a bit in the grey area, but here's my interpretation of it and the solution.

This is your B.h

void funcFromB();

i.e. you have the function signature in the header file.

I am not sure what exactly your A.h file has, but I assume it is not really important for our discussion, so we will leave it to that.

Now your B.c file will contains the implementation of the method funcFromB(). In that case, you don't have to do

void funcFromB(); //prototype

The above line is not required. When you #include your header file, the method signature will be available. So this is how your B.c file will look like.

#include "B.h"

void funcFromB()
{
  ...
}

Now, for you to be able to use this function in A.c, all you need to do is

#include "B.h"
funcFromB(); //This will work correctly

Here, the function will get the signature of the function from B.h, and the implementation is will get from B.c at compile time. All you need to do is, just call the function.

Now, coming to what have we done here. Well, what we wanted to do was to organise our code in a better way. For that, we wanted to use a function, defined in a different file and use it in another file. Now, B.c contains the definition of the function. B.h contains the method signature. If you want to use the function in A.c, you just need to tell this compilation unit, that how does the function look like. And when you link the files together later during compile time, the method implementation will be found out by the compiler on it's own.

Cheers.

Upvotes: 1

Jiminion
Jiminion

Reputation: 5168

Yes, the function will work correctly, provided B.o is linked to A. The .h files and .c files have nothing inherently to do with each other. For clarity purposes, they SHOULD, but nothing forces them to do so. There is no requirement for .h files at all, but coders find them useful to organize information and avoid repeating oneself.

Upvotes: 2

Peter Petrik
Peter Petrik

Reputation: 10165

The code fragment in your question both compiles ("works") and smells

Upvotes: 1

Spade Johnsson
Spade Johnsson

Reputation: 572

It will work,given you define your funFromB() function, preferably in the B.c file; In that case, just #include the B.h file containing the funcFromB() prototype for the preprocessor in your A.c and B.c(this is the one where you will be defining the function) files , and you should be good to go. No need to re-define the function in A.c, nor re-declare the prototype in (any) .c file, as long as it is declared in the b.h file, and every .c file using the function includes the corresponding (B).h file.

PS: Using headers is a great way of improving readability, so I wouldn't skip that part, by declaring the prototype in the .c file. If you want to avoid Headers, you could still just use one single file, let's call it 'master.h', where you would declare ALL the prototypes used by ALL .c files, and include the master.h in all your.c files. Headers tend to help keeping a better overview of how your files are linked to another, and establish the proper dependencies

Upvotes: 0

Related Questions