Talk About Network

Google





Programming > C - C++ Learning > Re: Using the s...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 5 of 22 Topic 4074 of 4400
Post > Topic >>

Re: Using the same source file from other source files

by Philip Potter <pgp@[EMAIL PROTECTED] > Mar 20, 2008 at 03:31 PM

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
 




 22 Posts in Topic:
Using the same source file from other source files
Hal Vaughan <hal@[EMAI  2008-03-20 07:10:47 
Re: Using the same source file from other source files
Richard Heathfield <rj  2008-03-20 07:25:59 
Re: Using the same source file from other source files
Hal Vaughan <hal@[EMAI  2008-03-20 07:29:28 
Re: Using the same source file from other source files
"Jim Langston"   2008-03-20 00:55:37 
Re: Using the same source file from other source files
Philip Potter <pgp@[EM  2008-03-20 15:31:49 
Re: Using the same source file from other source files
Richard Heathfield <rj  2008-03-20 08:36:26 
Re: Using the same source file from other source files
Hal Vaughan <hal@[EMAI  2008-03-20 08:50:25 
Re: Using the same source file from other source files
Hal Vaughan <hal@[EMAI  2008-03-20 09:28:25 
Re: Using the same source file from other source files
Hal Vaughan <hal@[EMAI  2008-03-20 09:37:01 
Re: Using the same source file from other source files
"Chris ( Val )"  2008-03-21 03:37:07 
Re: Using the same source file from other source files
Philip Potter <pgp@[EM  2008-03-25 17:22:35 
Re: Using the same source file from other source files
Ben Bacarisse <ben.use  2008-03-20 15:08:23 
Re: Using the same source file from other source files
Hal Vaughan <hal@[EMAI  2008-03-20 15:37:26 
Re: Using the same source file from other source files
Ben Bacarisse <ben.use  2008-03-20 15:48:02 
Re: Using the same source file from other source files
Hal Vaughan <hal@[EMAI  2008-03-20 15:56:20 
Re: Using the same source file from other source files
Philip Potter <pgp@[EM  2008-03-20 17:11:20 
Re: Using the same source file from other source files
Hal Vaughan <hal@[EMAI  2008-03-20 17:54:57 
Re: Using the same source file from other source files
Philip Potter <pgp@[EM  2008-03-21 10:03:58 
Re: Using the same source file from other source files
Ben Bacarisse <ben.use  2008-03-20 19:34:41 
Re: Using the same source file from other source files
Hal Vaughan <hal@[EMAI  2008-03-20 20:31:21 
Re: Using the same source file from other source files
Ben Bacarisse <ben.use  2008-03-20 19:45:49 
Re: Using the same source file from other source files
Hal Vaughan <hal@[EMAI  2008-03-20 20:34:08 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
localhost-V2008-12-19 Wed Jan 7 12:22:48 PST 2009.