Thread: function returning void

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    70

    function returning void

    Hi
    What is the benefit is having a function return a void than any other value.
    i mean why do we have the function

    void myfunction(myparameters) in the first place

  2. #2
    MetallicA adamg's Avatar
    Join Date
    May 2004
    Posts
    8
    I guess you could pass pointers to variables and change their values if you wanted to?

  3. #3
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218
    Well, I think that it can be used to return any type.
    Example:

    Code:
    void *malloc(size_t t);
    then, when you do:
    Code:
    char *input = (char*)malloc(128);
    Get it? I'm probably wrong, but that's my current understanding.
    01011001 01101111 01110101 00100000 01110100 01101111 01101111 1101011 00100000 01110100 01101000 01101001 01110011 00100000 01101101 01110101 01100011 01101000 00100000 01110100 01101000 01101101 01100101 00100000 01110100 01101111 00100000 01110010 01100101 01100001 01100100 00100000 01011001 01101000 01101001 01110011 00111111 00100000 01000100 01100001 01101101 01101110 00100001 00000000

  4. #4
    Registered User
    Join Date
    May 2004
    Posts
    127
    Often you'll have a function that has no reasonable return value. In other words, you call the function solely for the side effect. For example.
    Code:
    void swap(int *a, int *b)
    {
      int t = *a;
      *a = *b;
      *b = temp;
    }
    What value would you have this function return? C could, of course require a return value for every function and we could simply do this.
    Code:
    int swap(int *a, int *b)
    {
      int t = *a;
    
      *a = *b;
      *b = temp;
    
      return 0;
    }
    Though that makes it harder to say what you mean, and would probably encourage programmers to ignore return values more often than they should.

    >Well, I think that it can be used to return any type.
    You're thinking about a pointer to void. The question was about a function with no return value.

  5. #5
    I like code Rouss's Avatar
    Join Date
    Apr 2004
    Posts
    131
    It doesn't return void (kinghajj's example returns a void pointer).
    A void function doesn't return anything (as far as I know). Like adamg said, you can pass pointers to change values.
    Say you wanted to print an array, for example an integer array. And you were going to have to do it a few times through out your program.

    Code:
    void PrintArray(int array[], int num) /*** num is the number of elements in the array ***/
    {
       int i;
       for(i = 0; i < num; i++)
          printf("%3d", array[i]);
    
       printf("\n");
    }
    Now when you needed to print an array, instead for writing a for loop every time, you would just write:
    Code:
    PrintArray(array, num);
    That's not the only application for void functions, just a quick example.

    /edit/ Damn, Kip beat me to it... Oh well, now you have another example...
    Last edited by Rouss; 05-13-2004 at 08:31 AM.

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Example:
    Code:
    void *malloc(size_t t);
    malloc is returning a void pointer not void kinghajj

    The purpose of having a void return type is because there are times when you don't need any feedback from that functions.

    Lets say you had a program that required a menu, you could write a function that displays the menu and that is all, then you can have another that retrieves the user's selection, and a third that processes the request. Well the function that displays the menu doesn't really need to return anything since you can be reasonably sure it will execute without errors.

  7. #7
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218
    Quote Originally Posted by Thantos
    malloc is returning a void pointer not void kinghajj

    The purpose of having a void return type is because there are times when you don't need any feedback from that functions.

    Lets say you had a program that required a menu, you could write a function that displays the menu and that is all, then you can have another that retrieves the user's selection, and a third that processes the request. Well the function that displays the menu doesn't really need to return anything since you can be reasonably sure it will execute without errors.
    I never said it returned void: I just was giving an example on how void is used in malloc.
    01011001 01101111 01110101 00100000 01110100 01101111 01101111 1101011 00100000 01110100 01101000 01101001 01110011 00100000 01101101 01110101 01100011 01101000 00100000 01110100 01101000 01101101 01100101 00100000 01110100 01101111 00100000 01110010 01100101 01100001 01100100 00100000 01011001 01101000 01101001 01110011 00111111 00100000 01000100 01100001 01101101 01101110 00100001 00000000

  8. #8
    Registered User
    Join Date
    May 2004
    Posts
    127
    Quote Originally Posted by kinghajj
    I never said it returned void: I just was giving an example on how void is used in malloc.
    It appears that the question was about the concept of returning void as meaning no return value, not about the void keyword in general.

  9. #9
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Just a note: a function doesn't "return" void. What happens is that the function simply does not guarantee what value will be in the return location (on many platforms it is a specified register) and compiler basically keeps you from using that location as though there should be a known value in there.

  10. #10
    Registered User
    Join Date
    May 2004
    Posts
    127
    Quote Originally Posted by Thantos
    Just a note: a function doesn't "return" void. What happens is that the function simply does not guarantee what value will be in the return location (on many platforms it is a specified register) and compiler basically keeps you from using that location as though there should be a known value in there.
    How an implementation interprets the standard may vary, though a "function returning void" is accepted terminology for a function with void as the return type and semantics for no return value.

  11. #11
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    How can you return nothing? And the point was that the term is a misnomer.

  12. #12
    Registered User
    Join Date
    May 2004
    Posts
    127
    >How can you return nothing?
    I believe that explaining
    Code:
    void f(void)
    {
      return;
    }
    and
    Code:
    void f(void)
    {
    }
    as returning nothing makes more sense logically than trying to describe the internal workings of a compiler. You would also have difficulty being accurate because the C standard makes no mention of how a function returning a void type does what it does as long as it meets the required functionality. Wouldn't you agree that "returning nothing" makes more sense to everyone except theorists, implementors and language lawyers? If not, just say so and I'll leave you alone because your opinion clearly can't be swayed by reasoning.

    >And the point was that the term is a misnomer.
    I agree. Many terms in C are.

  13. #13
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    I would rather them say that "the function doesn't return anything" instead of "it returns nothing" a simple but important difference

    But of course I wouldn't tell that to a person just learning functions unless I was out to make their head explode (which is a fun past time btw)

  14. #14
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    Yeah, in other languages, aren't these called procedures? They are just there to do something.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  15. #15
    Registered User
    Join Date
    May 2004
    Posts
    127
    >a simple but important difference
    Well, if you want to get technical linguistically, returning nothing is equally accurate to not returning anything. Anything is defined as "Something or someone of importance". So not returning anything makes sense as void is an incomplete type and thus not something of importance. Nothing is defined as "One that has no substance or importance; a nonentity", which is strikingly close to the definition of void as an incomplete type.

    >(which is a fun past time btw)
    That it is...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  2. i cannot see the mouse arrow
    By peachchentao in forum C Programming
    Replies: 6
    Last Post: 12-10-2006, 04:14 AM
  3. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. msvc just ate one of my source files
    By Eber Kain in forum C++ Programming
    Replies: 6
    Last Post: 07-01-2004, 05:40 AM