Results 1 to 10 of 10

Hybrid View

  1. #1
    Join Date
    Oct 2010
    Posts
    4

    starting a new func problems.

    hi i just bought the C++ Complete on this website and im following along with the video tutorial and on the second video i fall into this problem im following along and they start doing new functions and mines not working the same? im guessing cause i have a newer version of microsoft visual studios? theirs is 2003 or something mines 2010. how can i start a new function iv noticed alot of things already where i have to go online and google some of this stuff out cause its out of date.

    in the video they do

    Code:
    main()
    {
      // stuff stuff stuff
    }
    but why is it now i have to do

    Code:
    void main()
    {
    
    }
    or int main()? and this is what my code looks like to start a new function

    Code:
    #include <iostream>
    
    using namespace std;
    
    void main()
    {
    	int tempint = 45;
    
    	cout << "Hello World!" << tempint << endl;
    
    	void newFunc();
    }
    
    void newFunc()
    {
    	cout << "rawr" << endl;
    }
    so basicly why did theirs work back in 2003 or w.e but now i have to write all this out in the 2010 version? and how do i start a new function now?

    also it dosnt give me no errors the way i just posted to you but it also dosnt print out "rawr" but will post out "Hello World!" with no problem.

  2. #2
    Join Date
    Oct 2009
    Location
    The In Between. US
    Posts
    2,030
    you need to declare the function above main to let the compiler know it exists

    they have updated the language. somebody else can give a detailed reason why
    elevating to new levels of knowledge (that's the plan at least )

  3. #3
    Join Date
    Oct 2010
    Posts
    4
    so i think this is what you were talking about?

    Code:
    #include <iostream>
    
    using namespace std;
    
    void newFunc();
    
    void main()
    {
    	int tempint = 45;
    
    	cout << "Hello World!" << tempint << endl;
    
    	newFunc();
    }
    
    void newFunc()
    {
    	cout << "rawr" << endl;
    }
    well if so it worked but why is it that i need to initialize a new function like that? i dont do alot of programming like C++ or C# java or anything like that but i mod for Call of Duty 2 and which is why C++ is in my interest and making a new func is as easy as just doing newFunc(); without having to initialize it so why is it now we have to initialize it just wondering?

  4. #4
    Join Date
    Mar 2009
    Location
    Bellevue, WA
    Posts
    803
    Quote Originally Posted by blizblat View Post
    i dont do alot of programming like C++ or C# java or anything like that but i mod for Call of Duty 2 and which is why C++ is in my interest and making a new func is as easy as just doing newFunc(); without having to initialize it
    That's because most modding tools that you will use aren't using a C-Language per say but more or less a C-style scripting language developed by the game company as part of their in-house and modding tools. So you would need to understand C-style syntax when scripting in these tools but these tools are usually built on top of C++ for example not C++ itself.

    As far as the need for the return type and prototypes Edge has a good explination.
    " Imagination is the preview of life's coming attractions " - Albert Einstein
    " If you can't explain it simply, you don't understand it well enough." - Albert Einstein

    My Website: http://www.gamedevlounge.com/

    If any post is informative please provide positive feedback by clicking the star below.

  5. #5
    Join Date
    Oct 2010
    Posts
    4
    That's because most modding tools that you will use aren't using a C-Language per say but more or less a C-style scripting language developed by the game company as part of their in-house and modding tools. So you would need to understand C-style syntax when scripting in these tools but these tools are usually built on top of C++ for example not C++ itself.
    right exactly and i thats why im so comfortable with trying to learn C++ i know its gonna take some time but i have somewhat of an understanding just need to learn more and keep practicing

  6. #6
    Join Date
    Mar 2009
    Location
    Bellevue, WA
    Posts
    803
    First to fix your problem you have to do what elevating said and declare your function above main. A declaration is just this

    Code:
    // return type function name(paramater type, etc...)
    void newFunc();
    
    int main()
    {
          // Some code here
          return 0;
    }
    
    void newFunc()
    {
        // some more code here
    }
    As far as the reasons why that was accepted and not accepted now I can't give an answer with 100% certainty and someone else may know a little better.

    I believe it may have to do with the compiler. While C++ drew up it's first standardized draft in 1994 it wasn't until 1998 when the ANSI/ISO standard became a reality and was accepted as a standard by most major compilers. However just because it became accepted as a standard didn't mean each compiler followed all the rules and there may have been a few differences from one compiler to the next. Most compilers today follow the standard a little closer than before and main requires a return value of int because when your program finishes executing it will return a value that shows the program successful quit which is why we say return 0; Any other value returned from main usually indicates a non successful execution of the program. Honestly though in the 5 years I've been programming in C++ I've never had to do anything other than return 0 from main.

  7. #7
    Join Date
    Oct 2010
    Posts
    4
    thank you for taking the time to clear that all up for me i really appreciate that
    Honestly though in the 5 years I've been programming in C++ I've never had to do anything other than return 0 from main.
    i will definitely keep that in mind as well thank you

  8. #8
    Join Date
    Jun 2011
    Location
    Westerville, OH
    Posts
    9
    I'm not the most experienced at C++, but I can add on to what has already been said

    Quote Originally Posted by blizblat View Post
    but why is it now i have to do

    Code:
    void main()
    {
    
    }
    First of all, I was told that you shouldn't use the data type void for your main function. Like Kwest said it needs something returned, so just use int main().

    I don't know about the 2003 version, but functions nowadays (for all intents and purposes within this thread) have their own data type that defines what will be returned by that function. void indicates that nothing will be returned by that function.

    Code:
    // return type function name(paramater type, etc...)
    void newFunc();
    
    int main()
    {
          // Some code here
          return 0;
    }
    
    void newFunc()
    {
        // some more code here
    }
    Like I said I'm no expert, so the way I think about it is that the compiler starts at the top and works down, if it gets to the main function and you call a function it hasn't seen yet, it freaks out.

    What you used here is called function prototyping. You just let the compiler know that it's okay, you'll add in the body of code needed later, it's alright if it's called before it has any code in it.

    Another way is to simply have the body of code above the main function. I'm not sure what would be standard, but generally I'd do it this way.

    Code:
    void newFunc()
    {
          //outputs "rawr" here
    }
    
    int main()
    {
          // Some code here
          return 0;
    }
    It may not be cleaner, but it works just as well.

    I don't know why the compiler works that way, but I hope anything I said here was useful.

  9. #9
    Join Date
    Jun 2003
    Posts
    150
    The reason for needing to put a return type on main() (or void for none at all) is for consistency's sake in writing the code. Basically you have to remember your application is going to run on top of an operating system. In the case of Windows, the .exe itself is essentially a program that makes a call to your program's main() function to get things going(other things such as resources like text files and image files can be stored inside the .exe file, however this tends bloat the file quite quickly).

    Because another program can call your program, your program needs a return type. Knowing your program will be called by another program allows you to send a return value to that program just as you would with any other function. Again in the case of Windows it allows you to send back an error code that Windows can respond to. If you don't need to send one you can specify void as the return type or just send back the integer 0(the compiler may actually just rewrite void/return to int/return 0, I've never really gone into the assembly to find out...some day I will).

    Now prototypes allow you to define an interface to a function while not requiring you to create an implementation at that moment. It basically tells the compiler "Hey, I haven't written the function yet or it's not defined here, but if anyone needs to call it in this source file, here's what they need to provide and here's what it returns. So you can go ahead and create a marker for where to find this function". What this is usually used for is code organization. They make more sense when you get into using header files, which will let you link code between different source files.

  10. #10
    Join Date
    Oct 2011
    Posts
    547
    I would buy the member sponsor, look at the software engineering videos and enter the course c++ 101. Afterwards look at the c++ complete videos that you bought (even if it might be outdated(?) it is still good to see).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •