• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

very basic question

 
andy car
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 18098
50
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
andy car
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you it makes alot more sense now. I am 65% done, on to working these boolean expressions.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic