Function prototypes

Discussion in 'C Programming' started by Steph Barklay, Aug 23, 2007.

  1. 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.
    Steph Barklay, Aug 23, 2007
    1. Advertisements

  2. Steph Barklay

    Al Balmer Guest

    Is this for real? I find it hard to believe that any such teacher
    exists. If this is not a troll, you should, if at all possible, enroll
    in another class with a different teacher. If this is not possible,
    ask your teacher to monitor this newsgroup for a while.

    If that doesn't work, I don't know what to suggest. Probably you
    should drop the course, and study C on your own.
    Al Balmer, Aug 23, 2007
    1. Advertisements

  3. Steph Barklay said:
    If he is being paid to teach you C, sue him.
    He is mistaken. Only fools (and those forced to use compilers so ancient
    that you can hardly see them through the accumulated dust, hair, and
    topsoil) omit them.
    Your teacher is 100% wrong, brain-dead, and beyond redemption. Only
    fools put code in headers.
    Richard Heathfield, Aug 23, 2007
  4. Steph Barklay

    Old Wolf Guest

    Only fools and C++ programmers ;)
    Old Wolf, Aug 23, 2007
  5. 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.
    Keith Thompson, Aug 23, 2007
  6. Old Wolf said:
    But you repeat yourself. :)
    Richard Heathfield, Aug 23, 2007
  7. Steph Barklay

    user923005 Guest

    Your teacher is criminally insane. Notify the authorities right away.

    On the other hand, this is my evil handiwork:

    #ifdef _MSC_VER
    #include <windows.h>
    #include <unistd.h>

    #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"
    #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"

    On the other hand, each of those files has headers with prototypes and
    there are headers with prototypes at the top even of this file.
    Some compilers can inline more aggressively if you make every file
    available as a single giant block of source code.
    user923005, Aug 23, 2007
  8. 99% wrong; there are valid reasons for code in headers, such as
    function-like macros or static inline functions. However, I doubt that the
    teacher was thinking about those things.

    Stephen Sprunk, Aug 23, 2007
  9. Steph Barklay

    CBFalconer Guest

    Either the teacher, or your interpretation of his words, are
    totally balmy.
    CBFalconer, Aug 23, 2007
  10. Steph Barklay

    Eric Sosman Guest

    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.
    Eric Sosman, Aug 23, 2007
  11. Steph Barklay

    pete Guest

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

    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?

    I hope that you're mistaken about what he said.
    He absolutely does not understand what header files are for,
    and is giving very strong indications
    that he has no experience with multifile C programs.
    Again, I very much hope that you have misunderstood what he meant.
    pete, Aug 23, 2007
  12. Steph Barklay

    osmium Guest

    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.
    osmium, Aug 23, 2007
  13. Steph Barklay

    Army1987 Guest

    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.)
    Army1987, Aug 23, 2007
  14. Steph Barklay

    Army1987 Guest

    At least their name ends with .c, so that one will know that extra
    care shall be taken not to define anything twice.
    Army1987, Aug 23, 2007
  15. #ifdef _MSC_VER
    just a little off topic:
    Would the compilation also be faster if all headers were included in a gaint
    header file ?
    Ravishankar S, Aug 23, 2007
  16. Steph Barklay

    osmium Guest

    I almost always write such programs "Pascal style", which obviates the
    prototypes. If I change something involving function calls, there is only
    one place to change. If such a program grew to a substantial size, and
    drifted from the original intent, I might reorganize the code to "C style",
    with prototypes, I think the documentation flows better that way, especially
    for someone new to the code.
    osmium, Aug 23, 2007
  17. osmium said:

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

    Richard Heathfield, Aug 23, 2007
  18. Steph Barklay

    osmium Guest

    As you should know by now, I am not a big fan of using word games in
    discussions involving newbies.
    osmium, Aug 23, 2007
  19. osmium said:
    As you should know by now, using proper terminology in the proper way is
    not a matter of word games, but a matter of correctness.
    Richard Heathfield, Aug 23, 2007
  20. Steph Barklay

    Richard Guest

    And as you should know, *any* thread that Mr Heathfield contributes to
    descends to just that.

    I still his recall "there is no such thing as a global variable in C" blather.
    Richard, Aug 23, 2007
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.