Some places till now, I've seen function prototypes within functions
instead of in the global declaration space, which I thought was the
way.
I thought it was <I>only</I>:
int myfunction(int, int);
int main(void)
{
int a,b;
return myfunction(a,b);
}
But I've also seen:
int main(void)
{
int a, b, myfunction(int, int);
return myfunction(a,b);
}
I've seen this in K&R as well, but haven't come accross a piece that
talks about this kinda declaration.
What's the deal here? I understand it as follows:
It defines the scope of usage. If defined in the global space, much
like extern variables, the function can be called from anywhere within
the file. If declared within another function, it may be called only
from within that function in which it is defined.
How true? 9 1633
In article <c5**************************@posting.google.com >, es*********@rediffmail.com (Curious Student) wrote: Some places till now, I've seen function prototypes within functions instead of in the global declaration space, which I thought was the way.
I thought it was <I>only</I>:
int myfunction(int, int);
int main(void) { int a,b; return myfunction(a,b); }
But I've also seen:
int main(void) { int a, b, myfunction(int, int); return myfunction(a,b); } I've seen this in K&R as well, but haven't come accross a piece that talks about this kinda declaration. What's the deal here? I understand it as follows:
It defines the scope of usage. If defined in the global space, much like extern variables, the function can be called from anywhere within the file. If declared within another function, it may be called only from within that function in which it is defined.
How true?
That is probably correct. However, it is an extremely bad programming
style. Here is why:
You write functions that are called only from within the file containing
them (to be more precise: From the same compilation unit), and functions
that are called from other places as well. For the first type, you
should use static functions. For the second type, you use extern
functions.
Every caller needs to know how to call a function. Since for extern
functions the caller usually cannot see the definition of the function,
you write a function declaration and put it into a header file, and
every caller should include that header file. You might change the
function parameters at some point in the future; in that case you also
change the declaration in the header file. If the new declaration is not
compatible with the old one, then the compiler will refuse to compile
callers of the function that have not been adapted to the new
parameters.
In the example you wrote, the declaration of myfunction is within main.
If someone changes the definition of myfunction, and also changes the
declaration in the header file, there is a good chance that the
declaration in main is missed. I could have changed it to "myfunction
(double, double, double)", but the compiler would still assume that
myfunction takes two int arguments, so it will not tell you that the
call to myfunction is incorrect, and things will go badly wrong.
You should never have the declaration of a function anywhere but in
exactly _one_ header file.
Christian Bau wrote:
.... snip ... You should never have the declaration of a function anywhere but in exactly _one_ header file.
Never say never :-) What about mutual recursion, e.g.:
static void foo(int bar);
static void fuzz(int ball)
{
printf("fuzz (%d}\n", ball);
if (ball) foo(ball - 1);
}
static void foo(int bar)
{
printf("foo (%d}\n", bar);
if (bar) fuzz(bar - 1);
}
.....
foo(12);
.....
--
fix (vb.): 1. to paper over, obscure, hide from public view; 2.
to work around, in a way that produces unintended consequences
that are worse than the original problem. Usage: "Windows ME
fixes many of the shortcomings of Windows 98 SE". - Hutchison
CBFalconer <cb********@yahoo.com> wrote: Christian Bau wrote:
You should never have the declaration of a function anywhere but in exactly _one_ header file.
Never say never :-) What about mutual recursion, e.g.:
static void foo(int bar);
static void fuzz(int ball) { printf("fuzz (%d}\n", ball); if (ball) foo(ball - 1); }
Not just mutual recursion. I never put any declarations of static
functions in a header; they all go at the top of their own source file.
Nobody else needs to know their declarations; functions in that one
source file do.
Richard
Richard Bos wrote: CBFalconer <cb********@yahoo.com> wrote: Christian Bau wrote:
You should never have the declaration of a function anywhere but in exactly _one_ header file.
Never say never :-) What about mutual recursion, e.g.:
static void foo(int bar);
static void fuzz(int ball) { printf("fuzz (%d}\n", ball); if (ball) foo(ball - 1); }
Not just mutual recursion. I never put any declarations of static functions in a header; they all go at the top of their own source file. Nobody else needs to know their declarations; functions in that one source file do.
That case never arises for me, because I always define functions
before use. Main comes last.
--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
CBFalconer <cb********@yahoo.com> wrote: Richard Bos wrote: Not just mutual recursion. I never put any declarations of static functions in a header; they all go at the top of their own source file. Nobody else needs to know their declarations; functions in that one source file do.
That case never arises for me, because I always define functions before use. Main comes last.
Clearly a case where tastes differ, since I do it exactly the other way
'round. I rarely design top-down, but I do prefer to be able to read my
code in that order.
Richard
Richard Bos wrote: CBFalconer <cb********@yahoo.com> wrote: Richard Bos wrote:
Not just mutual recursion. I never put any declarations of static functions in a header; they all go at the top of their own source file. Nobody else needs to know their declarations; functions in that one source file do.
That case never arises for me, because I always define functions before use. Main comes last.
Clearly a case where tastes differ, since I do it exactly the other way 'round. I rarely design top-down, but I do prefer to be able to read my code in that order.
Hah! And I virtually always design top-down. I also always bottom
post.
--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
> >> Richard Bos wrote: Not just mutual recursion. I never put any declarations of static functions in a header; they all go at the top of their own source file. Nobody else needs to know their declarations; functions in that one source file do.
I justed posted some code like that in the
"Sorting doubles with duplicates" thread.
Curious Student wrote: Some places till now, I've seen function prototypes within functions instead of in the global declaration space, which I thought was the way.
I thought it was <I>only</I>:
int myfunction(int, int);
int main(int argc, char* argv[]) { int a, b; return myfunction(a, b);
return 0; }
But I've also seen:
int main(int argc, char* argv[]) { int a, b, myfunction(int, int); return myfunction(a,b);
return 0; }
I've seen this in K&R as well, but haven't come across a piece that talks about this kind of declaration. What's the deal here? I understand it as follows:
It defines the scope of usage. If defined in the global space, much like extern variables, the function can be called from anywhere within the file. If declared within another function, it may be called only from within that function in which it is defined.
How true?
Suppose that you are writing a "wrapper" for a Fortran function:
double force(double mass, double acceleration) {
extern double force_(const double*, const double*);
return force_(&mass, &acceleration);
}
The idea is that you want C programmers to use function
force(double, double)
instead of function
force_(const double*, const double*)
so the only place that you declare force_(const double*, const double*)
is in force(double, double).
The Fortran function prototype isn't available to programmers.
This doesn't prevent C programmers from calling the Fortran function --
it just makes it a little more difficult and, maybe,
they will get the idea that they shouldn't call it directly.
In <40***************@yahoo.com> CBFalconer <cb********@yahoo.com> writes: Richard Bos wrote: CBFalconer <cb********@yahoo.com> wrote: Richard Bos wrote:
Not just mutual recursion. I never put any declarations of static functions in a header; they all go at the top of their own source file. Nobody else needs to know their declarations; functions in that one source file do.
That case never arises for me, because I always define functions before use. Main comes last.
Clearly a case where tastes differ, since I do it exactly the other way 'round. I rarely design top-down, but I do prefer to be able to read my code in that order.
Hah! And I virtually always design top-down. I also always bottom post.
I design top-down and implement bottom-up. This allows testing each
function as soon as it is written, which minimizes the debugging/testing
of the application (it's much easier to test the correctness of each
function independently of the rest of the application) so debugging the
application boils down to removing the bugs from the main function.
Then again, in a previous life, I was writing embedded control
applications, in assembly, without the benefit of an ICE (in-circuit
emulator) and my *only* debugging tool was an oscilloscope. Much easier
to avoid introducing bugs in the first place than to find them afterwards.
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Brandon J. Van Every |
last post by:
What's better about Ruby than Python? I'm sure there's something. What is
it?
This is not a troll. I'm language shopping and I want people's answers. I
don't know beans about Ruby or have...
|
by: David MacQuigg |
last post by:
I'm concerned that with all the focus on obj$func binding, &closures,
and other not-so-pretty details of Prothon, that we are missing what
is really good - the simplification of classes. There are...
|
by: Andy |
last post by:
Hi All!
We are doing new development for SQL Server 2000 and also moving from
SQL 7.0 to SQL Server 2000.
What are cons and pros for using IDENTITY property as PK in SQL SERVER
2000?
Please,...
|
by: Holly |
last post by:
I'm trying to validate my code and I can't figure out what kind of doctype
I have. The validator can't tell me anything because it can't move beyond
the doctype declaration.
...
|
by: allyn44 |
last post by:
HI--what I am trying to do is 2 things:
1. Open a form in either data entry mode or edit mode depending on
what task the user is performing
2. Cancel events tied to fields on the form if I am in...
|
by: Newbie_sw2003 |
last post by:
Where should I use them?
I am giving you my understandings. Please correct me if I am wrong:
MACRO:
e.g.:#define ref-name 99
The code is substituted by the MACRO ref-name. So no overhead....
|
by: jacob navia |
last post by:
I would like to add at the beginning of the C tutorial I am writing
a short blurb about what "types" are. I came up with the following text.
Please can you comment?
Did I miss something?
Is...
|
by: Larry David |
last post by:
Ok, first of all, let's get the obvious stuff out of the way. I'm an idiot. So please indulge me for a moment. Consider it an act of "community service"....
What does "64bit" mean to your friendly...
|
by: Xah Lee |
last post by:
in March, i posted a essay “What is Expressiveness in a Computer
Language”, archived at:
http://xahlee.org/perl-python/what_is_expresiveness.html
I was informed then that there is a academic...
|
by: antonyliu2002 |
last post by:
I know that this has been asked and answered thousands of times. As a
matter of fact, I know that I need to say
If Not Page.IsPostBack Then
'Do something
End If
for things that needs to be...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |