Message-ID: <35D15BC7.13911B88@geocities.com> From: Merlin MIME-Version: 1.0 Newsgroups: comp.os.msdos.djgpp Subject: Re: A very basic question about C programming... diary of a newbie Part 1 References: Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Lines: 116 Date: Wed, 12 Aug 1998 09:12:25 GMT NNTP-Posting-Host: 196-cy-wpg.ilos.net NNTP-Posting-Date: Wed, 12 Aug 1998 04:12:25 CDT Organization: MBnet Networking Inc. To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Precedence: bulk all prototyping is is putting a defenition of the function's used in your program at the beginning before main() so that you can put the actual function code after main() for example: #include //don;t need just there to show where it goes.. void do_nothing(); //these are prototypes int take_int_return_int(int) void main() { do_nothing(); int x=take_int_return_int(12); } void do_nothing() { delay(1000); } int take_int_return_int(int num) { num=(num*num-num)/num+1; return num; } you can take the prototypes out if you wanted and put the two functions at the top of the program... Prototypes are just there to tell the compiler that the functiion is defined below main...or elsewhere... At least that's what i was told in my cs101 class. oh yeah...i think that program up there will actually compile and run if you would want to do that... why you would is beond me.. :) cya Merlin. DM wrote: > Hello, > > I have an excellent tutorial on C that is helping me learn C very well, but > I can't seem to understand its definition of "prototyping". > > Here is a clip from my tutorial: > > "WHAT IS PROTOTYPING? > > A prototype is a model of a real thing and when programming in ANSI-C, you > have the ability to define a model of each function for the compiler. The > compiler can then use the model to check each of your calls to the function > and determine if you have used the correct number of arguments in the > function call and if they are of the correct type. By using prototypes, you > let the compiler do some additional error checking for you. " > > I understand the basic idea portrayed... here is more that confuses me: > > "Returning to lines 3, 4, and 5 in SCOPE.C, we have the prototypes for the > three functions contained within the program. The first void in each line > tells the compiler that these particular functions do not return a value, so > that the compiler would flag the statement index = head1(); as an error > because nothing is returned to assign to the variable named index. The word > void within the parentheses tells the compiler that this function requires > no parameters and if a variable were included, it would be an error and the > compiler would issue a warning message. If you wrote the statement > head1(index);, it would be a error. This allows you to use type checking > when programming in C" > > Here is a clip of the program called SCOPE.C that the article is refering > to: > /* Chapter 5 - Program 4 - SCOPE.C */ > #include /* Prototypes for Input/Output */ > void head1(void); /* Prototype for head1 */ > void head2(void); /* Prototype for head2 */ > void head3(void); /* Prototype for head3 */ > > int count; /* This is a global variable */ > > int main() > { > register int index; /* This variable is available only in main */ > > head1(); > head2(); > head3(); > /* main "for" loop of this program */ > for (index = 8 ; index > 0 ; index--) > { > int stuff; /* This var is only available in these braces*/ > for (stuff = 0 ; stuff <= 6 ; stuff++) > printf("%d ", stuff); > printf(" index is now %d\n", index); > } > > return 0; > } > > So my question is... writing the word "void" before a function is how you > prototype??? > > Or, if you "declare" the function before the main() is that prototyping? > > If the void is it, what if the function has to return a value? Can you not > prototype it then? > > I have the general idea, but I think the exact definition of exactly what to > type is slighty vague in the tutorial. > > Can anyone enlighten me with a good definition of prototyping? > > - DM