O'Reilly logo

Objective-C Programming: The Big Nerd Ranch Guide by Mikey Ward, Aaron Hillegass

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required