function declarations, global/within a function?

D

Douglas

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
 
R

Richard Bos

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
 
H

Herbert Rosenau

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,824
Members
47,369
Latest member
FTMZ

Latest Threads

Top