1. Updated Resource Submission Rules: All model & skin resource submissions must now include an in-game screenshot. This is to help speed up the moderation process and to show how the model and/or texture looks like from the in-game camera.
    Dismiss Notice
  2. DID YOU KNOW - That you can unlock new rank icons by posting on the forums or winning contests? Click here to customize your rank or read our User Rank Policy to see a list of ranks that you can unlock. Have you won a contest and still havn't received your rank award? Then please contact the administration.
    Dismiss Notice
  3. Travel to distant realms and encounter scenes unknown to the common folk. The Greatest of Adventures is upon us with the 8th Cinematic Contest. Join in on a fun ride.
    Dismiss Notice
  4. The 18th Icon Contest is ON! Choose any ingame unit and give him/her Hero abilities. Good luck to all.
    Dismiss Notice
  5. The Secrets of Warcraft 3 have revealed interesting works. The RESULTS for Abelhawk's Mini-Mapping Contest #15 have come out!
    Dismiss Notice
  6. Contestants are to create a scene set in the Stone Age. Come and see what you can come up with. We wish you the best of luck!
    Dismiss Notice
  7. Colour outside the lines! Techtree Contest #13 is a go. The contest is optionally paired.
    Dismiss Notice
  8. Night Rider gained several songs for his journey. The poll for the 12th Music Contest has started. Check it out!
    Dismiss Notice
  9. Greetings cerebrates, our Swarm needs new spawners that will have numerous children. Join the HIVE's 31st Modeling Contest - Spawners and Spawned! The contest is optionally paired.
    Dismiss Notice
  10. Check out the Staff job openings thread.
    Dismiss Notice
Dismiss Notice
60,000 passwords have been reset on July 8, 2019. If you cannot login, read this.

void in C++

