473,804 Members | 4,005 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Function prototypes

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 includes hello.h, to use functions within hello.c. So
hello.h should contain prototypes for the functions in hello.c, so when
main.c is compiled, there won't be any warnings. If there aren't any
prototypes in the header file, my compiler would assume the function
called with main() is extern and returns an int. I tried to explain 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.

Aug 22 '07
73 3472
"osmium" <r1********@com cast.netwrites:
"Richard Heathfield" writes:
>osmium said:
<snip>
>>I almost always write such programs "Pascal style", which obviates the
prototypes.

No, not really. A prototype is a function declaration that lists the
types of its parameters. For function definitions /not/ to do this,
they must either be written K&R style or with empty parentheses. Both
of these styles are very rare nowadays. It is almost certain that you
are in fact using prototypes, even when programming "Pascal style".

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.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 23 '07 #21
On Aug 23, 4:32 am, "Ravishanka r S" <s.ravishan...@ de.bosch.com>
wrote:
#ifdef _MSC_VER
#include <windows.h>
#else
#include <unistd.h>
#endif
#include "searchr.c"
#include "search.c"
#include "thread.c"
#include "searchmp.c "
#include "repeat.c"
#include "next.c"
#include "nexte.c"
#include "nextr.c"
#include "history.c"
#include "quiesce.c"
#include "evaluate.c "
#include "movgen.c"
#include "make.c"
#include "unmake.c"
#include "hash.c"
#include "attacks.c"
#include "swap.c"
#include "boolean.c"
#include "utility.c"
#include "valid.c"
#include "probe.c"
#include "book.c"
#include "analyze.c"
#include "annotate.c "
#include "bench.c"
#include "data.c"
#ifndef _MSC_VER
#include "dgt.c"
#endif
#include "drawn.c"
#include "edit.c"
#include "epd.c"
#include "epdglue.c"
#include "evtest.c"
#include "init.c"
#include "input.c"
#include "interupt.c "
#include "iterate.c"
#include "learn.c"
#include "main.c"
#include "option.c"
#include "output.c"
#include "ponder.c"
#include "preeval.c"
#include "resign.c"
#include "root.c"
#include "setboard.c "
#include "test.c"
#include "time.c"
#include "validate.c "
Some compilers can inline more aggressively if you make every file
available as a single giant block of source code.

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.
Aug 23 '07 #22
On Aug 23, 3:39 am, Army1987 <army1...@NOSPA M.itwrote:
On Wed, 22 Aug 2007 19:42:38 -0700, osmium wrote:
"Steph Barklay" <d...@spam.mewr ote in message
news:sl******** ***********@nos pam.invalid...
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 includes hello.h, to use functions within hello.c. So
hello.h should contain prototypes for the functions in hello.c, so when
main.c is compiled, there won't be any warnings. If there aren't any
prototypes in the header file, my compiler would assume the function
called with main() is extern and returns an int. I tried to explain 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 instructor is pathetic. He is talking about little toy programs, the
kind you write in the very class you are enrolled in. My guess is that he
has never seen the code for a real program in his entire life.

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.

Aug 23 '07 #23
"Keith Thompson" writes:
"osmium" <r1********@com cast.netwrites:
>"Richard Heathfield" writes:
>>osmium said:
<snip>

I almost always write such programs "Pascal style", which obviates the
prototypes .

No, not really. A prototype is a function declaration that lists the
types of its parameters. For function definitions /not/ to do this,
they must either be written K&R style or with empty parentheses. Both
of these styles are very rare nowadays. It is almost certain that you
are in fact using prototypes, even when programming "Pascal style".

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?
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..


Aug 23 '07 #24
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.

--
Er*********@sun .com
Aug 23 '07 #25
"osmium" <r1********@com cast.netwrites:
[...]
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?

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 23 '07 #26
Kenneth Brody wrote:
>
pete wrote:

Steph Barklay wrote:
>
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.
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 <stdio.hon day one.
I was granting that he meant prototypes in source code.
But if you take him literally to just mean
making use of prototypes in code, then yes,
I don't think you can write a program for a modern C implementation,
that has any output, without using prototypes.
I hope that you're mistaken about what he said.

I think/hope "ditto" applies to everyone reading clc.
Me too.
(Though I
wouldn't hold my breath waiting for the answer to that.)
I think we would even prefer that Steph Barklay was trolling us
than that this could actually be happening.
But we all know that there's no limit to incompetence anywhere.

--
pete
Aug 24 '07 #27
osmium wrote:
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.
OP already knows how to write programs.
OP wants to learn about data structures.

--
pete
Aug 24 '07 #28
Richard Heathfield wrote:
[...]
You'd think a teacher would know the difference between "principle" and
"principal" , wouldn't you?
I blam our flawhed eductaional sistym!

--
Eric Sosman
es*****@ieee-dot-org.invalid
Aug 24 '07 #29
On Aug 23, 6:23 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
Steph Barklay 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
Let's hope he does not ask anyone to print to stout or stderr or read
from stdin.
It's right out, then.
Aug 24 '07 #30

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

Similar topics

21
3862
by: Rob Somers | last post by:
Hey people, I read a good thread on here regarding the reason why we use function prototypes, and it answered most of my questions, but I wanted to double check on a couple of things, as I am writing something up on functions, and I don't like writing about things I am not sure about. Ok, then, here we go: I initially thought that one would only really need to use a function
6
7996
by: Daniel Nichols | last post by:
I've noticed that in a C module (.c, .h file combination) that if you create a function's definition before it is used in other functions than a declaration is not necessary. I believe if the compiler can find the definition of the function prior to encountering the use of the function it will generate the prototype itself. I don't currently use this feature, I explicitly create declarations for all functions in a header file. However, I...
7
2380
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 ?
6
5013
by: Robbie Hatley | last post by:
I'm maintaining a software project with 134 C++ files, some of them huge (as much as 10,000 lines each), and very few prototypes. The author's attitude towards prototypes was like this: Prototypes are only good for headers to be included in other files. For functions which call each other inside one file, such as A calls B which calls C and D, just define the functions in order D, C, B, A, and you'll never need prototypes.
9
1665
by: wizwx | last post by:
what does the following mean? int func2(); According to C++, it surely means func2 is a function that takes no argument and returns an integer. But what about in C? Does it have the same meaning or does it mean that func2 is a function that takes any number of arguments and returns an integer? Thanks for any clarification.
1
5561
by: Noah Roberts | last post by:
Trying to use boost::function in a C++/CLI program. Here is code: pragma once #include <boost/function.hpp> #include <boost/shared_ptr.hpp> #include <vector> using namespace System;
16
7608
by: arne | last post by:
Hi all, imagine I call a function, but omit one of the parameters, like: foo.c: void foo( int a, int b ) { /* do something with a and b */ return; }
29
8089
by: Ravishankar S | last post by:
Dear C Experts, While prepating a content for a C course,I made section on function prototypes. Could you kindly provide me your comments on its correctness. Thank you ! Q12: What is the difference between a function prototype and a function declaration? If you get this right, you must have done fair amount of research on C
16
3291
by: Xiaoxiao | last post by:
Hi, I got a C library, is there a way to view the public function names in this library so that I can use in my C program? Thanks.
0
9706
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, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10575
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
10076
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...
1
7616
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
6851
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
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4297
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
2
3816
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2990
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.