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, consider 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 will come into existence when showCookTimeForTurkey() starts to execute and ...

Get Objective-C Programming: The Big Nerd Ranch Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.