LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-13-2006, 12:28 AM   #1
RHLinuxGUY
Member
 
Registered: Oct 2003
Distribution: Ubuntu 7.04
Posts: 889
Blog Entries: 1

Rep: Reputation: 30
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
 
Old 05-13-2006, 12:39 AM   #2
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
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.
 
Old 05-13-2006, 12:46 AM   #3
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
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.

Last edited by exvor; 05-13-2006 at 12:48 AM.
 
Old 05-13-2006, 07:56 AM   #4
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Why have prototypes?

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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Emc promises open source, prototypes consumer gear LXer Syndicated Linux News 0 04-27-2006 10:03 PM
function prototypes in template class' (c++) qwijibow Programming 4 12-13-2004 09:34 AM
extract prototypes from a shared library tieulent Linux - Software 1 08-27-2004 10:15 AM
Question about function prototypes xailer Programming 4 12-15-2003 10:10 AM
Defining terms Kurt M. Weber General 1 10-25-2003 07:25 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:13 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration