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 trialDaniel Nochta
1,377 PointsStage 1 IOS VARIABLES
Why I need to use variable when I can just type:
printf("7 days in a week.\n");
Only rational explanation is that I can call the variable another time so I don't need to type number 7 all the time. But the main usage I will only see with long numbers/words? correct?
My programming skills are really beginner one so I'm a bit lost. Don't get me wrong but I just can not see the full potential of variables yet :(
can you please either explain me variables more simple way or try to show me some usable example?
thank you very much
1 Answer
Stone Preston
42,016 PointsAs you start writing more complex programs you will understand why they are important. With simple programs (such as a program that simply prints out a value) variables really dont come into play/have advantages. but with programs that actually do stuff, perform operations, use functions/methods, etc variables are a necessity. most programming would not be possible without them.
you are correct that an advantage of variables is being able to reference them whenever you want how every many times you want
one of the biggest advantages of variables is that they allow you to associate a name with a value. they make your code much easier to read and understand.
hard coding values is referred to as using "magic numbers". you have no idea what they are for. they are just there.
also variables can change (hence the name). lets say I had a variable called temperature. I could raise the temperature by increasing the variable by 1.
int increaseTemp(int temperature) {
return temperature + 1;
}
//...
int temp = 85;
temp = increaseTemp(temp); // temp is now 86
temp = increaseTemp(temp); //temp is now 87
// ... further down in code
temp = increaseTemp(temp); temp is no 88
I couldnt do that with a magic number. I would have to do it manually each time and I would have to go back and check what the value was before I increased it every single time.
int temp = 85;
temp = 86;
it would be too hard to manage that for all your different values you wanted to keep track of.
Daniel Nochta
1,377 PointsDaniel Nochta
1,377 PointsThank you very much, I got it better now. I guess I will see the whole potential later on in the course of IOS.
Is there some list of most commonly used variables or its up to me to create one?
I tried to use the "int increaseTemp" in the Xcode file created by treehouse (course with Doug) but its an error which says its not available here.
Thank you very much
Stone Preston
42,016 PointsStone Preston
42,016 Pointswell to make it work in an actual program you would need to make sure the increase temp function is defined outside and above the main function.
when you say "Is there some list of most commonly used variables or its up to me to create one?" what exactly do you mean. do oyu mean like data types such as int, float, char, double, etc. or just variable names? variable names can be whatever you want provided they comply with the naming rules (cannot start with a number, there are a few special characters they cant contain/start with etc)
Daniel Nochta
1,377 PointsDaniel Nochta
1,377 PointsThank you very much for your help :)
I'm getting it better now. Already at for statement section (loops).
Again thank you:)