Advertisement

Function prototypes

Started by July 23, 2002 05:58 PM
9 comments, last by CountOfMonteChristo 22 years, 4 months ago
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?
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.
Advertisement
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.
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
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.
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
Advertisement
int afunc()
{
return 0;
}

int afunc();

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

... so how come this compiles in MSVC 6?
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
You fell for it!
Try calling the file test.c instead of test.cpp
0 errors, 0 warnings.
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

This topic is closed to new replies.

Advertisement