4 Replies - 4701 Views - Last Post: 05 May 2014 - 07:43 AM

#1 npanagakis  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 01-December 12

What is the point of the void function?

Posted 10 December 2013 - 02:50 PM

So I am learning C++, and I came across the void function. What I read told me that it is the same thing as a regular function but it does not return a variable. Whats the point of this? Couldn't you just leave the return statement out of a regular function?
Is This A Good Question/Topic? 0
  • +

Replies To: What is the point of the void function?

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9479
  • View blog
  • Posts: 35,743
  • Joined: 12-June 08

Re: What is the point of the void function?

Posted 10 December 2013 - 02:55 PM

No you can't. If a function does not have a return option then that sort of pisses off compilers.. if the return type doesn't match the header that tends to piss off a compiler.. what data type and value would you feel is representative of "this function returns nothing"?

void is a solid way to flag a method as just a subroutine that does stuff and doesn't need to report back a value.
Was This Post Helpful? 1
  • +
  • -

#3 npanagakis  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 01-December 12

Re: What is the point of the void function?

Posted 10 December 2013 - 03:08 PM

View Postmodi123_1, on 10 December 2013 - 02:55 PM, said:

No you can't. If a function does not have a return option then that sort of pisses off compilers.. if the return type doesn't match the header that tends to piss off a compiler.. what data type and value would you feel is representative of "this function returns nothing"?

void is a solid way to flag a method as just a subroutine that does stuff and doesn't need to report back a value.

Ah, thanks for the reply. It makes sense now, though I did exclude a return statement in a regular function, it did not return any errors, perhaps this is because I am using ming. I'd assume with other compilers I'd have an error.
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9479
  • View blog
  • Posts: 35,743
  • Joined: 12-June 08

Re: What is the point of the void function?

Posted 10 December 2013 - 03:15 PM

Yeah.. IDEs like Visual Studios will pitch a fit if a function has paths that all do not reach a return statement.
Was This Post Helpful? 1
  • +
  • -

#5 Z5T1  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 6
  • Joined: 03-May 14

Re: What is the point of the void function?

Posted 05 May 2014 - 07:43 AM

View Postnpanagakis, on 10 December 2013 - 05:08 PM, said:

View Postmodi123_1, on 10 December 2013 - 02:55 PM, said:

No you can't. If a function does not have a return option then that sort of pisses off compilers.. if the return type doesn't match the header that tends to piss off a compiler.. what data type and value would you feel is representative of "this function returns nothing"?

void is a solid way to flag a method as just a subroutine that does stuff and doesn't need to report back a value.

Ah, thanks for the reply. It makes sense now, though I did exclude a return statement in a regular function, it did not return any errors, perhaps this is because I am using ming. I'd assume with other compilers I'd have an error.


Most modern compilers allow you to omit the return statement and will just return the value in the eax register*. This, while allowed in the latest standard, is bad practice as you have no control over what value your function is returning; it could be anything. It is always much safer to explicitly return a value (unless of course you're using a void function).

* If this statement makes no sense to you, the just think that the function returns a random value.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1