473,327 Members | 2,112 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Prototyping rules/recommendations sought

rs
Dear All,
I have a question regarding proptypes for functions. What is the
recommended practice? The way I do it is to put all my external functions
in a header file, while protyping internal (file scope) functions at the
start of the source file. I've seen many people (especially using gcc
under linux) don't botehr prototyping internal functions but just declare
them inline so to speak.

Is there any recommendations advantages/disdvantages of the approach? It
seems to be that there is a tradeoff here:

1. If the prototypes are going to help me by catching incorrect parameter
types,numbers etc.. they are worth having

2. However its a hassle to maintain them.

So, the question is, are most modern compilers able to catch type/number
errors so that I can avoid prototyping internal functions? What are your
suggestions?

Nov 13 '05 #1
13 1661
rs wrote:
So, the question is, are most modern compilers able to catch type/number
errors so that I can avoid prototyping internal functions? What are your
suggestions?


If a function has no declaration, only a definition, and is used before
it is defined, it gets used without a prototype. Some compilers may
give a warning if the expected prototype is wrong, but they may not
infer the prototype from the later definition. So you should prototype
such static functions. Other static functions need no declarations.

Of course, you might forget that you are using a static function before
declaring it unless the compiler warns about implicitly declared
functions (e.g. with the gcc -Wimplicit option), so you may prefer to
declare all static functions anyway.

--
Hallvard
Nov 13 '05 #2

"rs" <no************@talk21.com> schrieb im Newsbeitrag
news:bq**********@pegasus.csx.cam.ac.uk...
Dear All,
I have a question regarding proptypes for functions. What is the
recommended practice? The way I do it is to put all my external functions
in a header file, while protyping internal (file scope) functions at the
start of the source file. I've seen many people (especially using gcc
under linux) don't botehr prototyping internal functions but just declare
them inline so to speak.

Is there any recommendations advantages/disdvantages of the approach? It
seems to be that there is a tradeoff here:

1. If the prototypes are going to help me by catching incorrect parameter
types,numbers etc.. they are worth having

2. However its a hassle to maintain them.

So, the question is, are most modern compilers able to catch type/number
errors so that I can avoid prototyping internal functions? What are your
suggestions?


Not a suggestion (who am I to give suggestions)..
I write the functions in a compilation unit "upside down" and put protoypes
of only those functions which are called from a different compilation unit
into a header:

in somecode.c:

int foo(void)
{
return 1;
}

double bar(double some_val)
{
return some_val / 1.234;
}

int main(void)
{
int int_val = foo();
double my_dbl = bar(2.55);
return 0;
}

in somecode.h i have only

double bar(double);
because foo() is used only in somecode.c, but bar() is called from
someothercode.c as well.

just my <whatever currency you like> 0.02
Robert
Nov 13 '05 #3
"Robert Stankowic" <pc******@netway.at> wrote:
"rs" <no************@talk21.com> schrieb: <snip>
So, the question is, are most modern compilers able to catch type/number
errors so that I can avoid prototyping internal functions? What are your
suggestions?


Not a suggestion (who am I to give suggestions)..
I write the functions in a compilation unit "upside down" and put protoypes
of only those functions which are called from a different compilation unit
into a header:

in somecode.c:

int foo(void)
{
return 1;
}


You may want to change this to:

static int foo(void)
...

<snip> just my <whatever currency you like> 0.02


I added mine, that makes 0.04. ;-)

Regards
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #4
rs wrote:
Dear All,
<snip>
1. If the prototypes are going to help me by catching incorrect parameter
types,numbers etc.. they are worth having <snip> So, the question is, are most modern compilers able to catch type/number
errors so that I can avoid prototyping internal functions? What are your
suggestions?


Quoting the C90 & C99 standards: A function prototype is a declaration
of a function that declares the types of its parameters.

We end up with the interesting effect that the definition of a function
is also a prototype:

/* Both definition and prototype! */
static int foo(char *p)
{
/* ... */
return 42;
}

int main(void)
{
char *p;
foo(p); /* Prototype in scope, full type checking */
return 0;
}

Many experienced programmers do this whenever possible for static
functions. As you noted, why maintain function prototypes for static
functions when you don't have to?
Mark F. Haigh
mf*****@sbcglobal.net
Nov 13 '05 #5

"Irrwahn Grausewitz" <ir*******@freenet.de> schrieb im Newsbeitrag
news:gi********************************@4ax.com...
"Robert Stankowic" <pc******@netway.at> wrote:
"rs" <no************@talk21.com> schrieb: <snip>
So, the question is, are most modern compilers able to catch type/number errors so that I can avoid prototyping internal functions? What are your suggestions?


