Function prototypes

K

Kenneth Brody

Keith said:
Steph Barklay said:
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 [...]
to my teacher, but the answer he gave me is that I should just put the
whole function within the header file and not have any other *.c files.
I haven't seen anyone put whole functions within header files before. Am
I wrong about this or is my teacher wrong? Thank you.

Your teacher is very very very very very wrong.

Ask him to explain section 4.5 of K&R2 (that's Kernighan & Ritchie,
_The C Programming Language_, 2nd Edition) (it starts on page 81),
which absolutely contradicts your teacher's claims.

"And what makes those guys such experts at C programming?" :) :)

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
K

Kenneth Brody

pete said:
That implies, or very nearly implies,
that professional programmers don't write C programs
with more than one file of source code.

.... nor use any standard library functions other than the non-varadic
ones that return int. (Bye bye malloc and printf, among others.)
It also suggests that your teacher has never
written a C program with more than one file of source code,
and thus is a neophyte programmer.
How old is this kid?

Even neophytes can #include said:
I hope that you're mistaken about what he said.

I think/hope "ditto" applies to everyone reading clc. (Though I
wouldn't hold my breath waiting for the answer to that.)

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
K

Keith Thompson

osmium said:
As you should know by now, I am not a big fan of using word games in
discussions involving newbies.

I see no word games here. The word "prototype" has a very specific
meaning, and it includes both standalone function declarations:
void func(int);
and full function definitions:
void func(int arg) { /* ... */ }

The term is used to distinguish declarations that include argument
types (a feature introduced in ANSI C89, borrowed from early C++) from
the older K&R-style declarations that do not include argument types.

Do you have some reason to assume that the original poster was using
the term in the same narrow sense in which you were using it?

If we're using the term "prototype" in some sense other than the way
the standard defines it, then *somebody* should say so explicitly.
 
U

user923005

just a little off topic:
Would the compilation also be faster if all headers were included in a gaint
header file ?

No, probably slower -- a lot slower.
The above technique does not speed up compilation (generally compiles
run much slower). On some systems, it will increase the executable
speed of the binary.
 
U

user923005

I can't see any use in doing so in even the smallest program. (If
a program is *really* a little toy program, there is no real
reason to split it across several source files, either, unless to
teach how that is done; but I think that teaching to do that badly
is worse that not to teach it at all.)

I must agree with Army1987 here.
It seems likely that even the most trivial of programs will want to
print something to standard output.
If there is anything more than a simple character string that could
utilize puts(), then printf() is required which is a varadic
function. Calling a varadic function without the presence of a
prototype causes undefined behavior.
So even with the most trivial of toy programs, function prototypes are
basically a necessity.
 
R

Richard Tobin

Steph Barklay said:
"During this semester, all programs submitted for homework or extra
credit and the project must conform to the following programming
standards:
5. Use of function prototypes is prohibited. As such, all functions
must appear in reverse order of use, with the function main() being
the last function in the program.

Are you allowed to use mutually recursive functions? If so, how are
you supposed to do it?

This rule seems entirely stupid.

-- Richard
 
S

Steph Barklay

I dont think I misunderstood, here are the course guidelines quite clear
about this.

"During this semester, all programs submitted for homework or extra
credit and the project must conform to the following programming
standards:

1. Use of global variables is prohibited
2. All variables must be in lower case, with clear and concise names
that depict what data the varible will contain.
3. All constants must be in uppercase
4. Use of global constants, such as PI, is permitted
5. Use of function prototypes is prohibited. As such, all functions
must appear in reverse order of use, with the function main() being
the last function in the program.
6. Internal documentation for programs must include data range
specifications for each variable.
7. Before each function a short narrative must be included that
describes the purpose of the function, pre-conditions that must exist
for the function, what data the function returns and by what method
the data is returned, and who developed and programmed the function
which must, include email contact information..
8. At the top of the main source file, a narrative must be included
that describes the purpose of the program, who was the principle
programmer on the programming team. If the program went through
several revisions, a revision history must be included.
9. If "include" files are used, each "include" file must conform to
items 1 to 8."


Steph said:
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.
[...]

Two possibilities occur to me. First, you may have
misunderstood what the teacher said (the blame for this
could lie on either side, or on both). It would be a
good idea to discuss this matter with the teacher to clear
up any possible misunderstanding.

