Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > function declarations, global/within a function?

Reply
Thread Tools

function declarations, global/within a function?

 
 
Douglas
Guest
Posts: n/a
 
      07-05-2004
Hi,

When and why would you want to...

1)
//=============================
int main(){

int someFunction(void);//declaration

someFunction();

}

int someFunction(void){} //definition

//===============================


instead of.....

2)
//==============================
int someFunction(void); //declaration


int main(){

someFunction(void);

}
int someFunction(void){} //prototype

//================================


Thanks, Douglas
 
Reply With Quote
 
 
 
 
Richard Bos
Guest
Posts: n/a
 
      07-05-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) (Douglas) wrote:

> When and why would you want to...


> int main(){
> int someFunction(void);//declaration
> someFunction();
> }
> int someFunction(void){} //definition


> instead of.....


> int someFunction(void); //declaration
>
> int main(){
> someFunction(void);
> }
> int someFunction(void){} //prototype


You wouldn't, really. I can't think of a single reason why
well-structured could should need that.

Richard
 
Reply With Quote
 
 
 
 
Herbert Rosenau
Guest
Posts: n/a
 
      07-05-2004
On Mon, 5 Jul 2004 11:54:49 UTC, (E-Mail Removed) (Douglas) wrote:

> Hi,
>
> When and why would you want to...
>
> 1)
> //=============================
> int main(){
>
> int someFunction(void);//declaration


This makes the prototype of someFunction only visible in main but not
aywhere in the translation unit before the definition of it occures.

> someFunction();
>
> }
>
> int someFunction(void){} //definition
>
> //===============================
>
>
> instead of.....
>
> 2)
> //==============================
> int someFunction(void); //declaration


This makes the prototype of the function visible throuth the whole
translation unit beginning at this point.

>
> int main(){
>
> someFunction(void);
>
> }
> int someFunction(void){} //prototype
>
> //================================
>
>
> Thanks, Douglas


This makes sense whenever you have a fuction you would use only in a
specific point. Here you says in the first exapmle that only main()
should know how to call somFunction() but no other fuction that is
declared before the definition of someFunction() itself what is a
declaration on itself.

Shortening the visibility of something to the only point it is really
needed in a translation unit can help to find mistakes in coding much
earlier than in golden code. As any try outside the points where the
prototype should be known gives you a diagnostic from the compier, so
you would check if either your code or your design has a flaw long
before you starts testing.



--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Function versus pointer to function, in context of std::function,huh? Alf P. Steinbach C++ 10 07-27-2011 05:51 AM
Function pointer to void function and int function Giannis Papadopoulos C Programming 5 09-05-2005 09:06 PM
How override ALL function calls? (Is there a "function call function"?) seberino@spawar.navy.mil Python 2 08-01-2005 12:38 PM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
Passing a C++ object's member function to a C function expecing a function pointer! James Vanns C++ 7 01-21-2004 02:39 AM



Advertisments