Not a suggestion (who am I to give suggestions)..
I write the functions in a compilation unit "upside down" and put protoypes of only those functions which are called from a different compilation unit into a header:

in somecode.c:

int foo(void)
{
return 1;
}


You may want to change this to:

static int foo(void)
...


Yes, thank you.

<snip>
just my <whatever currency you like> 0.02


I added mine, that makes 0.04. ;-)


100% in less than one day.. not so bad, really :)

Regards
Robert
Nov 13 '05 #6
rs
Does this apply only to functions that are defined BEFORE they are used or
also to those defined after they are used?
"Mark F. Haigh" <mf*****@sbcglobal.ten> wrote in message
news:r7*******************@newssvr25.news.prodigy. com...
rs wrote:
Dear All,

<snip>

1. If the prototypes are going to help me by catching incorrect parameter types,numbers etc.. they are worth having

<snip>
So, the question is, are most modern compilers able to catch type/number
errors so that I can avoid prototyping internal functions? What are your suggestions?


Quoting the C90 & C99 standards: A function prototype is a declaration
of a function that declares the types of its parameters.

We end up with the interesting effect that the definition of a function
is also a prototype:

/* Both definition and prototype! */
static int foo(char *p)
{
/* ... */
return 42;
}

int main(void)
{
char *p;
foo(p); /* Prototype in scope, full type checking */
return 0;
}

Many experienced programmers do this whenever possible for static
functions. As you noted, why maintain function prototypes for static
functions when you don't have to?
Mark F. Haigh
mf*****@sbcglobal.net

Nov 13 '05 #7
Robert Stankowic wrote:
"rs" <no************@talk21.com> schrieb im Newsbeitrag

I have a question regarding proptypes for functions. What is the
recommended practice? The way I do it is to put all my external
functions in a header file, while protyping internal (file scope)
functions at the start of the source file. I've seen many people
(especially using gcc under linux) don't botehr prototyping
internal functions but just declare them inline so to speak.

Is there any recommendations advantages/disdvantages of the
approach? It seems to be that there is a tradeoff here:

1. If the prototypes are going to help me by catching incorrect
parameter types,numbers etc.. they are worth having

2. However its a hassle to maintain them.

So, the question is, are most modern compilers able to catch
type/number errors so that I can avoid prototyping internal
functions? What are your suggestions?


Not a suggestion (who am I to give suggestions)..
I write the functions in a compilation unit "upside down" and put
protoypes of only those functions which are called from a different
compilation unit into a header:

in somecode.c:

int foo(void)
{
return 1;
}

double bar(double some_val)
{
return some_val / 1.234;
}

int main(void)
{
int int_val = foo();
double my_dbl = bar(2.55);
return 0;
}

in somecode.h i have only

double bar(double);
because foo() is used only in somecode.c, but bar() is called from
someothercode.c as well.


In this case you should also declare foo() as being static, to
avoid polluting the external name space. The general rule is that
the header contains only things meant to be visible to other
modules. Because of the context sensitive meaning of static, you
should use it for all declarations visible over file scope that
are NOT intended to be visible externally.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 13 '05 #8
"rs" <no************@talk21.com> wrote:

