Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

-(void) re visted

Can someone break down/define the use of void? Why/when it should be used. I'm trying to think back to previous lessons and I'm drawing a complete blank. (I'm sure you can imagine how frustrating this can be!)

example code from current video: Intercepting Motion Events

-(void) motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event   {
    NSLog(@"motion began");
}

Thank you,

Your fellow treehouse student

2 Answers

you use void as a method or functions return type when it doesnt return anything. In that method you posted nothing is returned (all you do is log that string to the console, you dont return a value) so its return type is void.

On the other hand, I could write a method that returns an actual value such as this one:

- (float) addFloatOne:(float)floatOne floatTwo:(float)floatTwo {

  return floatOne + floatTwo;

}

as you can see it returns a value. It returns a float value so its return type is float.

here is another example of a method with a return type of void

- (void) doSomething {

NSLog(@"something is being done");

}

that method does not return a value. It just does something. In fact most methods dont return anything, they just do stuff. So you will see a lot of methods with a return type of void.

Thank you so much! That makes a lot more sense now!