473,588 Members | 2,474 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

prototypes

If a function is visible everywhere, even out the file where it is
declared and defined, why should i write prototypes?
--
Devaraja (Xdevaraja87^gm ail^c0mX)
Linux Registerd User #338167
http://counter.li.org
Nov 15 '05 #1
13 1460
DevarajA ha scritto:
If a function is visible everywhere, even out the file where it is
declared and defined, why should i write prototypes?


I've just read that they're used for type checking when calling external
functions. Is that true?

--
Devaraja (Xdevaraja87^gm ail^c0mX)
Linux Registerd User #338167
http://counter.li.org
Nov 15 '05 #2

"DevarajA" <no@spam.com> wrote in message
news:Om******** *************@n ews4.tin.it...
DevarajA ha scritto:
If a function is visible everywhere, even out the file where it is
declared and defined, why should i write prototypes?


I've just read that they're used for type checking when calling external
functions. Is that true?


The thing described by the OP (who,oddly enough, has the same nym as you) is
not an external function. He said it was *visible*. C does compiling in a
forward direction and it must know the argument types when it compiles. So
if you do the physical definition prior to the first usage you don't need a
prototype. This is sometimes called "Pascal style". The prototypes for
external functions are usually in a visible header file.
Nov 15 '05 #3
osmium ha scritto:
"DevarajA" <no@spam.com> wrote in message
news:Om******** *************@n ews4.tin.it...

DevarajA ha scritto:
If a function is visible everywhere, even out the file where it is
declared and defined, why should i write prototypes?


I've just read that they're used for type checking when calling external
functions. Is that true?

The thing described by the OP (who,oddly enough, has the same nym as you) is
not an external function. He said it was *visible*. C does compiling in a
forward direction and it must know the argument types when it compiles. So
if you do the physical definition prior to the first usage you don't need a
prototype. This is sometimes called "Pascal style". The prototypes for
external functions are usually in a visible header file.


so why this works and only gives "implicit declaration" warnings?

int main()
{
a(8,9);
b(9,9.5);
c(9.5,10);
return 0;
}
int a(int x, int y){return 0;}
int b(int x, double y){return 0;}
int c(double x, int y){return 0;}
--
Devaraja (Xdevaraja87^gm ail^c0mX)
Linux Registerd User #338167
http://counter.li.org
Nov 15 '05 #4
"DevarajA" writes:
so why this works and only gives "implicit declaration" warnings?

int main()
{
a(8,9);
b(9,9.5);
c(9.5,10);
return 0;
}
int a(int x, int y){return 0;}
int b(int x, double y){return 0;}
int c(double x, int y){return 0;}


There was a time when something called K&R C was a more or less de facto
standard. Before there was a real standard. It assumed that everything was
int. Your compiler is in a mode such that it accepts the old dialect.
Nov 15 '05 #5
osmium ha scritto:
"DevarajA" writes:

so why this works and only gives "implicit declaration" warnings?

int main()
{
a(8,9);
b(9,9.5);
c(9.5,10);
return 0;
}
int a(int x, int y){return 0;}
int b(int x, double y){return 0;}
int c(double x, int y){return 0;}

There was a time when something called K&R C was a more or less de facto
standard. Before there was a real standard. It assumed that everything was
int. Your compiler is in a mode such that it accepts the old dialect.


So for that old standard a call to a undeclared function f1 is an
implicit declaration (as int f1(...))?

--
Devaraja (Xdevaraja87^gm ail^c0mX)
Linux Registerd User #338167
http://counter.li.org
Nov 15 '05 #6


DevarajA wrote:
If a function is visible everywhere, even out the file where it is
declared and defined, why should i write prototypes?
--
Devaraja (Xdevaraja87^gm ail^c0mX)
Linux Registerd User #338167
http://counter.li.org


You need to have a prototype declaration in scope if the function is
being called before it has been defined. If you have all your
functions in the same file, and you define each function before it is
used, you don't need a prototype declaration.

Example:

double foo(int x, float y)
{
...
}

char *bar(void)
{
...
double g = foo(1, 2.0);
}

int main(void)
{
char *msg = bar();
...
}

Since the functions are defined (using prototype syntax) before they
are called, the compiler is able to verify that the return types and
the number and types of arguments match between the definition and
call.

Now, suppose the order were reversed:

int main (void)
{
char *msg = bar();
...
}

char *bar(void)
{
double g = foo(1, 2.0);
...
}

double foo(int x, float y);
{
...
}
When the compiler sees the call to bar(), it has no prior definition or
declaration of bar in scope, so it assumes bar() returns int, and does
no parameter checking. In this case, the compiler should issue a
diagnostic since it thinks you're trying to assign an int value to a
char *. Similarly, when the compiler first sees the call to foo(), it
assumes that foo() returns int, does no checking on the parameters, and
applies the default type promotions (IIRC, without a definition or
declaration in scope, 2.0 will be passed as a double instead of a
float). Again, you'll get a warning about trying to assign an int to a
double.

Nov 15 '05 #7


osmium wrote:
"DevarajA" <no@spam.com> wrote in message
news:Om******** *************@n ews4.tin.it...

DevarajA ha scritto:
If a function is visible everywhere, even out the file where it is
declared and defined, why should i write prototypes?


I've just read that they're used for type checking when calling external
functions. Is that true?

The thing described by the OP (who,oddly enough, has the same nym as you) is
not an external function. He said it was *visible*. C does compiling in a
forward direction and it must know the argument types when it compiles. So
if you do the physical definition prior to the first usage you don't need a
prototype. This is sometimes called "Pascal style". The prototypes for
external functions are usually in a visible header file.


