Variable scope

walden systems, geeks corner, programming, languages, objective c, c, scope, local scope, global scope, functions, variables, developer, object oriented language, OOP
Objective-C defines a small but powerful set of extensions to the ANSI C programming language that enables sophisticated object-oriented programming. Objective-C is the native language for Cocoa programming—it’s the language that the frameworks are written in, and the language that most applications are written in. You can also use some other languages—such as Python and Ruby—to develop programs using the Cocoa frameworks. It’s useful, though, to have at least a basic understanding of Objective-C because Apple’s documentation and code samples are typically written in terms of this language.

In Objective-C, programs have code divided up into functions, classes and code structures. A program uses 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: block scope, method scope, global scope and file scope.

Block scope

Variables declared in a code block is only accessible within that block but is inaccessible to code anywhere else in the program code. Any attempt to access them outside the code block will result in a compile error. Similar to C#, this rule also applies to nested code blocks. Not all programming languages have the block scope, one example is JavaScript where we must use the "local" keyword to force the variable scope.

A side effect of variable scope within this context is that it allows us to have more than one variable with the same name as long as they are in different scopes.

1    int x;
2    int y = -1 ;
3
4    for (x = 0; x < 3; x++)
5    {
6        int y = x * 10;
7        NSLog (@"y = %i", y);
8    }
9    NSLog (@"outside y = %i", y);
10   . . .


The above code will have the following output :

    y = 0
    y = 10
    y = 20
    outside y = -1


Method scope

Method are nothing more than statement blocks that they are encapsulated in braces and variables declared within those braces are local to that function block. In some programming languages such as JavaScript, methods and code bloc are treated differently since to keep a variable in block scope, we must use the "local" keyword.

1    int addNum()
2    {
3        int a = 3; 
4        int b = 2; 
5        return a + b;
6    }
7
8    int main () 
9    {
10       int sum ;
11       sum = addNum();
12   }


Variables a and b are only available in function addNum(). Same thing goes with variable sum in function main().

Global scope

A variable that has global scope is accessible to code anywhere else in an Objective C program. The code can also be in different file then where the variable was declared. Global variables are declared outside of any statement blocks. This is in contrast to C# which does not support global scope but can be simulated using a static class.

Declaring a variable outside of any function or code block puts the variable in a global scope but to make the variable accessible to other files, we use the "extern" keyword. This is similar to Bash where we would use the "export" keyword to make the variable accessible to other Bash files.

1    #import 
2
3    int myVar1 = 1;
4    int myVar2 = 2 ;
4
5    int main ()
5    {
6        NSLog (@"myVar = %i", myVar1);
7    }


The variable myVar1 is accessible anywhere in this file. The variable myVar2 is accessible anywhere where this file is included.

File scope

If we want a variable to only be accessible within the file where the variable is declared by using the static specifier. In C#, however, we would use "static" keyword to allow other files to access the variable.

        
1    static int myFileVar = 0;  
2
3    void myFunc1()  
4    {  
5        y = y + 3 ; 
6    } 
7
8    int myFunc2() 
9    {
10       int myProd ;
11       myFunc1() ;
12       myProd = y * 2 ;
13       return myProd ;
14   }