Thread: Function differences help

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    3

    Question Function differences help

    Alright, so on my way through learning c++, I noticed that functions are done two different ways, but I cannot find if there are differences, or if they are just preferred ways of doing functions.

    Here I have two different simple codes done in different ways.
    The first way declares the entire function at the top:
    Code:
    #include <iostream>
    
    using namespace std;
    
    
    int factorial(int num)
    {
    int starter;
    int total = 1;
    for (starter = 1; starter <= num; starter++)
    {
    total *= starter;
    }
    return total;
    }
    
    
    int main()
    {
    int num;
    cout << "Please enter a number greater than 1!" << endl;
    cin >> num;
    if (num <= 1)
    {
    cout << "Please use a number GREATER than 1." << endl;
    }
    else
    {
    cout << "Function returned: " << factorial(num) << endl;
    }
    system("PAUSE");
    return 0;
    }
    This second way uses what I've read as being a "Prototype" and then has all the information at the bottom of the code.
    Code:
    #include <iostream>
    
    using namespace std;
    
    
    int factorial(int prototype);
    
    
    int main()
    {
    int num;
    cout << "Please enter a number greater than 1!" << endl;
    cin >> num;
    if (num <= 1)
    {
    cout << "Please use a number GREATER than 1." << endl;
    }
    else
    {
    cout << "Function returned: " << factorial(num) << endl;
    }
    system("PAUSE");
    return 0;
    }
    
    
    
    
    int factorial(int num)
    {
    int starter;
    int total = 1;
    for (starter = 1; starter <= num; starter++)
    {
    total *= starter;
    }
    return total;
    }
    Both of these codes work the exact same way, however, the prototype seems useless, as I could just replace it with the entire function that I want. Not only that, but with the prototype, my (int prototype); seems to never have any use.

    So yeah, I'm lost here. Can anyone please explain the differences, if any in this? Do I NEED to use a prototype, or can I just do it the first way that I have it in all cases and still have it work just fine? Thanks in advanced!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    You need either the prototype or the definition before you try and use the function.

    Which way you go in a program that fits in a single source file is just a matter of style.

    But function prototypes are pretty much essential when you start writing programs which span many source files.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by ZezXion View Post
    Both of these codes work the exact same way, however, the prototype seems useless, as I could just replace it with the entire function that I want..
    Absolutely correct. Of course you can't always just put every function before the one that calls it. For example if you have functionA calls functionB, but functionB also calls functionA, then you cant put neither first and must use a prototype. This happens in real world code, notably parsers.

    Not only that, but with the prototype, my (int prototype); seems to never have any use.
    The prototype is there to tell the code that the function exists and what parameters it expects. This is needed due ot the way C compilers worked right from the beginning. Some languages such as D, do not require prototypes as they actually scan the code to find the function itself. C compilers just don't do that.

    The variable name isn't technically necessary in the prototype, but it is typically given and is the same as the one in the real function, to make it more self-documenting.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C , C++ , C# differences.
    By mystic in forum Tech Board
    Replies: 15
    Last Post: 07-14-2010, 11:04 PM
  2. Differences
    By Kitt3n in forum Windows Programming
    Replies: 8
    Last Post: 06-10-2010, 06:39 AM
  3. Differences between if() and for()
    By Robert_Sitter in forum C++ Programming
    Replies: 7
    Last Post: 10-31-2005, 10:39 PM
  4. Differences between C and C++
    By blankstare77 in forum C++ Programming
    Replies: 10
    Last Post: 07-25-2005, 09:03 PM
  5. What are the differences between C & C++??
    By moenia in forum C++ Programming
    Replies: 5
    Last Post: 04-29-2002, 09:32 PM