Jim Langston wrote:
> Hal Vaughan wrote:
>> Richard Heathfield wrote:
>>> Hal Vaughan said:
>>>
> [SNIP]
>>>> How can I make sure several files can access the functions in a
>>>> separate file without getting repeated definitions?
>>> In the source, you simply declare (but not define) the functions in a
>>> header, and #include the header into the sources that need to call
>>> those functions (and the source that defines them, as a safety
>>> check).
>> So I create protofunctions in a header and include that instead of the
>> actual source file, right?
>
> Yes. The techinical term borrowed from C is "prototype". A
fiunction/class
> declaration is a prototype. I.E.
>
> int Foo(int);
> float Bar(float MyVar);
> void Bax();
>
> are all prototypes.
Nit: the last one is not a prototype in C, only in C++. It is a
declaration in both languages. This is one of those small-but-annoying
differences between C and C++.
Roughly speaking, a declaration declares the existance of an object or
function, and a prototype is a function declaration which *also*
specifies the number and type of parameters. In C++, all function
declarations are prototypes - void Bax(); is a declaration which states
that Bax() is a function taking no arguments and returning nothing. In
C, there exist function declarations which aren't prototypes - void
Bax(); is a declaration that Bax() is a function returning nothing but
it says nothing about the number and types of parameters. Bax() could
actually take 15 'char *' and two 'FILE *' parameters if the programmer
decided. If you had wanted to specify that Bax() took no arguments, you
would say void Bax(void); - which in C++ is legal and has the same
meaning but is considered poor style [except in code which is intended
to compile as both C and C++].
If you're not interested in learning C, it's probably safe to ignore the
above. Nevertheless, I consider it sensible to be aware of these
differences between the two closely-related languages.
> They declare functions, being function
declarations,
> but do not define them. It simply tells the compiler that these
functions
> are somewhere, and this the value they return and the parameters they
> accept.
>
> int Foo( int X )
> {
> return X * 3;
> }
>
> is a function declaration. You can only have one declaration for a
function
ITYM "definition". It is also a declaration, but the fact that it is a
definition is what makes it different from the previous examples.
> in a program. (With the exception of inline functions, but there's
always
> an exception to every rule, isn't there?).
>
Phil


|