Post Reply 
Social Buttons
 
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tutorial on Scope
Author Message
Scorch Offline
Intermediate C++
********

Trade Count: (0)
Posts: 83
Joined: Jan 2011
Reputation: 15
Post: #1
Tutorial on Scope
What is scope? Well scope is how something relates to the program, whether globally or locally, it's important to know how it works.

Think of local variables like levels in a video game.
Code:
void level2()
{
   int x = 12;
}

void level1()
{
   int x = 2;
}
The reason this compiles fine is because x only exists in the scope of its function. This is normally a good thing and be very sure when you want to make global variables.

Code:
int x = 12;

void level1()
{
//do something
}

void level2()
{
//do something
}

Now you cannot use x again as a variable because it is global. However this means you can use it in any function you want.

Scope is important to realise when passing variables or just calling functions.
Consider this,
Code:
int main()
{
  Func();
}

void Func()
{
//Do something
}
This will produce a, "Func is undeclared" error. The reason this happens is because the compiler reads left-to-right and up-to-down.

This means now this will work because the compiler knows what Func isCool.
Code:
void Func()
{
//Do something
}
int main()
{
  Func();
}

However if you want Func below main that's entirely possible.
Just be sure to declare to the compiler that your going to have a function called Func. Here's an example.
We declare of a future function and the program will run fine.
Code:
void Func();

int main()
{
   Func();
}

void Func()
{
   //Do something
}

So now about passing variables. That's what the () in a function are for.
Code:
void Func(int a)
{
   // Do something
}

int main()
{
   int b = 12;
   Func(b);
}
Wondering how this works?
Remember how I said variables are local to that function?
Your passing a copy of the value of b into the int a in Func.
Func then stores a copy of b's value in it's own local variable a.
It's important to realise this because what if you want to directly edit b?Huh

This is where pointers/references come in.
Code:
#include <iostream>

void Func(int &b)
{
    ++b;
    std::cout<<b;
}

int main()
{
    int b = 20;
    int *a = &b;
    Func(*a);
    std::cin.get();
    return 0;
}
Using these will allow you to edit local variables in different fuctions without making them global.

This concludes my intro to scopeTongue.
(This post was last modified: 01-16-2011 03:17 AM by Scorch.)
01-16-2011 03:02 AM
Find all posts by this user Quote this message in a reply
Hidden Dragon Offline
Amazing
*******

Trade Count: (0)
Posts: 63
Joined: Jan 2011
Reputation: 4
Post: #2
RE: Tutorial on Scope
Very nice, it explains the concept clearly and is easy to understand. It's a pretty basic concept but important to know.
01-16-2011 03:14 AM
Find all posts by this user Quote this message in a reply
Ironside Offline
Site Owner
**********

Trade Count: (0)
Posts: 170
Joined: Nov 2010
Reputation: 7
Post: #3
RE: Tutorial on Scope
Indeed, another important post.
01-16-2011 11:17 AM
Find all posts by this user Quote this message in a reply
Post Reply 


Forum Jump:


User(s) browsing this thread: