aspose file tools*
The moose likes Linux / UNIX and the fly likes very basic question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "very basic question" Watch "very basic question" New topic
Author

very basic question

andy car
Greenhorn

Joined: Nov 10, 2005
Posts: 7
I am taking a operating systems class and we are using C and running the program on the command line through Linux.

My question is about a void return type. I am not asking for the answer to the following example. Just what or why "void is used". I bolded the word void below. I am not sure I ever used void as the parameter.

I am new to C, but have used Java alot.



/*
* minusOne - return a value of -1
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 2
* Rating: 6
*/
int minusOne(void) {



return 2;

}
Gerardo Tasistro
Ranch Hand

Joined: Feb 08, 2005
Posts: 362
Well I guess it's because it requires no parameters to know it just has to return 2. Also, the return type isn't void, it is int. And if in Java you ever wrote something like public String handleThis(), you have used void parameters as there is nothing in the parethesis.
andy car
Greenhorn

Joined: Nov 10, 2005
Posts: 7
Thanks that makes a little more sense with the (), (void) reference.

Well the "return 2" part is so that it is syntactally correct. I have to manipulate that and make sure it returns a -1. That is why I asked, I wanted to make sure I assign the same 'type', not just 2.

So basically, it will return an 'int' because that is the default refrence?
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16019
    
  20

To truly appreciate void, you'd have to have been there. In the original C, functions all returned ints, unless declared otherwise and there were no function prototypes to to keep programmers from miscoding calling sequences.

As C developed into C++, the extra rigor was not only desirable, but in the case of function overloading, it was absolutely essential to nail things down or the function overloader would be an even bigger nightmare than it is.

When you declare a function as "myfn() {...}", you are defining a function that returns an int (and if you don't set one, results may be a random integer) and saying that you don't know what arguments may be passed.

However, defining "void myfn(void)" say that your function isn't expected to return a result AND that is cannot pass in any arguments either. Thus, the compiler has several useful cues - like flagging return statements with values as obvious errors.

For documentation purposes, I sometimes explicitly cast function calls whose results I intend to throw away:


main(), BTW is a real screwball case. Technically, it's "int main(int argc, char* argv[])" - give or take a char-pointer. Historically, the "int" return type wasn't specified, just taken for granted. However, some apps didn't set a return value from main.


Customer surveys are for companies who didn't pay proper attention to begin with.
andy car
Greenhorn

Joined: Nov 10, 2005
Posts: 7
Thank you it makes alot more sense now. I am 65% done, on to working these boolean expressions.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: very basic question