void

Discussion in 'Programmer's Corner' started by TAKYMOUNIR, Sep 29, 2012.

  1. TAKYMOUNIR

    Thread Starter Active Member

    Jun 23, 2008
    351
    1
    what void mean in c language like
    int main(void){
    }
    or some function
    like void myfun(void)
    thanks
     
  2. MrChips

    Moderator

    Oct 2, 2009
    12,414
    3,353
    myfun(void) means there are no parameters to pass to the subroutine.

    void myfun(xxx) means there is no result returned from the subroutine.
     
    TAKYMOUNIR likes this.
  3. TAKYMOUNIR

    Thread Starter Active Member

    Jun 23, 2008
    351
    1
    thanks very much ,can you please simplify for me by like example i am new in c language
     
  4. MrChips

    Moderator

    Oct 2, 2009
    12,414
    3,353
    Some compilers allow you to omit the word void.
     
    TAKYMOUNIR likes this.
  5. WBahn

    Moderator

    Mar 31, 2012
    17,715
    4,787
    The key to understanding this is that, unlike mathematical functions, program functions in many/most languages do a lot more than take values as arguments and return values as results. Because they can do other things, it is not always necessary for them to take any values as arguments and/or return values as results.

    So we might right a function that takes a single argument, a pointer to a string that is assumed to be someone's name, and that does nothing by print out a greeting message:

    Code ( (Unknown Language)):
    1.  
    2. void Greetings(char *s)
    3. {
    4.    printf("Greetings, %s\n", s);
    5. }
    6.  
    Notice that there is no return statement. None is needed because no value is being returned.

    Now let's say we had a function that doesn't take any arguments but that returns a random number between 0 and 1:

    Code ( (Unknown Language)):
    1.  
    2. double RandNorm(void)
    3. {
    4.    return (double) rand()/ (double) RAND_MAX;
    5. }
     
    TAKYMOUNIR likes this.
  6. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    That would be any ANSII C compiler.

    The void keyword instructs the compiler that "no parameter is being used here and that is what is intended." However, the following is also permitted:

    Code ( (Unknown Language)):
    1.  
    2. void foo();
    3.  
    4. ...
    5. void foo()
    6. {
    7.   // code here
    8. }
    9.  
    Omitting the parameter void also turns off any parameter checking, a dangerous feature indeed.

    A pointer may also be cast a void. A pointer typically points to a certain type of object, such as a character or an integer. A void pointer may point to any type.
     
    TAKYMOUNIR likes this.
  7. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
  8. WBahn

    Moderator

    Mar 31, 2012
    17,715
    4,787
    While a pointer may be cast as a void pointer (or even declared as one), it can't be cast as a void. These are two very different things. I don't know what casting something as a void would even mean (so perhaps I am missing something -- always possible).
     
    TAKYMOUNIR likes this.
Loading...