"void"

This is a discussion on "void" within the C Programming forums, part of the General Programming Boards category; hey, I'm back y'all I got this question about "void". My book, which I like, is by some Herbert Schildt ...

  1. #1
    jjj
    Guest

    Talking "void"

    hey, I'm back y'all
    I got this question about "void". My book, which I like, is by some Herbert Schildt guy where I teach myself C.

    He said this: "
    If a function does not return a value, then it's type should be 'void"

    See, now he used a function that only printed a statement out
    like this

    Code:
    void myfunc (void)
    {
    printf ("this is a test.");
    }
    ok, now it does "return" that statment in the " ", but what does that mean. I understand some functions I create will punch out some number to be used by the program, so that would have to be something other than a "void" return type. So is it that "void" is basically used for functions or statement return-types?

    just getting familiar. and if you could explain the "(void)", aka the "parameter-list", that would be cool too. thanks, I'll get back to my book.
    listening to some old school michael jackson while I do this, that guy used to be a monster in music, tore it up.

  2. #2
    jjj
    Guest
    see, check this, this is why I may not be understanding something. All this program does is covert your dollar amount you put in to pounds using a conversion factor of 2$ per pound(britain money type, in case someone don't know).

    Code:
    #include <stdio.h>
    
     int convert(void);
    
    int main(void)
    {
      int pounds;
      
      pounds = convert();
      printf( "That is %d pounds\n", pounds );
      
      return 0;
      }
      
      int convert(void)
      {
      int money, pounds;
      
      printf( "Enter your dollars ");
      scanf( "%d", &money );
      return money/2;
      }
    see, well I just don't get it, there is a void for the parameteres of the function. I am having trouble understanding that in my mind, because it looks like my convert() function I made does return a value, that is, it returns "money/2"
    I though void was only ok to use when you are NOT returning a value. Hmmm, I am very picky but I need to know and understand, thanks in advance peeps.

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    52
    When void is used like so
    Code:
    void function(int a)
    then it declares that function does not return a value to the calling function.
    ok, now it does "return" that statment in the " ",
    myfunc does not return a value to the calling function. It justs prints a string to stdin.

    When void is used like this
    Code:
    int foobar(void)
    it specifies that foobar takes no arguments

    This might be a good time to read the FAQ if you haven't done so already, especially the part about int main() vs void main(). It'll save you getting mocked unmercifully later on
    Turn no more aside and brood
    Upon love's bitter mysteries
    For Fergus rules the brazen cars...

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    Type void can also be used with variables. For example

    void *ptr;

    declares a void-pointer. Such a pointer can be used to point to all other datatypes, therefore it is usually called a general pointer.

  5. #5
    jjj
    Guest
    icarus(sounds like a video game name, I like it)

    where is the link to what you are talking about here, I will maybe check out the tutorials on this site too

    "This might be a good time to read the FAQ if you haven't done so already, especially the part about int main() vs void main(). It'll save you getting mocked unmercifully later on"

  6. #6
    Registered User Dr. Bebop's Avatar
    Join Date
    Sep 2002
    Posts
    96
    Processing error: Stupidity detected.
    ------------------------------
    Dr. Bebop
    Windows XP Professional Ed.
    Microsoft Visual Studio 6

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What exactly does "void" mean?
    By GoldPhoenix in forum C++ Programming
    Replies: 9
    Last Post: 12-27-2006, 08:09 PM
  2. "VOID" as opposed to void
    By Trauts in forum Windows Programming
    Replies: 23
    Last Post: 05-28-2003, 10:56 AM
  3. questions about "void"...
    By Gamma in forum C++ Programming
    Replies: 7
    Last Post: 04-13-2002, 10:04 AM
  4. Replies: 1
    Last Post: 11-25-2001, 05:31 AM

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