Putting the function body with the prototype

This is a discussion on Putting the function body with the prototype within the C++ Programming forums, part of the General Programming Boards category; In the code below, I was testing to see if putting the function body with the prototype would work. I'm ...

  1. #1
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209

    Putting the function body with the prototype

    In the code below, I was testing to see if putting the function body with the prototype would work. I'm assuming this makes it inline like it would in a class?

    Code:
    #include <iostream>
    
    using namespace std;
    
    int mult(int num)
    {
      return num*num;
    };
    
    int main()
    {
      int number;
      cout<<"Input a number. ";
      cin >>number;
      cout<<"That number squared is: "<<mult(number)<<endl;
      system("PAUSE");
      return 0;
    }

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Only one thing can ask the compiler to inline a function and that is the inline keyword.

    Its perfectly ok to put the body with a prototype in the manner you did above. Of course you don't need the ; after the function's closing }

  3. #3
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    Yeah, but if the semicolon wasn't there, wouldn't that be negating the purpose of the prototype (like if I had three functions up there, to not have to rely on order of declaration)?

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I was testing to see if putting the function body with the prototype would work.
    It works as long as the function is defined before its use. This is because a definition is also a declaration, but a declaration is required before use. Now say that 3 times fast.

    >I'm assuming this makes it inline like it would in a class?
    No, outside of an inline class definition, you need to use the inline keyword to suggest to the compiler that you want the function inlined. Though it's free to ignore you, and it probably will because the programmer is notoriously bad at determining these things.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 08-24-2006, 12:22 AM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  4. error LNK2001: unresolved external symbol
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 07-12-2002, 08:45 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21