Second, you may not have misunderstood at all, and
the teacher may actually have made this assertion. If so,
I recommend you drop the course: a teacher who utters this
sort of nonsense (other than as a pedagogic device intended
to elicit push-back, which really falls under Possibility 1)
is ignorant of his or her subject. This person may be able
to teach you something useful, but will teach you a lot of
nonsense along with it and leave you the task of separating
the one from the other unassisted.
 
C

Chris Dollin

Steph said:
I dont think I misunderstood, here are the course guidelines quite clear
about this.

"During this semester, all programs submitted for homework or extra
credit and the project must conform to the following programming
standards:
5. Use of function prototypes is prohibited. As such, all functions
must appear in reverse order of use, with the function main() being
the last function in the program.
9. If "include" files are used, each "include" file must conform to
items 1 to 8."

Bonkers. And impossible to boot.

You have to use #includes to be able to correctly use several
of the IO functions. Those #includes will have prototypes in
them. BOOM.

If they mean /your/ include files, not /system/ include files,
it's still daft. The point of introducing include files is to
allow you to declare things, some of which are defined in other
compilation units. This pair of rules would mean you cannot
safely call functions defined in another unit. BOOM.

If they gave some reason for the rule it might be comprehensible.
Might.
 
S

santosh

Steph Barklay wrote:

[Top posting corrected]
Steph said:
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.
[...]

Two possibilities occur to me. First, you may have
misunderstood what the teacher said (the blame for this
could lie on either side, or on both). It would be a
good idea to discuss this matter with the teacher to clear
up any possible misunderstanding.

I dont think I misunderstood, here are the course guidelines quite clear
about this.

"During this semester, all programs submitted for homework or extra
credit and the project must conform to the following programming
standards:

5. Use of function prototypes is prohibited. As such, all functions
must appear in reverse order of use, with the function main() being
the last function in the program.

I'm guessing that this rule exists because the compiler at your organisation
is a pre-ANSI one, with no support for prototypes.

If not, then this is a very bad rule to teach to new programmers.

<snip rest of the "rules">
 
F

Flash Gordon

santosh wrote, On 23/08/07 22:10:
Steph Barklay wrote:


I'm guessing that this rule exists because the compiler at your organisation
is a pre-ANSI one, with no support for prototypes.

If that is the reason, it is still a very bad reason. If it were the
case the best thing to do would be move to a compiler that fully
supports ISO C.
If not, then this is a very bad rule to teach to new programmers.

Delete the "If not, then" from the above sentence.

Personally it looks to me like rule 5 was devised for programs small
enough to be a single source file and the author failed to think through
what it meant for a program split across multiple source files.
 
O

osmium

Keith Thompson said:
I see no word games here. The word "prototype" has a very specific
meaning, and it includes both standalone function declarations:
void func(int);
and full function definitions:
void func(int arg) { /* ... */ }

The term is used to distinguish declarations that include argument
types (a feature introduced in ANSI C89, borrowed from early C++) from
the older K&R-style declarations that do not include argument types.

Do you have some reason to assume that the original poster was using
the term in the same narrow sense in which you were using it?

Of course I have such a reason. The instructor's programs wouldn't work
otherwise, all he could do is write functions that returned an int or
returned nothing. I have already agreed the instructor shouldn't have said
what he said. What do you want, blood?

Why burden the OP in this thread with a bunch of esoterica on the fine
points of the standard? Presumably he wants to learn to write programs, not
how to write or interpret standards for a programming language. I talk to
people every day who have a poor grasp of English, yet we manage to muddle
though and I end up getting my hamburger..
 
E

Eric Sosman

(Please don't top-post. I've rearranged your reply.)

Steph Barklay wrote On 08/23/07 16:49,:
Steph said:
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.
[...]

Two possibilities occur to me. First, you may have
misunderstood what the teacher said (the blame for this
could lie on either side, or on both). [...]
I dont think I misunderstood, here are the course guidelines quite clear
about this.

"During this semester, all programs submitted for homework or extra
credit and the project must conform to the following programming
standards:

1. Use of global variables is prohibited

Ask whether you're allowed to make exceptions for
stdin, stdout, stderr, and (on some systems) errno. (Or,
if you don't want to seem a nit-picking pedant, don't.)
2. All variables must be in lower case, with clear and concise names
that depict what data the varible will contain.

There's a conflict between conciseness and descriptive
power. There's another conflict between descriptive power
and ease of reading.
3. All constants must be in uppercase

So, no lower-case character literals? (See earlier
advice on nit-picking pedantry.)
4. Use of global constants, such as PI, is permitted

