Do you make prototypes of functioned before defining them?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Do you make prototypes of functioned before defining them?
I don't have a seriouse problem with this, since cprogramming.com clarified that you can make a prototype and define at the same time then just make a prototype and define later on in the text file. But it doesn't give (so far at least) me a reason why I should have prototypes? As far as I understand, the code will compile if it sees the prototype, even if it is not defined. Such as the following code bit:
Code:
int Multiply(int x, int y);
int main()
{
int x, y;
cin >> x >> y;
cout << Multiply(x,y);
return 0;
}
.. but the problem is that the prototype didn't get defined with a block of code. The only reason why I would have prototypes instead of defining the functions on the spot would to make it easier for me to know what functions were in that .cpp (or .cc) file. Was there something I missed? Do you use it for a good reason? If so what is it? Here is the link to that tutorial: http://www.cprogramming.com/tutorial/lesson4.html
That code shouldn't compile, you'll get an "undefined reference to" error. I use prototypes myself, but mainly I think that's because I just like having main() at the top before any other function definitions.
Actually you can omit the varibles in the prototype altogether.
like this
Code:
int Multiply(int , int );
int main()
{
int x, y;
cin >> x >> y;
cout << Multiply(x,y);
return 0;
}
This tells the compiler that it should expect to have 2 int's when looking at function Multiply. This was actually very critical back when compilers were not as good. But is still considered good to do as it makes your code eazier to maintain.
And Nylex is a madman j/k
a co worker once gave me a very good pice of advice when programming. He said imagine that the code is a pice of tape from top to bottom and it will make sense why things need to be defined at the top.
Built a large program, say 40,000 lines of code. The first thing that you want to do is to break that down into a number of different source files. As soon as you do that you will find that the same function needs to be called from functions that are in different files. The simple solution is to include the prototype in both and leave it up to the linker to find the actual source code for that function.
Another reason which again becomes quite common in large programs is when you have function A that will call function B and under different circumstances when function B will call function A.
Which of course leads to a third reason, recursive functions.
With many small programs prototypes are not necessary, but they are a really good habit to get into using.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.