473,796 Members | 2,595 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What's it with this kind of definition?

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?
Nov 14 '05 #1
9 1663
In article <c5************ **************@ posting.google. com>,
es*********@red iffmail.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.
Nov 14 '05 #2
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
Nov 14 '05 #3
CBFalconer <cb********@yah oo.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
Nov 14 '05 #4
Richard Bos wrote:
CBFalconer <cb********@yah oo.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********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 14 '05 #5
CBFalconer <cb********@yah oo.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
Nov 14 '05 #6
Richard Bos wrote:
CBFalconer <cb********@yah oo.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********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 14 '05 #7
> >> 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.
Nov 14 '05 #8
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, &acceleratio n);
}

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.
Nov 14 '05 #9
In <40************ ***@yahoo.com> CBFalconer <cb********@yah oo.com> writes:
Richard Bos wrote:
CBFalconer <cb********@yah oo.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
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

220
19175
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 any preconceived ideas about it. I have noticed, however, that every programmer I talk to who's aware of Python is also talking about Ruby. So it seems that Ruby has the potential to compete with and displace Python. I'm curious on what basis it...
28
3309
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 a number of aspects to this simplification, but for me the unification of methods and functions is the biggest benefit. All methods look like functions (which students already understand). Prototypes (classes) look like modules. This will...
112
10369
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, share your experience in using IDENTITY as PK .
39
2917
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. http://www.wavian.com/clients/pugwash/ Is there anyway to tell what kind of doctype this is? I tried inserting a few different types (please excuse me if this is the stupid way to do it, I am learning...) but am unsuccessful.
6
2148
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 edit mode. The reason I want to do this is becasue when entering a new record the form is entered in data entry mode and I have lots of stuff happening upon entering and leaving fields. In edit mode I do not want the events to fire.
7
23557
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. Execution is faster. Where will it be stotred?(Is it in bss/stack/?) FUNCTION:
51
4568
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 there something wrong in there? -------------------------------------------------------------------- Types A type is a definition for a sequence of storage bits. It gives the meaning of the data stored in memory. If we say that the object a is an
58
30254
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 neighborhood C# programmer? The standard answer I get from computer sales people is: "It means that the CPU can process 64 bits of data at a time instead of 32." Ok... I guess I *kind* of understand what that means at an intuitive level, but what...
669
26237
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 paper written on this subject. On the Expressive Power of Programming Languages, by Matthias Felleisen, 1990. http://www.ccs.neu.edu/home/cobbe/pl-seminar-jr/notes/2003-sep-26/expressive-slides.pdf
11
14831
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 processed by the web server. I am still struggling to understand this postback logic, and hope that some kind gurus out there could help me clarify the confusion I have.
0
9679
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9527
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10453
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10172
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9050
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6785
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5441
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4115
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.