473,780 Members | 2,229 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1699
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******@netwa y.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*******@free net.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*****@sbcglob al.net
Nov 13 '05 #5

"Irrwahn Grausewitz" <ir*******@free net.de> schrieb im Newsbeitrag
news:gi******** *************** *********@4ax.c om...
"Robert Stankowic" <pc******@netwa y.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*****@sbcglo bal.ten> wrote in message
news:r7******** ***********@new ssvr25.news.pro digy.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*****@sbcglob al.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********@yah oo.com) (cb********@wor ldnet.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*****@sbcglo bal.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*******@free net.de)
Nov 13 '05 #9
On Sat, 29 Nov 2003 18:31:15 +0100
Irrwahn Grausewitz <ir*******@free net.de> wrote:
"rs" <no************ @talk21.com> wrote:

[Please don't top-post; fixed.]
"Mark F. Haigh" <mf*****@sbcglo bal.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

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

Similar topics

30
2779
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 Aikido and was born in Sun Labs, but has been released as open source. I no longer work for Sun, but am continuing to use and develop it. The language has a syntax similar to C++ and Java but is aimed at adhoc and prototyping tasks. Unlike other...
9
2678
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 computational schemes. The use of Python as my first-choice language has made me extremely productive. Now, I have always believed that Python is a poor performer in terms of numerical speed. My experience, however, is that the efficient use of...
1
2374
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
7981
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 pretty good: http://expertrating.com/c++test.asp
1
1814
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
2230
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 them? Do people ever use the generated code in production? Thoughts appreciated. Chris.
0
993
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 requiring us to rebuild our application for slight changes in rules. It would save us a large amount of time if we could store these outside our application logic in say a database or xml document that would be able to update. Any suggestions...
2
3324
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 recently -- or maybe I should just speak for myself.) The fact that JSON is more elegant goes without saying, yet I can't seem to find a way to use JSON the way I *really* want to use it: to create objects that can be instantated into multiple...
0
9474
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
10306
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...
0
10139
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9931
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8961
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 project—planning, coding, testing, and deployment—without 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
6727
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
5373
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
5504
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2869
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.