Whats the point of "void"

void blah() { }

whats the point of the word void?

Tagged:

Answers

  • Answer ✓

    it is used in a function to define the return type, means there isn't one. Just roll with it. If the function returned an integer, it would be int blah() {}

  • It indicates that the function does not return a value. For instance the method

    float foofoo(){
    
    }
    

    must return a float

  • edited July 2016

    The point is that w/o keyword void, Java compiler's parser wouldn't be able to distinguish (or at least have a very hard time to figure that out) whether we're defining some function w/o a returning type or simply invoking it! ;))

  • From experience, it is important for automatic syntax checking. If you have something like float myFunction(){}, then you will have an error stating that myFunction needs to return a float:

    float myFunction(){
      float f=0.0;
      return f;
    }
    

    On the other hand, you won't have any complaints when using void. Syntax checking will know that there is nothing to return.

    I hope this helps,

    Kf

Sign In or Register to comment.