This is a post concerning what we call "scope" in C++. This should also be applicable to C and Java etc in some respects, but will be biased towards C++ and any code shown will be C++. (Because it is the real man's programming language, lol)
So, basically, we can define your scope as your present working surface. Programs can normally be thought of having 2 or more scopes at any given time (supposing you are inside a function at all times).
Thusly, we have the Global Scope and the Local Scope, plus Namespaces, which will be dealt with at a later time. The Global Scope includes anything declared outside of your functions, classes, etc. Your local scope would include anything inside the function you are currently in.
so if we had a program that looked like:
Looking at this, we really only need to think in terms of 2 scopes. There is the local scope, and there is the global scope. The global scope contains the variables x and y, and the function main, plus anything defined in iostream, within the namespace std. A namespace is basically a method of encapsulating data, which is a big deal in C++, but will not be addressed here, as to not loose focus. The local scope of the function main includes the variables a and b. Because x and y are declared in the global scope, they can be used in main as if they were declared in main, and any other function, and will retain their values as they are modified by functions. So, say I set x equal to zero in one function, then 4 in another. when I exit the second function, x is still 4. Simple? Good.
Say we had another function in this, call it functionA, like so:
As it is declared now, the function functionA is available globally. If you were to put the prototype statement into main, or any other function, the function then only exists in the scope of that function.
Also, If I were to call functionA in main or any other function, the variables in the original function will no longer be available, thus being why we use function parameters. So, if I wanted to access the value of a in functionA, I would need to tell it to take an integer:
int functionA(int var)
If i wanted to change the value permanently, I would hand it to the function as a reference variable:
int functionA(int& var)
both of these will allow me to just pass a itself from main like so:
functionA(a);
but the second will allow you to manipulate a, while the first will only allow you to get a copy of a.
A scope can also be established inside a function.
for example, the if statement.
if I have a function with an if statement inside of it like so:
There are 2 scopes here, the local scope and the if statement scope. The if statement scope can access variables from the local and global scope, but after the terminating } of the if statement, the variable a disapears.
It's really late right now, And I dont feel like talking about namespaces, but look em up cuz they rock, and if anyone actually finds this helpful, and wants me to write up something on namespaces, just leave a comment. Also, any questions or concerns can be left as comments.
peace
~Bodom
So, basically, we can define your scope as your present working surface. Programs can normally be thought of having 2 or more scopes at any given time (supposing you are inside a function at all times).
Thusly, we have the Global Scope and the Local Scope, plus Namespaces, which will be dealt with at a later time. The Global Scope includes anything declared outside of your functions, classes, etc. Your local scope would include anything inside the function you are currently in.
so if we had a program that looked like:
#include<iostream>
using namespace std;
int x, y;
int main(void)
{
int a,b;
return 0;
}
Looking at this, we really only need to think in terms of 2 scopes. There is the local scope, and there is the global scope. The global scope contains the variables x and y, and the function main, plus anything defined in iostream, within the namespace std. A namespace is basically a method of encapsulating data, which is a big deal in C++, but will not be addressed here, as to not loose focus. The local scope of the function main includes the variables a and b. Because x and y are declared in the global scope, they can be used in main as if they were declared in main, and any other function, and will retain their values as they are modified by functions. So, say I set x equal to zero in one function, then 4 in another. when I exit the second function, x is still 4. Simple? Good.
Say we had another function in this, call it functionA, like so:
[code]
#include<iostream>
using namespace std;
// prototype
int functionA(void);
int x, y;
int main(void)
{
int a,b;
return 0;
}
int functionA(void)
{
x = 0;
return 0;
}
As it is declared now, the function functionA is available globally. If you were to put the prototype statement into main, or any other function, the function then only exists in the scope of that function.
Also, If I were to call functionA in main or any other function, the variables in the original function will no longer be available, thus being why we use function parameters. So, if I wanted to access the value of a in functionA, I would need to tell it to take an integer:
int functionA(int var)
If i wanted to change the value permanently, I would hand it to the function as a reference variable:
int functionA(int& var)
both of these will allow me to just pass a itself from main like so:
functionA(a);
but the second will allow you to manipulate a, while the first will only allow you to get a copy of a.
A scope can also be established inside a function.
for example, the if statement.
if I have a function with an if statement inside of it like so:
void functionB(void)
{
if(x != 0)
{
int a = x + 4;
cout << a;
}
}
There are 2 scopes here, the local scope and the if statement scope. The if statement scope can access variables from the local and global scope, but after the terminating } of the if statement, the variable a disapears.
It's really late right now, And I dont feel like talking about namespaces, but look em up cuz they rock, and if anyone actually finds this helpful, and wants me to write up something on namespaces, just leave a comment. Also, any questions or concerns can be left as comments.
peace
~Bodom
2 Comments On This Entry
Page 1 of 1
Martyr2
01 March 2009 - 07:21 PM
You will want to be careful about how you talk about scope. Scope is a narrowing or broadening of view. Thus a scope can have several scopes. A variable inside an if statement is said to have "block scope" (aka local scope to a control statement) which is then in function scope which might be part of a class scope which that class can be part of a file scope which can be part of a namespace scope which can be part of a project scope and even more outside of that if you go with .NET and assemblies.
It is much easier to think and explain in terms of zoom in and zoom out where one scope is just a piece of a higher scope.
But anyways, like the blog and good post.
It is much easier to think and explain in terms of zoom in and zoom out where one scope is just a piece of a higher scope.
But anyways, like the blog and good post.
Page 1 of 1
← January 2022 →
| S | M | T | W | T | F | S |
|---|---|---|---|---|---|---|
| 1 | ||||||
| 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| 9 | 10 | 11 | 12 | 13 | 14 | 15 |
| 16 | 17 | 18 | 19 | 20 | 21 | 22 |
| 23 | 24 | 25 | 26 | 27 | 28 | 29 |
| 30 | 31 |
Tags
- Adminsitration
- Apple
- assembly
- atmel
- AVR
- avr-libc
- Bada
- beginner
- Blackberry
- boot
- C
- C#, .NET, and XNA
- C++
- ccleaner
- code
- defragmentation
- Desktop
- dynamic
- embedded
- FooBada
- foobar
- foobar2000
- Gnome
- high
- interrupts
- Java
- level
- Linux
- Math
- Matricies
- Matrix
- microcontroller
- nasm
- New
- Open Source
- Operating System
- Oracle
- oscilloscope
- Programming
- Python
- Random Computer Stuff
- Random Rants
- Repair
- Samsung
- sector
- serial
- speed
- Square
- static
- timer
- UART
- Ubuntu
- unix
- USART
- virtualbox
- Visual
- VOIP
- Windows
- Windows 7
- x86
My Blog Links
Recent Entries
-
Playing in Pythonon Apr 24 2011 10:06 PM
-
Past, Present, and Future (and.... a christmas tree in NASM)on Dec 24 2010 09:31 PM
-
Using Virtualbox as a bootloader testing environmenton Nov 13 2010 11:00 PM
-
AVR Oscilloscope, Part 1on Sep 13 2010 07:21 PM
-
My thoughts on Bada as an Operating System and as a Development Platformon Sep 05 2010 10:43 AM
Recent Comments
Search My Blog
21 user(s) viewing
21 Guests
0 member(s)
0 anonymous member(s)
0 member(s)
0 anonymous member(s)



2 Comments








|