In previous chapters we have invested a considerable amount of time talking about variables in terms of the various types available and how they are defined. One area we have yet to cover involves the places from which a variable is accessible once it has been declared. This is a concept known as ''variable scope'' and the scope of a variable is very much dependent upon where it is declared within the Objective-C codeof a program.
In this chapter we will also look at the Objective-C variable storage classes. These are specifiers that tell the Objective-C compiler information about how the variable is going to be used within the application code.
== Variable Scope ==
An Objective-C program will consist of code divided up into functions, classes and code structures (such as ''do .. while'' and ''for '' loops). Invariably a typical program will make extensive use of variables to store and manipulate data. Once a variable has been declared it may or may not be accessible to other sections of the program code. This accessibility depends on where and how the variable was declared and where the code is that needs to access it. This is known as ''variable scope'' and falls into a number of categories, each of which will be covered in this chapter.
== Block Scope ==
Objective-C code is divided into sequences of code blocks and files that define the structure of a program. Each block, referred to as a ''statement block'' , is encapsulated by braces ({}). For example, a ''for'' loop typically contains a statement block:
<pre>
</pre>
In the above example, variable ''j'' is declared within the statement block of the ''for'' loop and as such is considered to be ''local'' to that block. This means that the variable can be accessed from within the ''for'' loop statement block but is essentially invisible and inaccessible to code anywhere else in the program code. Any attempt to access variable ''j'' outside the ''for '' loop will result in a compile error. For example:
<pre>
<tt>error: 'j' undeclared (first use in this function)</tt>
Exception encountered, of type "Error"
[c739b146] /index.php/Special:MobileDiff/8619 Error from line 434 of /var/www/techotopia/includes/diff/DairikiDiff.php: Call to undefined function each()
Backtrace:
#0 /var/www/techotopia/includes/diff/DairikiDiff.php(544): DiffEngine->diag()
#1 /var/www/techotopia/includes/diff/DairikiDiff.php(344): DiffEngine->compareSeq()
#2 /var/www/techotopia/includes/diff/DairikiDiff.php(227): DiffEngine->diffLocal()
#3 /var/www/techotopia/includes/diff/DairikiDiff.php(721): DiffEngine->diff()
#4 /var/www/techotopia/includes/diff/DairikiDiff.php(859): Diff->__construct()
#5 /var/www/techotopia/includes/diff/DairikiDiff.php(980): MappedDiff->__construct()
#6 /var/www/techotopia/extensions/MobileFrontend/includes/diff/InlineDiffFormatter.php(99): WordLevelDiff->__construct()
#7 /var/www/techotopia/includes/diff/DiffFormatter.php(140): InlineDiffFormatter->changed()
#8 /var/www/techotopia/includes/diff/DiffFormatter.php(82): DiffFormatter->block()
#9 /var/www/techotopia/extensions/MobileFrontend/includes/diff/InlineDifferenceEngine.php(117): DiffFormatter->format()
#10 /var/www/techotopia/includes/diff/DifferenceEngine.php(797): InlineDifferenceEngine->generateTextDiffBody()
#11 /var/www/techotopia/includes/diff/DifferenceEngine.php(728): DifferenceEngine->generateContentDiffBody()
#12 /var/www/techotopia/extensions/MobileFrontend/includes/specials/SpecialMobileDiff.php(241): DifferenceEngine->getDiffBody()
#13 /var/www/techotopia/extensions/MobileFrontend/includes/specials/SpecialMobileDiff.php(135): SpecialMobileDiff->showDiff()
#14 /var/www/techotopia/extensions/MobileFrontend/includes/specials/MobileSpecialPage.php(53): SpecialMobileDiff->executeWhenAvailable()
#15 /var/www/techotopia/includes/specialpage/SpecialPage.php(384): MobileSpecialPage->execute()
#16 /var/www/techotopia/includes/specialpage/SpecialPageFactory.php(553): SpecialPage->run()
#17 /var/www/techotopia/includes/MediaWiki.php(281): SpecialPageFactory::executePath()
#18 /var/www/techotopia/includes/MediaWiki.php(714): MediaWiki->performRequest()
#19 /var/www/techotopia/includes/MediaWiki.php(508): MediaWiki->main()
#20 /var/www/techotopia/index.php(41): MediaWiki->run()
#21 {main}