From the course: Computer Science Principles: Programming

Unlock the full course today

Join today to access over 24,400 courses taught by industry experts.

Variables and scope

Variables and scope

- When we create variables, we need them to hold values during our program. But, how long do we need them to stick around? As we build programs, we can define how long we want to use variables, using Scope. Scope defines how long a variable, and the memory that is used to store it, is kept. And how it's available for the program. One obvious instance when the variable isn't available any more, is when the program closes. When the program stops, it releases all the memory that it used and frees it for other programs or systems to use. Inside of our program, if we create a variable, we can access it using the name. In our code, if we create a variable and then call a function or method, the variable is available to that method since it was already created. But if we enter into a function or method, and then create a variable there, as soon as that function or method is complete, the variable is thrown away. Including the variable name, and the value. It's like walking down a hallway. If…

Contents