I wonder what he means by "global constant." It's
not a phrase used by the C Standard. He might mean macros,
but he might mean file-scope variables with `const'. It
makes a difference.
5. Use of function prototypes is prohibited. As such, all functions
must appear in reverse order of use, with the function main() being
the last function in the program.

Aha! We come to the crux, which is that the writer
of the guidelines has used the word "prototype" incorrectly.
He's trying to forbid forward declarations -- equivalently,
he requires that the only declaration of a function shall
be its definition. This is sometimes called "Pascal style,"
where functions can only call functions that are defined
earlier or are in libraries. (This raises some problems
with mutually recursive functions, by the way: If f() calls
g() and g() calls f(), which appears first?)

However, "prototype" doesn't mean "declaration." A
"prototype" is a particular way of describing the parameters
a function takes, e.g.

/*********************/
int func(int this, double that) { ... }
/*********************/

The highlighted portion is the prototype of the function's
argument list. This is in contrast to the older "K&R style"
of function definition

int func(this, that)
int this;
double that;
{ ... }

In this form there is no prototype. What he said is that
you should use this outmoded syntax, but I doubt that's
what he actually meant.
6. Internal documentation for programs must include data range
specifications for each variable.
7. Before each function a short narrative must be included that
describes the purpose of the function, pre-conditions that must exist
for the function, what data the function returns and by what method
the data is returned, and who developed and programmed the function
which must, include email contact information..

It seems overkill to include your contact information on
every lousy little qsort() comparator, but ... If your
teacher has a bee in his bonnet, you tolerate the buzzing or
risk getting stung.
8. At the top of the main source file, a narrative must be included
that describes the purpose of the program, who was the principle
programmer on the programming team. If the program went through
several revisions, a revision history must be included.

The word is "principal," not "principle."

The reference to "main source file" is interesting, as
it implies the existence of programs that have more than one
source file. Given the prohibition against declarations that
aren't definitions, it is not at all clear how the functions
in one file are to call functions in other files. I suppose
you could (under some circumstances) just #include all the
other files into the compilation of the main file, but that's
a pretty silly way to write C programs.
9. If "include" files are used, each "include" file must conform to
items 1 to 8."

General impression: The guidelines require some good
practices, but they go overboard on occasion and in a few
cases (notably the mixup about prototypes) they mis-state
their intent. Their probable intent, I guess I should say.
 
E

Eric Sosman

osmium wrote On 08/23/07 17:28,:
[...]
Why burden the OP in this thread with a bunch of esoterica on the fine
points of the standard?

Because his instructor has confused him by misusing
a technical term. Resolving the confusion by explaining
the misuse is not imposing a burden.
[...] I talk to
people every day who have a poor grasp of English, yet we manage to muddle
though and I end up getting my hamburger..

Or your jelly-filled doughnut, as the case may be.
 
C

CBFalconer

Steph said:
I dont think I misunderstood, here are the course guidelines quite
clear about this.

"During this semester, all programs submitted for homework or extra
credit and the project must conform to the following programming
standards:

1. Use of global variables is prohibited
2. All variables must be in lower case, with clear and concise names
that depict what data the varible will contain.
3. All constants must be in uppercase
4. Use of global constants, such as PI, is permitted
5. Use of function prototypes is prohibited. As such, all functions
must appear in reverse order of use, with the function main() being
the last function in the program.
6. Internal documentation for programs must include data range
specifications for each variable.
7. Before each function a short narrative must be included that
describes the purpose of the function, pre-conditions that must exist
for the function, what data the function returns and by what method
the data is returned, and who developed and programmed the function
which must, include email contact information..
8. At the top of the main source file, a narrative must be included
that describes the purpose of the program, who was the principle
programmer on the programming team. If the program went through
several revisions, a revision history must be included.
9. If "include" files are used, each "include" file must conform to
items 1 to 8."

That's quite reasonable for one-file programs, and with
modifications for all programs. It is not banning prototypes - it
is forcing proper organization of prototypeless source.

The history has been lost by the unnecessary top-posting.

Please do not top-post. Your answer belongs after (or intermixed
with) the quoted material to which you reply, after snipping all
irrelevant material. See the following links:

--
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html>
<http://www.netmeister.org/news/learn2quote.html>
<http://cfaj.freeshell.org/google/> (taming google)
<http://members.fortunecity.com/nnqweb/> (newusers)
 
C

CBFalconer

Chris said:
Bonkers. And impossible to boot.

You have to use #includes to be able to correctly use several
of the IO functions. Those #includes will have prototypes in
them. BOOM.

If they mean /your/ include files, not /system/ include files,
it's still daft. The point of introducing include files is to
allow you to declare things, some of which are defined in other
compilation units. This pair of rules would mean you cannot
safely call functions defined in another unit. BOOM.

If they gave some reason for the rule it might be comprehensible.
Might.

This appears, to me, to be rules for a particular class. You can
pick holes in the demands, but in general it is quite satisfactory,
and will lead to well organized student programs. It is not the
earlier promulgated "nobody uses prototypes".
 
C

CBFalconer

santosh said:
Steph Barklay wrote:
.... snip ...


I'm guessing that this rule exists because the compiler at your
organisation is a pre-ANSI one, with no support for prototypes.

If not, then this is a very bad rule to teach to new programmers.

Pure prototypes are not needed if you are not connecting to other
files, nor using some forms of recursion. Simply writing the code
in a logical order suffices.
 
K

kuyper

Steph said:
I dont think I misunderstood, here are the course guidelines quite clear
about this.

"During this semester, all programs submitted for homework or extra
credit and the project must conform to the following programming
standards: ....
5. Use of function prototypes is prohibited. As such, all functions
must appear in reverse order of use, with the function main() being
the last function in the program.

I strongly suspect that in this context, your teacher is misusing the
word prototype. I suspect that he is not prohibiting the use of
prototypes that enter your code through standard headers, and he is
not prohibiting the use of prototypes at the start of a function
definition. I suspect that the only thing he intends to prohibit is
prototypes for your own functions, which are not part of the function
definition.

This means that you cannot build any programs that are divided into
multiple files. It also means that you can't write programs involving
mutually recursive functions. However, if I'm right, it's still
possible for you to create some fairly useful programs without
violating his restrictions. Those are silly restrictions, but not as
silly as a complete prohibition on the use of prototypes.

Personally, when I write functions that are used in only one module, I
do define them before their first use, exactly as your teacher
suggests. It avoids a small amount of typing, because I only have to
write the prototype once, as part of the definition. However, most
people seem to prefer the opposite order.

....
9. If "include" files are used, each "include" file must conform to
items 1 to 8."

I suspect that this only applies to #includes of header files written
by you, and is not meant to refer to standard headers.
 
K

Keith Thompson

osmium said:
Of course I have such a reason. The instructor's programs wouldn't work
otherwise, all he could do is write functions that returned an int or
returned nothing. I have already agreed the instructor shouldn't have said
what he said. What do you want, blood?

Why burden the OP in this thread with a bunch of esoterica on the fine
points of the standard? Presumably he wants to learn to write programs, not
how to write or interpret standards for a programming language.
[...]

Presumably he wants to write and talk about programs as well as
writing them. In doing so, it's going to be essential to understand
what technical words and phrases actually mean. One such word is
"prototype".

What if the instructor said not to use "functions", but actually meant
that each function must return a non-void result? Would you complain
if I pointed out that the word "function" is being misused? Or does
each person get to decide what a word means?

Now that we all know what the word "prototype" actually means, can we
drop this?
 
D

Default User

santosh said:
Steph Barklay wrote:


I'm guessing that this rule exists because the compiler at your
organisation is a pre-ANSI one, with no support for prototypes.


I don't agree. I believe the rule indicates that no prototype
declarations are to be used.

So, this:


void f(void)
{
}

int main(void)
{
f();
return 0;
}

NOT:

void f(void);

int main(void)
{
f();
return 0;
}

void f(void){
{
}




Brian
 
K

Keith Thompson

kuyper said:
I strongly suspect that in this context, your teacher is misusing the
word prototype. I suspect that he is not prohibiting the use of
prototypes that enter your code through standard headers, and he is
not prohibiting the use of prototypes at the start of a function
definition. I suspect that the only thing he intends to prohibit is
prototypes for your own functions, which are not part of the function
definition.
[...]

To clarify this point, ask your teacher whether this:

int trivial_func(int n)
{
printf("n = %d\n", n);
return n + 1;
}

would violate rule 5. If he says yes, ask if he really wants you to
write it like this instead:

int trivial_func(n)
int n;
{
printf("n = %d\n", n);
return n + 1;
}

You might also ask whether
#include <stdio.h>
would violate rule 5, since stdio.h will certainly contain prototypes
(unless you're using a *really* ancient implementation).

If your teacher is really insisting that you use old-style function
declarations, he could be as bad as we've been saying he is, but I
suspect that he's just misuing the term "prototype" and not thinking
through his requirements.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top