Thread: just a random question

  1. #16
    Registered User
    Join Date
    Jan 2011
    Posts
    87
    if it returns nothing then what would this result in, 0 or space?
    Code:
    #include <iostream>
    void test()
    {
           return;
    }
    
    int main()
    {
         cout << test();
    }

  2. #17
    Registered User
    Join Date
    Jan 2011
    Posts
    87
    also, what happens when you do this with while or if?
    Code:
    while/*or if*/(1; 0+1; /* and so on */ )
    {}

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Or how about a compile error?
    The second is a syntax error.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #19
    Registered User
    Join Date
    Jan 2011
    Posts
    87
    probably, maybe i will try it

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Heh. Not probably. There will be a compile error. In two places, actually.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #21
    Registered User
    Join Date
    Jan 2011
    Posts
    87
    you're right, i just tried it

  7. #22
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    Quote Originally Posted by Elysia View Post
    return; does not return void, though. It returns nothing.
    Sounds like hair splitting to me; "void" means "nothing" right? Is there a technical difference?

  8. #23
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, because void is treated as a type.
    Try, for example:

    Code:
    void foo() {}
    int main() { int x = foo(); }
    error C2440: 'initializing' : cannot convert from 'void' to 'int'.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #24
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    void doesn't even have a size, so it is nothing. I don't see a meaningful difference here. It is equally erroneous to state that just because a function has no value to return, that it will never return because "you cannot return void". The only circumstance any function does not return would be because it does not reach an exit point. Compilers do complain about unreachable code in a couple of ways.

    Code:
    void foo() {}
    int main() { int x = foo(); }
    Blatant mistakes don't really prove your point. This is the same error as any invalid type conversion.

  10. #25
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    My point is that the compiler treats it as something. While in your return statement you don't put void to return nothing, you simply leave it empty.
    It is my understanding or interpretation of the whole anyway. "void" can be seen as a type.
    (Otherwise why would void* be legal? And why not void in the parameter list be legal?)
    Void is a curious thing, yet there. Semantics.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #26
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    My point is that the compiler treats it as something. While in your return statement you don't put void to return nothing, you simply leave it empty.
    I would think the inverse -- if the compiler treated void as something, then you would need to create it to appease the compiler. You don't need to do that, and thank goodness, because you can't. Meanwhile void* is something, because it's a data pointer. Symmetry is for the deluded people.

    I'm not saying void isn't a type, but saying it is nothing would be perfectly correct. After all, returning void means returning nothing. In common vernacular it's just harmless.

  12. #27
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Meh. Whatever.
    void is special; let's just be content with that.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #28
    Registered User
    Join Date
    Jan 2011
    Posts
    87
    thats a good way to put it,
    why would you have main return anything other than 0 though?

  14. #29
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by bobknows View Post
    thats a good way to put it,
    why would you have main return anything other than 0 though?
    To let the caller know how the execution went. Let's say you wrote a program that just output the contents of a file to the console. On success, your program would return 0. If your program failed to open the specified file then main() could return 1, letting the caller know that something went wrong.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. random question program
    By rogster001 in forum C++ Programming
    Replies: 1
    Last Post: 10-08-2009, 02:29 AM
  2. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 05:51 PM
  3. How do I restart a random number sequence.
    By jeffski in forum C Programming
    Replies: 6
    Last Post: 05-29-2003, 02:40 PM
  4. Noob question for random number
    By SKINp in forum C++ Programming
    Replies: 6
    Last Post: 01-03-2003, 12:53 PM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM

Tags for this Thread