Storage Lifetime
So far, in the applications and code we've been writing, we've been declaring all our variables in our main
function. Since all our other code lives in this function also, we've had full access to all those variables. When we start to build bigger applications, however, and start making use of functions and classes, we need to understand scope and storage lifetime.
Object lifetime refers to how long an object is valid and accessible to us. With most of our variables having been declared in our main
function so far, their lifetime has matched that of the application we've been writing, and there's nothing to worry about. Anytime we've wanted to use that variable, it's been there and valid as we're working within the same scope. Scope refers to a section of code that denotes the lifetime of objects declared within it, so we can see how these terms are related.
We use curly brackets to denote scope, be that in a function...