Local variables, frames, and the stack

Every function can have local variables. Local variables are variables declared inside a function. They exist only during the execution of that function and can only be accessed from within that function. For example, imagine that you were writing a function that computed how long to cook a turkey. It might look like this:

v​o​i​d​ ​s​h​o​w​C​o​o​k​T​i​m​e​F​o​r​T​u​r​k​e​y​(​i​n​t​ ​p​o​u​n​d​s​)​
{​
 ​ ​ ​i​n​t​ ​n​e​c​e​s​s​a​r​y​M​i​n​u​t​e​s​ ​=​ ​1​5​ ​+​ ​1​5​ ​*​ ​p​o​u​n​d​s​;​
 ​ ​ ​p​r​i​n​t​f​(​"​C​o​o​k​ ​f​o​r​ ​%​d​ ​m​i​n​u​t​e​s​.​​n​"​,​ ​n​e​c​e​s​s​a​r​y​M​i​n​u​t​e​s​)​;​
}​

necessaryMinutes is a local variable. It came into existence when showCookTimeForTurkey() started to execute and will cease to exist once that function completes execution. The parameter of the function, pounds, is also a local variable. A parameter is a local variable that has been initialized to the value of the corresponding argument.

A function can have many local variables, and all of them are stored in the frame for that function. Think of the frame as a blackboard that you can scribble on while the function is running. When the function is done executing, the blackboard is discarded.

Imagine for a moment that you are working on the Baked Chicken recipe. In your kitchen, all recipes get their own blackboards, so you have a blackboard for the Baked Chicken recipe ready. Now, when you call the Seasoned Bread Crumbs recipe, you need a new blackboard. Where are you going to put it? Right on top of the blackboard for Baked Chicken. After all, you’ve suspended execution of Baked Chicken to make Seasoned Bread Crumbs. You won’t need the Baked Chicken frame until the Seasoned Bread Crumbs recipe is complete and its frame is discarded. What you have now is a stack of frames.

Figure 5.3  Two blackboards in a stack

Two blackboards in a stack

Programmers say, When a function is called, its frame is created on top of the stack. When the function finishes executing, its frame is popped off the stack and destroyed.

Let’s look more closely at how the stack works by putting showCookTimeForTurkey() into a hypothetical program:

v​o​i​d​ ​s​h​o​w​C​o​o​k​T​i​m​e​F​o​r​T​u​r​k​e​y​(​i​n​t​ ​p​o​u​n​d​s​)​
{​
 ​ ​ ​ ​i​n​t​ ​n​e​c​e​s​s​a​r​y​M​i​n​u​t​e​s​ ​=​ ​1​5​ ​+​ ​1​5​ ​*​ ​p​o​u​n​d​s​;​
 ​ ​ ​ ​p​r​i​n​t​f​(​"​C​o​o​k​ ​f​o​r​ ​%​d​ ​m​i​n​u​t​e​s​.​​n​"​,​ ​n​e​c​e​s​s​a​r​y​M​i​n​u​t​e​s​)​;​
}​

i​n​t​ ​m​a​i​n​(​i​n​t​ ​a​r​g​c​,​ ​c​o​n​s​t​ ​c​h​a​r​ ​*​ ​a​r​g​v​[​]​)​
{​
 ​ ​ ​ ​i​n​t​ ​t​o​t​a​l​W​e​i​g​h​t​ ​=​ ​1​0​;​
 ​ ​ ​ ​i​n​t​ ​g​i​b​l​e​t​s​W​e​i​g​h​t​ ​=​ ​1​;​
 ​ ​ ​ ​i​n​t​ ​t​u​r​k​e​y​W​e​i​g​h​t​ ​=​ ​t​o​t​a​l​W​e​i​g​h​t​ ​-​ ​g​i​b​l​e​t​s​W​e​i​g​h​t​;​
 ​ ​ ​ ​s​h​o​w​C​o​o​k​T​i​m​e​F​o​r​T​u​r​k​e​y​(​t​u​r​k​e​y​W​e​i​g​h​t​)​;​
 ​ ​ ​ ​r​e​t​u​r​n​ ​0​;​
}​

Recall that main() is always executed first. main() calls showCookTimeForTurkey(), which begins executing. What, then, does this program’s stack look like just after pounds is multiplied by 15?

Figure 5.4  Two frames on the stack

Two frames on the stack

The stack is always last-in, first-out. That is, the showCookTimeForTurkey() will pop its frame off the stack before main() pops its frame off the stack.

Notice that pounds, the single parameter of showCookTimeForTurkey(), is part of the frame. Recall that a parameter is a local variable that has been assigned the value of the corresponding argument. For this example, the variable turkeyWeight with a value of 9 is passed as an argument to showCookTimeForTurkey(). Then that value is assigned to the parameter pounds and copied to the function’s frame.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset