Jump to content

Function Prototypes

- - - - -

  • Please log in to reply
2 replies to this topic

#1
Atomic_Sheep

Atomic_Sheep

    Newbie

  • Members
  • Pip
  • 4 posts
I know one can probably program your entire life without knowing this, but I can't. Could someone please enlighten me as to why the compiler needs a forward declaration of functions, variables etc and why it cares so much about their prototypes?

#2
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
That's part of a strongly typed language. As far as variables are concerned, the compiler needs to know their types ahead of time because it has to allocate a specific amount of memory depending on how large that type is, and it has to interpret the bits found at that memory location in a certain way, because different types are represented differently. As far as functions go, these have specific signatures because their parameters are allocated in much the same way as variables: areas of memory on the program stack. It also allows the compiler to reject unsafe operations on certain types at compile time, instead of causing the program to crash at runtime. In other words, it's a method of guaranteeing program accuracy.

If you want to learn more about strong type systems, you might try studying type theory: Type theory - Wikipedia, the free encyclopedia
Computer type systems find their roots in lambda calculus, so there's a deep rooted structure in mathematics, not just something somebody came up with one day. :)
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#3
Atomic_Sheep

Atomic_Sheep

    Newbie

  • Members
  • Pip
  • 4 posts
Many thanks, great response :thumbup1:




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users