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
Francis Chouquet
290 PointsA bit lost with variables and functions
Hi there,
I just started the iOS Dev class, and so far so good (just finished the Scopes video). But there are few points that are a bit blurry for me. If someone could help me with simple words, that would be great.
- Variables
I learnt about int, float and char but I am still not sure when to use what. Could someone tell me simply when I should use one instead of the other ? Then, to display results, I just saw a %s, I know about %c and %d but is there somewhere a table that shows which one corresponds to the chosen variable ?
I understand mostly of these things but it is still a bit not clear for me :)
- functions
In the video about functions, Douglass uses the function "void" and I don't understand why he does use that function.
Thanks a lot for your help.
Francis
5 Answers
eirikvaa
18,015 PointsVariables
When you want to use an int, a float or a char really depends on the application your developing. If you need to store a single character somewhere, use a char. Int vs float is all about precision: If you only need whole numbers (4,200,-30), then you go for an int, but if you need numbers like PI (3,14) or something with that kind of precision, then you use a float. It's really not much to it.
For format specifiers (%d and so on), you can check out this list. It's a little bit long and detailed, so I'll list the most commony used below:
- int - %d
- float - %f (there is also a double, but a float and a double is more or less the same)
- char - %c
- string - %s (a string is just a combination of characters)
Functions
Void is not actually a function, but what's called a return type. A function can either return something or not depending on what you specify. For example, the main function all C programs must have returns an integer:
int main() {
return 0;
}
Functions that don't return anything would have been written like this:
void function() {
}
See the difference? Functions don't have to return anything, that's why we use the void keyword.
Ask again if you have more questions.
Gunjeet Hattar
14,483 PointsHi,
- Variables
- int represents an integer (numbers without a decimal (whole numbers if you may say) ). So when you need to use a number like selecting choices from 1-5 you wouldn't select a number like 2.5. So in situations like these you use int
- You use float on the other hand to represent numbers with decimal points. For e.g. when calculating average marks of a class. A student could get 50.4 percent marks and using only integers (int) will not suffice
- You use char to represent a character. Unlike String which is like a combination of characters, char can only represent one value . For e.g. if you want the users to select from a choice list A-G you cannot use integers so chars seem more appropriate
%s and %d are format specifiers . You use %s to represent string, %d to represent numbers . Technically if you have to output a string (like "Mark" which is not a number or a character) you use %s . More information here Apple Developer Link
Functions
void is just to specify that the particular function will not return any value. You could have a function like int some_function() .... this function will return an int value upon completion wheres when you don't want a function to return anything but just perform some action then you use void.
Hope that helps.
George S-T
6,624 PointsI would really recommend buying a notepad and making notes as you watch the videos, it really helps me and Im always looking back at them.
Francis Chouquet
290 PointsHi guys,
Thanks for your answers. Now I see more clearly how it works. Not easy to find clear and simple explanations on the web. Yours are easy to understand. I can continue with a clearer vision now :)
Francis Chouquet
290 PointsThat is what I am going to do. I was thinking about it yesterday. I keep all files on my computer and come back to them when I need them. But the last videos were a bit more though and I realized I need to take notes. Thanks George for the idea :)