Programming: Creating Prototypes for Functions and Skeleton Functions

Why are prototypes needed. Prototypes are used for the compiler to know in advance the functions that will be used before the functions are defined. It allows the programmer to place the actual functions in any order in the program. For example, if a function1 used function2 within its code and function2 was defined after function1, then the compiler would get confused.



Example (using no prototype): void Function1() { newValue = Function2() } int Function2() { return 1; } When the compiler tries to compile Function1 and gets to the use of Function2, it knows nothing about Function2, so the compiler would not be able to continue. Example (with prototype): int Function2(void); void Function1() { newValue = Function2() } int Function2() { return 1; } Now the compiler is not confused because it knows the return type and that it will not have any parameters passed in. This makes allows the compiler to continue.
Back to blog

Leave a comment

Please note, comments need to be approved before they are published.