Archived

This topic is now archived and is closed to further replies.

Function prototypes

This topic is 5624 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Okay, dumb question here, and I''ll probably smack myself and go "Doh!" when I know, but... ...when looking at the DirectX SDK samples... What happened to the prototypes of all those functions? I''ve always just made prototypes for any function I made, assuming they were compulsory in C++, and here the sample codes use no prototypes whatsoever. What''s up with that?

Share this post


Link to post
Share on other sites
If you have the following:

void FunctionA()
{
printf("hello");
}

int main()
{
FunctionA();
}


You don''t need to declare a prototype for FunctionA since it is .. ''above'' the call to the function, in main(). The processor for the compiler finds FunctionA()''s declaration and enters that into its symbol table. When it finds the call to FunctionA() in main(), it looks up in the symbol table and finds a matching function and continues along.

In this case:

void FunctionA()
{
FunctionB();
}

void FunctionB()
{
printf("Hello");
}

int main()
{
FunctionA();
}

The compiler will barf on FunctionB because it hasn''t been declared before it was used.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
header files are filled with prototypes, so when they type something after a #include that means that they are bring in prototypes from other files.

Share this post


Link to post
Share on other sites
Some compilers assume the default form for a function is void NameOfFunction( void ), so if you''re using one of those compilers, you don''t need to declare a prototype for such functions.

Roo, Pantheon Software

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Those would be "C" compilers. And the default function signature is: int func() NOT void func().

... and that''s the C (not C++) meaning of (), i.e. NOT (void) which is the C++ meaning. Empty parentheses in C function declarations means "any number of any type of arguments".

In C++, EVERY function (except main) MUST be prototyped before it is used. Function overloading, anyone?

Check out C/C++ differences/incompatibilities, Bjarne''s book goes into it in detail.

Share this post


Link to post
Share on other sites
Neither my C or C++ compiler insists on prototypes being declared, and the default form is definately void NameOfFunction( void ) or void NameOfFunction(), they aren''t fussy which. Saves a lot of time and messing about, that does.

In C, if you want to pass a variable number of parameters to a function, the prototype form is "type NameOfFunction( ... )", and then you have to use some standard lib functions to retrieve the parameters.

I''ve tried to read Bjarne''s book. I think it''s atrocious.

Roo, Pantheon Software

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
int afunc()
{
return 0;
}

int afunc();

void main(void)
{
afunc(1) ;
afunc(1,2) ;
}

... so how come this compiles in MSVC 6?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
it doesn''t I get

c:\program files\microsoft visual studio\myprojects\test\test.cpp(10) : error C2660: ''afunc'' : function does not take 1 parameters
c:\program files\microsoft visual studio\myprojects\test\test.cpp(11) : error C2660: ''afunc'' : function does not take 2 parameters

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You fell for it!
Try calling the file test.c instead of test.cpp
0 errors, 0 warnings.

Share this post


Link to post
Share on other sites
I was just about to say you''d probably get an error trying to do that. With standard C compilers you need to use the elipsis "..." character to specify a variable number of arguments. That said, both my compilers have compiled your prototype example without any fuss. I assume this is because they treat () the same as (...).

Roo, Pantheon Software

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Yes, that''s correct.
In C, empty parentheses means the same as (...) i.e. turn off all type checking.

Share this post


Link to post
Share on other sites