Marvin Irwin
Marvin Irwin

Reputation: 1030

Why define a function in a source file, then declare it in another before using its reference?

I found the following pattern in the DOOM source and I'm not sure what to make of it.

The definition .c file

The declaration and use .c file

enemy.c

// A nice definition, but there is no corresponding header file, kind of weird
void A_Pain (mobj_t* actor)
{
    if (actor->info->painsound)
    S_StartSound (actor, actor->info->painsound);   
}

info.c

// Now it's redeclared, but without a parameter?
void A_Pain();
// ...
state_t states[NUMSTATES] = {
// ...
    {SPR_PLAY,6,4,{A_Pain},S_PLAY,0,0}, // S_PLAY_PAIN2
// ...
}

Why wouldn't a header file be used, when the rest of the code base uses them? Is there an advantage to this method?

Why declare the functions in a different source file with different signatures?

Upvotes: 2

Views: 618

Answers (3)

hobbs
hobbs

Reputation: 240264

Why wouldn't a header file be used?

It doesn't make a real difference, header files are just textual inclusion anyway. It makes sense to put declarations in header files, especially if they're used in several places, but it's not required. As long as every compilation unit agrees on what the name means, things will work fine.

Why declare the functions in a different source file with different signatures?

They don't have different signatures; if they did, there would be a problem. But the declaration in info.c has no parameter list. This is allowed as long as "the parameter list [in the definition] shall not have an ellipsis terminator and the type of each parameter shall be compatible with the type that results from the default argument promotions" (N1256 6.7.5.3, which is probably the wrong version of the spec for application to Doom, but it doesn't really matter). In other words, if a function is declared without a signature, the number and types of its parameters will be inferred from how it's called. As long as this inference is correct, you have a valid program that will work correctly. It's just old-fashioned, and because of the lack of explicitness, a bit harder to maintain.

Upvotes: 0

Govind Parmar
Govind Parmar

Reputation: 21562

In C, the declaration T f() does not declare a function f returning T and taking zero parameters; it declares a function f returning T and accepting an unspecified number of parameters.

You can put function declarations in a source file even if the function definition is in a translation unit that your source file is not aware of, since joining it all together is done by the linker, not the compiler.

Upvotes: 0

CS Pei
CS Pei

Reputation: 11047

This is not good practice I think. But based on my understanding of the C standard, when you declare a function like void A_Pain(); basically that function can take any number of parameters, which is different from C++. So in C you need to use void A_Pain(void); to say it has no parameter.

But in C++, void A_Pain(); means that it takes no parameters.

Upvotes: 3

Related Questions