Member Avatar

Hi, This question is related to the below.

I used that to solve my issue. What I would like to understand is why the function prototype is to be declared before the main function? In my textbook that I am following the function prototype wasn't there. I did as written in that thread and my error got resolved.

As the program gets complex, it would be a bunch of prototypes on top. Any way to get around this? Am I doing something wrong?

Here's my code:

// My first C++ app

#include <iostream>

using namespace std;

void print(int i); // Why? This wasn't there on textbook and on this site I'm referring

int main()
    int marks = 0;

    cout << "Enter a number\n";
    cin >> marks;

    return 0;

void print(int i)
    int y = i;
    cout << "You entered " << y << endl;

Recommended Answers

All 4 Replies

The compiler begins reading the code at the top, and goes downwards through it, once.

When it gets to a function call, like this:
it has to already know what parameters that function takes, and what it returns.

There are two ways it can already know this. Either you told it by having a prototype somewhere before this point, or the function definition is before this point.

As the program gets complex, it would be a bunch of prototypes on top. Any way to get around this?

This is what header files are for. Put your function prototypes in a header file, and then #include that header file at the top.

commented: Decent patient explanation of the basics, awesome and helpful +3
Member Avatar

Yep, thought as much. Moved the function definition before the main function.

Thank You very much.

As a program gets more complex, it's a good idea to use prototypes. One reason to use prototypes is because it makes it easier to see what parameters any particular function uses. If all of your prototypes are in alphabetical order and listed one after the other, it's easy to look up any function.

But a more important reason to use prototypes, and in some cases it will be mandatory, is because of the need to properly list the function definitions in the right order. For example, if function A calls function B then you have to define function B first. And if function A calls function B and function B calls function C then you have to define C first, then B then A. But what if function A calls function B and function B calls function A? Then there is no way to do this, unless you use prototypes.

So if your program gets to be complicated with many functions, do you really want to be burdened with making sure everything is defined in the right order? I don't think so, especially if you decide to change some things, like having function 14 call function 58 which already calls 13 which calls both 26 and 4. And who knows what functions 26 and 4 call! Just use prototypes in any order that you want and all of that mess is taken care of automatically.

Prototypes are a good thing. Just to be consistant, I always use prototypes, even if I have only one function.

commented: Pretty good explanation and reason to use prototypes +0

So long as we are discussing prototypes, let me discuss why we need them in the first place. Actually, in the early days of C programming (and maybe still today?) you didn't need a prototype for a function. The compiler could translate the source code even if there was no prototype and the function was defined at the end of the program instead of before the main function.

If the compiler was going down through the code and suddenly, out of the blue, there appeared a function call like this: x = foo(a,b), then the compiler would just assume there was a function out there somewhere called foo, and it would just assume that it returned an int (if x was defined that way) and that it accepted an int and a double (if a and b were defined that way respectively). In effect, the compiler would use the function call itself as a prototype in order to do the translation into machine code. The problem is, it's all just an assumption. The compiler is just assuming that there is a function called foo that returns an int and accepts an int and a double, because that's what you said (or acted like : ). And that is all fine and dandy, so long as you didn't make any mistakes in writing the function call. But if you did, say, passing two integers instead of a int and double, then the stack would become corrupted and the program would crash at runtime. And the bug would probably be hard to find.

So to save you from yourself, "they" (the standards committee?) decided that you would have to submit a discription (prototype) of the function to the compiler, and the compiler would then keep this information on file so it could use it to check your work and to be sure you were using the function correctly.

So the only reason you need a prototype is so that the compiler and check your work rather than just assuming you are doing everything right. Of course, if you define the function before it is used, like moving it ahead of main(), then the compiler can use that to check your work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.