There's some evidence of confusion here, so to clear
the air: A prototype is the parenthesis-enclosed list of
types or type/argument pairs that appears in a function
declaration. It is possible to declare a function with
a prototype

double func(double, int, const char*);

or without a prototype

double func();

Equally, it is possible to define a function with a
prototype

double func(double x, int i, const char *s) {
return x + i * strlen(s);
}

or without a prototype

double func(x, i, s)
double x;
int i;
const char *s;
{
return x + i * strlen(s);
}

DevarajA and osmium seem to be saying "prototype" when
what they actually mean is "declaratio n." osmium's point
is that a function definition is also a declaration (though
the opposite is clearly not true), and is really not about
prototypes at all.

--
Er*********@sun .com

Nov 15 '05 #8


John Bode wrote:

DevarajA wrote:
If a function is visible everywhere, even out the file where it is
declared and defined, why should i write prototypes?
--
Devaraja (Xdevaraja87^gm ail^c0mX)
Linux Registerd User #338167
http://counter.li.org

You need to have a prototype declaration in scope if the function is
being called before it has been defined. [...]


No: You need a declaration, but the declaration is not
required to provide a prototype. (Under C90 rules, even
the declaration is sometimes optional.)

Prototypes are a Good Thing; I cannot think of a case
where a prototype-less declaration wouldn't be improved by
adding a prototype. However, the only time a prototype is
actually required is when the function has a variable-length
argument list. All fixed-length prototypes can be omitted,
if you've got masochistic tendencies and are willing to suffer
opprobrium from others who work with your awful code.

--
Er*********@sun .com

Nov 15 '05 #9


Eric Sosman wrote:
John Bode wrote:

DevarajA wrote:
If a function is visible everywhere, even out the file where it is
declared and defined, why should i write prototypes?
--
Devaraja (Xdevaraja87^gm ail^c0mX)
Linux Registerd User #338167
http://counter.li.org

You need to have a prototype declaration in scope if the function is
being called before it has been defined. [...]


No: You need a declaration, but the declaration is not
required to provide a prototype. (Under C90 rules, even
the declaration is sometimes optional.)

Prototypes are a Good Thing; I cannot think of a case
where a prototype-less declaration wouldn't be improved by
adding a prototype. However, the only time a prototype is
actually required is when the function has a variable-length
argument list. All fixed-length prototypes can be omitted,
if you've got masochistic tendencies and are willing to suffer
opprobrium from others who work with your awful code.

--
Er*********@sun .com


You're right. That first sentence should be amended to, "you need to
have a declaration in scope (preferably using prototype syntax in favor
of the old K&R-style syntax) ..."

Nov 15 '05 #10

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

Similar topics

145
6265
by: David MacQuigg | last post by:
Playing with Prothon today, I am fascinated by the idea of eliminating classes in Python. I'm trying to figure out what fundamental benefit there is to having classes. Is all this complexity unecessary? Here is an example of a Python class with all three types of methods (instance, static, and class methods). # Example from Ch.23, p.381-2 of Learning Python, 2nd ed. class Multi:
7
3552
by: Michele Simionato | last post by:
So far, I have not installed Prothon, nor I have experience with Io, Self or other prototype-based languages. Still, from the discussion on the mailing list, I have got the strong impression that you do not actually need to fork Python in order to implement prototypes. It seems to me that Python metaclasses + descriptors are more than powerful enough to implementing prototypes in pure Python. I wrote a module that implements part of what...
14
2730
by: fb | last post by:
Does the C language require you to prototype functions? If it's not required, is it recommended?
9
1907
by: Grumble | last post by:
Hello everyone, I've come across some strange code. Here it is, stripped down: int main(void) { int *foo; int *bar(); foo = bar(0); return 0;
7
2370
by: junky_fellow | last post by:
Can a function have two different prototypes ? If not , then how can main() have two different prototypes ? int main(void) and int main argc(int argc, char *argv) I mean to say, if I declare main in either of the above mentioned ways my compiler does not give any warning. How can it accept two different prototypes ?
1
1569
by: petermichaux | last post by:
Hi, I have searched the archives but didn't find the questions and answers I am looking for. I have been looking at Prototype.js quite a bit lately as I need to create a very small library of similar functionality to a subset of Prototype.js. This is for use with Ruby on Rails. About Prototype.js Rob G wrote:
20
2111
by: Ari Krupnik | last post by:
scripts can add methods to the prototypes of builtin objects in JaavScript. I can assign functions to String.prototype.*, for instance. I want to add a method to Node, but when I try to execute the following IE says "'Node' is undefined." Mozilla works as I expected it to. Is Node called something else in IE? Does IE not allow manipulating the prototypes of some builtin objects? Node.prototype.nt = function() { return this.nodeType; }
73
3410
by: Steph Barklay | last post by:
Hi, I'm currently taking a data structures course in C, and my teacher said that function prototypes are not allowed in any of our code. He also said that no professional programmers use function prototypes. This kind of bugged me, because from other people's code that I've seen in the past, almost all of them use function prototypes. The following also bugged me. Let's say you have a file called main.c with only the main function, and...
4
1917
by: robtyketto | last post by:
Greetings, I’m writing a research report on justifying disposable prototypes. The problem being there is a wealth of material regarding failures in the IT press and some of these put partial blame on prototypes. There are lots of books and website promoting the use of prototypes and all state the wonders of prototyping. However I can’t find (on initial googling session of a few hours) a single whitepaper, new item, statistics or...
0
7862
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
8228
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
7987
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
6634
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...
1
5729
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3847
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
3887
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2372
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
1
1459
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.