[Please don't top-post; fixed.]
"Mark F. Haigh" <mf*****@sbcglobal.ten> wrote:

<snip>
Quoting the C90 & C99 standards: A function prototype is a declaration
of a function that declares the types of its parameters.

We end up with the interesting effect that the definition of a function
is also a prototype:

/* Both definition and prototype! */
static int foo(char *p)
{
/* ... */
return 42;
}

int main(void)
{
char *p;
foo(p); /* Prototype in scope, full type checking */
return 0;
}

Many experienced programmers do this whenever possible for static
functions. As you noted, why maintain function prototypes for static
functions when you don't have to?


Does this apply only to functions that are defined BEFORE they are used or
also to those defined after they are used?


To have a prototype in scope when a function is called, the function
has to be either defined (as a function definition serves as prototype
as well) or explicitly prototyped /before/ used.

Mark's example above is of the first kind: definition before use.

The same example, using a separate prototype:

/* Prototype: */
static int foo(char *);

int main(void)
{
char *p;
foo(p); /* Prototype in scope, full type checking */
return 0;
}

/* Note that the definition of foo can now savely be
moved to the end of the file: */

static int foo(char *p)
{
/* ... */
return 42;
}

If you omit the prototype in this example, the compiler cannot
perform full type checking.

HTH
Regards
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #9
On Sat, 29 Nov 2003 18:31:15 +0100
Irrwahn Grausewitz <ir*******@freenet.de> wrote:
"rs" <no************@talk21.com> wrote:

[Please don't top-post; fixed.]
"Mark F. Haigh" <mf*****@sbcglobal.ten> wrote:

<snip>
Quoting the C90 & C99 standards: A function prototype is a
declaration of a function that declares the types of its
parameters.

We end up with the interesting effect that the definition of a
function is also a prototype:

/* Both definition and prototype! */
static int foo(char *p)
{
/* ... */
return 42;
}

int main(void)
{
char *p;
foo(p); /* Prototype in scope, full type checking */
return 0;
}

Many experienced programmers do this whenever possible for static
functions. As you noted, why maintain function prototypes for
static functions when you don't have to?


Does this apply only to functions that are defined BEFORE they are
used or also to those defined after they are used?


To have a prototype in scope when a function is called, the function
has to be either defined (as a function definition serves as prototype
as well) or explicitly prototyped /before/ used.


<snip>

<mode=awkward sod>
Not all function definitions provide prototypes.
static int foo(*p)
char *p
{
/* ... */
return 42;
}

does not provide a prototype. This, of course, is a very good reason for
never using this type of function definition.
</mode>
--
Mark Gordon
Paid to be a Geek & a Senior Software Developer
Although my email address says spamtrap, it is real and I read it.
Nov 13 '05 #10
Mark Gordon <sp******@flash-gordon.me.uk> wrote:
Irrwahn Grausewitz <ir*******@freenet.de> wrote:

<snip>
To have a prototype in scope when a function is called, the function
has to be either defined (as a function definition serves as prototype
as well) or explicitly prototyped /before/ used.


<snip>

<mode=awkward sod>
Not all function definitions provide prototypes.
static int foo(*p)
char *p
{
/* ... */
return 42;
}

does not provide a prototype. This, of course, is a very good reason for
never using this type of function definition.
</mode>


Yes, you are right. I stopped using "old-style" function definitions
years ago, and almost forgot it ever existed.

Regards
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #11
Mark Gordon wrote:
Not all function definitions provide prototypes.
static int foo(*p)
char *p
{
/* ... */
return 42;
}

does not provide a prototype. This, of course, is a very good reason for
never using this type of function definition.


Very good point.

<OT>
Some compilers have switches to warn you when this occurs. For gcc,
it's -Wstrict-prototypes, which will produce something like:

foo.c:13: warning: function declaration isn't a prototype
</OT>
Mark F. Haigh
mf*****@sbcglobal.net
Nov 13 '05 #12
In article <20******************************@flash-gordon.me.uk>,
sp******@flash-gordon.me.uk says...
<mode=awkward sod>
Not all function definitions provide prototypes.
static int foo(*p)
char *p
{
/* ... */
return 42;
}


Not all function compile...
Nov 13 '05 #13
On Mon, 1 Dec 2003 16:48:47 -0000
twalker <tr****@emtex.com> wrote:
In article <20******************************@flash-gordon.me.uk>,
sp******@flash-gordon.me.uk says...
<mode=awkward sod>
Not all function definitions provide prototypes.
static int foo(*p)
char *p
{
/* ... */
return 42;
}


Not all function compile...


I put in an extra * and missed a ;. It should have been

static int foo(p)
char *p;
{
/* ... */
return 42;
}

That'll teach me to compile first.
--
Mark Gordon
Paid to be a Geek & a Senior Software Developer
Although my email address says spamtrap, it is real and I read it.
Nov 13 '05 #14

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

Similar topics

30
by: Dave Allison | last post by:
Oh no, not another "check out my cool new language" posting :-) For about 5 years now, I have been developing a scripting/prototyping language that is now available on the net. It's called...
9
by: Carl | last post by:
I have been using Python for quite some time now and I love it. I use it mainly for explorative computing and numerical prototyping, ie testing and trying out different kinds of algorithms and...
1
by: Will | last post by:
function obj1() { this.children; this.Children = function() { if(typeof(this.children) === 'undefined') { this.children = new col(); } return this.children; }
51
by: Matt | last post by:
Hello, I'm a hiring C++ developer employer looking for existing, online C++ aptitude tests. I have not yet extensively researched this yet, but as an example, I thought this test looked...
1
by: cainlevy | last post by:
Hey all, What are the pros and cons of defining methods in the constructor vs through the prototype? For example: Constructing: ------------- function MyObj() { this.MyMethod = function()...
4
by: Tilted | last post by:
Does anyone here use prototyping tools? I'm building one myself as I feel like I'm doing the same thing over and over again with the majority of my projects, how do people generally feel about...
0
by: jmathesius | last post by:
Can anyone recommend any articles or links that might explain the best way of externalizing business rules? I'm on a development team that goes through tons of work orders that are constantly...
2
by: ChrisO | last post by:
I've been pretty infatuated with JSON for some time now since "discovering" it a while back. (It's been there all along in JavaScript, but it was just never "noticed" or used by most until...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.