Objective-C Variable Scope and Storage Class

From Techotopia
Revision as of 19:11, 19 October 2009 by Neil (Talk | contribs) (New page: 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 ...)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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 Objective-C code.

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:

int x;

for (x = 0; x < 10; x++)
{
    int j = x + 10;
    NSLog (@"j = %i", j);
}

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:

int x;

for (x = 0; x < 10; x++)
{
    int j = x + 10;
    NSLog (@"j = %i", j);
}

j += 20; // illegal - j is now out of scope

An attempt to compile the above code within the context of an application will result in a compilation error along the lines of the following:

error: 'j' undeclared (first use in this function)

An interesting side effect of variable scope within this context is that it enables us to have more than one variable with the same name as long as they are in different scopes. For example, we can have a variable named j declared outside the for loop and a variable named j declared inside the for loop. Although these variables have the same name they occupy different memory locations and contain different values. This can be illustrated using a simple application that contains two jj variables:

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
        NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];


        int x;
        int j = 54321;

        for (x = 0; x < 10; x++)
        {
                int j = x + 10;
                NSLog (@"Varible j in for loop is %i", j);
        }


        NSLog (@"Variable j outside for loop is %i", j);

        [pool drain];

        return 0;


}

As we can see from the above code, variable j is declared twice but in two different scopes. When compiled and executed we can see that modifying the value of jj within the for loop has no effect on the value assigned to the variable jj declared outside the for loop:

2009-10-19 15:09:39.875 t[8130:10b] Varible j in for loop is 10
2009-10-19 15:09:39.877 t[8130:10b] Varible j in for loop is 11
2009-10-19 15:09:39.877 t[8130:10b] Varible j in for loop is 12
2009-10-19 15:09:39.878 t[8130:10b] Varible j in for loop is 13
2009-10-19 15:09:39.878 t[8130:10b] Varible j in for loop is 14
2009-10-19 15:09:39.879 t[8130:10b] Varible j in for loop is 15
2009-10-19 15:09:39.879 t[8130:10b] Varible j in for loop is 16
2009-10-19 15:09:39.879 t[8130:10b] Varible j in for loop is 17
2009-10-19 15:09:39.880 t[8130:10b] Varible j in for loop is 18
2009-10-19 15:09:39.880 t[8130:10b] Varible j in for loop is 19
2009-10-19 15:09:39.881 t[8130:10b] Variable j outside for loop is 54321

Function Scope