Changes

Jump to: navigation, search

Objective-C Variable Scope and Storage Class

7 bytes added, 19:51, 19 November 2009
no edit summary
The reason for this error is that variables ''j'' and ''k'' are declared in the ''main()'' function and are, therefore, local to that function. As such, these variables are not visible to the ''multiply()'' function resulting in an error when we try to reference them. If we wanted to have access to the values of those variables we would have to pass them through as arguments to the multiply function (for details on function arguments refer to [[An Overview of Objective-C Functions]]) or specify them as global or static variables (discussed below).
As with block scope, it is possible to have multiple variables with the same name inside a single function as long as each instance appears within its own local scope. For example, we can add a while loop to our ''main()'' that has its own local variable also named ''j'':
<pre>
== File Scope ==
In the preceding section on ''global scope'' we talked about how a variable declared outside of any statement blocks is considered to be global and may be access accessed both by code in the same file, or by code in different files. Suppose, however, that you wanted a variable to be accessible ''only'' to code within the file where the variable is declared. This is achieved by using the ''static'' specifier when declaring the variable. For example, the following code is from a file named ''main.m''. This file declares variable ''myVar'' to be static. As such, the variable will be accessible only to code within the ''main.m'' file and cannot be accessed by code in any other file:
<pre>

Navigation menu