Discussion in 'Programming' started by GywGod133, Sep 10, 2014.

  1. GywGod133

    GywGod133

    Joined:
    Jul 16, 2012
    Messages:
    646
    Resources:
    3
    Maps:
    1
    Spells:
    1
    Tutorials:
    1
    Resources:
    3
    Hello Hivers :)

    I have a project in C++ (Collage)

    can you tell me what use of this void

    I dunno how to use this..

    can you make me a example for this

    Thank You!
     
  2. IcemanBo

    IcemanBo

    Joined:
    Sep 6, 2013
    Messages:
    6,153
    Resources:
    22
    Maps:
    3
    Spells:
    11
    Template:
    1
    Tutorials:
    4
    JASS:
    3
    Resources:
    22
    Where exactly?

    void myFunc(int i) --> no returnType for your function, int i is parameter

    int myFunc(void) --> int as returnType, no parameters needed ... but "(void)" can also just become "()"

    void myFunc(void) / void myFunc() --> no returnType, no parameters

    edit:

    When you declare a pointer you usually define the type of the pointer. If you just write "void *pt;" the type will be generic.
     
  3. edo494

    edo494

    Joined:
    Apr 16, 2012
    Messages:
    3,855
    Resources:
    5
    Spells:
    1
    JASS:
    4
    Resources:
    5
    void in C++ and in C means "nothing". It can be used as return type, or as unnamed parameter.

    Code (C++):

    void myFunc(void)
    {
    ...
    }
     


    basically this function tells you that it doesnt accept anything as argument and does not return anything.

    You can replace void with nothing in argument signature, but this only works in C++, because in C that means the function takes any number of any arguments. So this is the same as above(in C++)

    Code (C++):

    void myFunc()
    {
    ...
    }
     
     
  4. GywGod133

    GywGod133

    Joined:
    Jul 16, 2012
    Messages:
    646
    Resources:
    3
    Maps:
    1
    Spells:
    1
    Tutorials:
    1
    Resources:
    3
    Do nothing? :eekani:

    so, void main() is nothing?
     
  5. chobibo

    chobibo

    Joined:
    Sep 24, 2005
    Messages:
    2,692
    Resources:
    0
    Resources:
    0
    no, it means function main returns no value.

    Code (vJASS):
    int main(){
     // do something here
     return 0;
    }

    void main(){
     // do something here
    }
     
  6. GywGod133

    GywGod133

    Joined:
    Jul 16, 2012
    Messages:
    646
    Resources:
    3
    Maps:
    1
    Spells:
    1
    Tutorials:
    1
    Resources:
    3
    so void is a variable,
     
  7. chobibo

    chobibo

    Joined:
    Sep 24, 2005
    Messages:
    2,692
    Resources:
    0
    Resources:
    0
    it's a keyword, just like int, float, bool etc.
     
  8. IcemanBo

    IcemanBo

    Joined:
    Sep 6, 2013
    Messages:
    6,153
    Resources:
    22
    Maps:
    3
    Spells:
    11
    Template:
    1
    Tutorials:
    4
    JASS:
    3
    Resources:
    22
    What makes you think now it is a variable?

    It's not a variable. It's like an indicator for a non-type.

    int myFunc() --> returns an int type
    void myFunc() --> returns nothing (void just indicates here that no data type will be returned in the function)

    Like in chobibo's example before.
     
  9. GywGod133

    GywGod133

    Joined:
    Jul 16, 2012
    Messages:
    646
    Resources:
    3
    Maps:
    1
    Spells:
    1
    Tutorials:
    1
    Resources:
    3
    oh okay....

    It means you can do the function int main() same in void main()

    for example

    int main()
    {
    cout<<"Hello";
    }


    void main()
    {
    cout<<"Hello";
    }
     
  10. edo494

    edo494

    Joined:
    Apr 16, 2012
    Messages:
    3,855
    Resources:
    5
    Spells:
    1
    JASS:
    4
    Resources:
    5
    void main is not legal C nor C++, and if your compiler allows compilation of void main, you should considering upgrading to something that is at least standard compilant
     
  11. LordDz

    LordDz

    Joined:
    May 11, 2007
    Messages:
    4,308
    Resources:
    0
    Resources:
    0
    void main(void) //All C++ projects need a main function
    {
    function1();
    }

    void function1() //A function that doesn't return anything when called
    {
    cout<<"Hello!\n";
    cout<<function2();
    }

    int function2() //Returns an int when called
    {
    return 2;
    }

    Output:
    Hello!
    2
     
  12. Dr Super Good

    Dr Super Good

    Spell Reviewer

    Joined:
    Jan 18, 2005
    Messages:
    25,427
    Resources:
    3
    Maps:
    1
    Spells:
    2
    Resources:
    3
    void means an absence of type.

    returning a void type means the function does not return anything.

    a void pointer means a pointer to no defined type. Although void pointers themselves are valid types (they correspond to a memory address) they cannot be dereferenced due to void having no type information. They also cannot be manipulated like normal pointers as the compiler has no size information for the void type (cannot commute byte offset).

    Incorrect. The main function of a program should return an integer which corresponds to the application exit status. It should also take an array of character pointers and a argument count. The arc count and argument array can optionally be left out, which is usually the case for embedded software since it becomes the entry point of the processor so those arguments are not useful.

    Code (Text):

    int main(int argc, char *argv[]);
     
    Next to as a return type, void is mostly used for generic pointers. This is best seen in standard C for generic data structures like lists where void pointers to elements are commonly used. When getting the element you get a void pointer out of the data structure interface and then type cast it to the appropriate type (which you should know based on what you put into the data structure). In C++ void pointers see considerably less use as virtual methods and templates should be used instead as they are considerably more type safe and flexible.
     
  13. edo494

    edo494

    Joined:
    Apr 16, 2012
    Messages:
    3,855
    Resources:
    5
    Spells:
    1
    JASS:
    4
    Resources:
    5
    http://ideone.com/6StORy

    that summed it up.

    And I dont give a shit about the order of declaration in your code, but the first word is already wrong
     
  14. -Kobas-

    -Kobas-

    Joined:
    Jan 17, 2010
    Messages:
    5,895
    Resources:
    28
    Icons:
    1
    Tools:
    2
    Maps:
    10
    Spells:
    4
    Template:
    5
    Tutorials:
    6
    Resources:
    28
    You should always stick with
    int main(){return 0;}
    .
    Some compilers require that you explicitly define main as such (GNU GCC Compiler - Code::Blocks IDE).
    At the other hand some compilers will allow you to define
    main
    as void function, but still in background use it as
    int
    (Visual Studio Compiler - Visual Studio 2013 IDE, for example).

    Another reason why I mention those two is that for example, Visual Studio will terminate your program asap
    main
    return value, because, you actually write that, right. At the other hand Code::Blocks will insert
    system("pause")
    command just before
    return
    , as well as do few other things, but that's story for itself.

    Finally to use those functions (
    function1, function2
    ) you need to declare them above
    main
    , and ofc.
    #include <iostream>
    using namespace std;
    .

    EDIT:

    Oh, another thing, it's not true that all C++ projects require a main function.
    If you want to create Dynamic-Link Library (well DLL file) and link your core C++ code with C# project it won't have main at all.
     
  15. edo494

    edo494

    Joined:
    Apr 16, 2012
    Messages:
    3,855
    Resources:
    5
    Spells:
    1
    JASS:
    4
    Resources:
    5
    to quote standard:

    source: N3690 standard draft §3.6.1.2

    it is required that main returns int
     
  16. Dr Super Good

    Dr Super Good

    Spell Reviewer

    Joined:
    Jan 18, 2005
    Messages:
    25,427
    Resources:
    3
    Maps:
    1
    Spells:
    2
    Resources:
    3
    All Windows, MAC or Linux programs should take command line arguments.
     
  17. edo494

    edo494

    Joined:
    Apr 16, 2012
    Messages:
    3,855
    Resources:
    5
    Spells:
    1
    JASS:
    4
    Resources:
    5
    you cant find the standard and no, it is fully optional
     
  18. LordDz

    LordDz

    Joined:
    May 11, 2007
    Messages:
    4,308
    Resources:
    0
    Resources:
    0
    Yeah, it was a long time since I did C++ sorry :)
     
  19. gorillabull

    gorillabull

    Joined:
    Jul 17, 2011
    Messages:
    1,368
    Resources:
    2
    Spells:
    2
    Resources:
    2
    i think for the purpose that you are asking its just a function which does not require you to write
    return something;
    at any point in it.

    return type of a function is always specified at the start: int main, float g() etc..
    these two have to have a return type int and float whereas void does not void functions are usually used to just perform an action (like displaying something) and not to modify data.
     
  20. edo494

    edo494

    Joined:
    Apr 16, 2012
    Messages:
    3,855
    Resources:
    5
    Spells:
    1
    JASS:
    4
    Resources:
    5
    This is fully opinion and design based. In fact, most functions that change state of instance of given member usually dont return anything, why would they after all