-
-
Notifications
You must be signed in to change notification settings - Fork 1
Variables
A variable is like a box with a name on it. It stores a value that can change. In C++, you have to tell the computer what kind of value is going to be in the box. You do this by using the keyword "int", "char", "float", etc. For example, you could say "int x" to tell the computer that the box is for a number. After that, you can fill the box with a value. You can also change the value whenever you want.
The scope of a variable is where it can be used. Variables outside of a function can be used anywhere in the program. Variables inside a function can only be used inside that function. You can also pass variables between functions. This means that one function can give a variable to another function.
Variables can be given an initial value when they are declared. This is called initialization. For example, you could say "int x = 10" to give the box a value of 10. Variables can also be declared without an initial value. Then the box will be empty.
Finally, variables can also be declared using the keyword "const". This means that the value in the box cannot be changed.
int age;
// age is a variable of type integer
age = 20;
// the value of age is set to 20