Scope

Questions about programming languages and debugging
User avatar
hackerdude
On the way to fame!
On the way to fame!
Posts: 29
Joined: 16 Sep 2009, 16:00
14
Contact:

Scope

Post by hackerdude »

Explain in your own words: Global variables, Local variables, and Scope

Ok, Global variables I know are what it says Global.. like say "Name" can be used anywhere in the program as a variable. A Local variable I believe is a variable that can only be used within a certain module. So say "hackerName" cant be used in another module only the one it is declared in, what is scope though? Can someone please explain this stuff better, esp. scope

User avatar
IceDane
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 197
Joined: 12 Aug 2009, 16:00
14

Post by IceDane »

Explained in C++:

Code: Select all

// Includes here
int GlobalInteger = 4;

int main(void)
{
     int LocalInteger = 31337; // Only valid inside main. Its "scope" is main.

     for(int i = 0; i < 10; i++)
     {
          char LocalChar = 'c'; // This variable goes out of scope after the for loop ends.
   
     } // LocalChar goes out of scope here.

     cout << GlobalInteger << endl; // Fine
     cout << LocalInteger << endl; // Fine
     cout << LocalChar << endl;  // Will not compile. 

     while(true)
     {
          int LocalInteger = 10; // Redefined the variable. The other still exists.
          // But the one that will be used is the one in the innermost scope.
          cout << LocalInteger << endl; // Prints 10
          break;
     }
     cout << LocalInteger << endl; // Prints 31337
     
     return 0;
}


I didn't compile this, but I am almost 100% sure that the stuff with the innermost scope is correct. Try it out.

User avatar
hackerdude
On the way to fame!
On the way to fame!
Posts: 29
Joined: 16 Sep 2009, 16:00
14
Contact:

Post by hackerdude »

Scope - is used to describe the part of a program in which a variable may be accessed. A local variables scope usually begins at the variables declaration and ends at the end of the module in which the variable is declared.

So the scope is like the scope of how much a module can be used in the program?

Sorry I do not know C++ yet
learning puesdo code and programming logic basics

User avatar
hackerdude
On the way to fame!
On the way to fame!
Posts: 29
Joined: 16 Sep 2009, 16:00
14
Contact:

Post by hackerdude »

so for example.. global would all be called within the main module which is at the top of the programming heirarchy chart, local variables would just be within modules that are within the main() module, parameters are what is included within the parenthesis in the modules name like Module Main(Subroutine + anotherSubroutine)

Code: Select all

 Module Main()
Declare Real cupsNeeded
Call showIntro()
Call getCups (cupsNeeded)
Call cupsToOunces(cupsNeeded)
End module

Module showIntro()
Display "this prog converts measurements"
Display "in cups to fluid ounces. For yours"
Display "reference the formula is 1 cup = 8 fluid ounces"
End Module

Module getCups(Real Ref cups)
Display "Enter the number of cups"
Input cups
End module

Module cupsToOunces(Real cups)
Declare Real ounces
Set ounces = cups * 8
Display "That converts to ",ounces, "ounces."
End Module
What would be considered the scope? So cups & ounces are local variables, and the global variables are cupsToOunce, getCups, and showIntro?

User avatar
hackerdude
On the way to fame!
On the way to fame!
Posts: 29
Joined: 16 Sep 2009, 16:00
14
Contact:

Post by hackerdude »

So by "Scope" there is really only two, global and local? and if those are not global variables, what would be an example of one? could cups be call in another module and THEN it is considered global? or if it local there is no way to call it to begin with, Also does order of modules matter? Like say the Module can only call upon the modules before them or is it different with each language? thanks

edit:: sorry I seen cups in 2 different modules in the code so im trying to see how that is considered local

User avatar
hackerdude
On the way to fame!
On the way to fame!
Posts: 29
Joined: 16 Sep 2009, 16:00
14
Contact:

Post by hackerdude »

Yes, im still in the early chapters of programming logic :lol:

So Scope is pretty much just how far the variable or module or whatever can see within its code?

What you said makes me think again about functions because its actully one of my questions (so when I said it was a "global variable" that would = a function?)

Code: Select all

Function is another name for ________. 
 
A) 
 
Variable  
B) 
 
Sub class  
C) 
 
Module  
thats the question.. is there any good information you can tell me about the difference betweeen "Pass by Value" and "Pass by Reference" in dealing with computer programming?

and btw ; as a programmer how many actully make flowcharts, hand trace programs, or create heirarchy charts before you start the actual coding of the program?

User avatar
3XTORTION
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 246
Joined: 29 Jul 2007, 16:00
16
Contact:

Post by 3XTORTION »

When you pass by value to a function (for example), it will copy the value stored in the variable you pass and if you change the copied variable it will not change the variable in the function that called the function.

However, when you pass by reference, you pass the address of variable, rather then the variable itself. So when you change the variable in the function, it WILL be changed in the function that called this function.

User avatar
hackerdude
On the way to fame!
On the way to fame!
Posts: 29
Joined: 16 Sep 2009, 16:00
14
Contact:

Post by hackerdude »

Thanks P4inL0v3r, I think I have a better understanding now of modules and functions. So basically Pass by Value and Pass by Reference determines if it is a Global or Local variable?

"When you pass by value to a function (for example), it will copy the value stored in the variable you pass and if you change the copied variable it will not change the variable in the function that called the function.

However, when you pass by reference, you pass the address of variable, rather then the variable itself. So when you change the variable in the function, it WILL be changed in the function that called this function

Pass by value = Cant be changed globally
Pass by reference = Can be changed globally?

User avatar
IceDane
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 197
Joined: 12 Aug 2009, 16:00
14

Post by IceDane »

hackerdude wrote:Thanks P4inL0v3r, I think I have a better understanding now of modules and functions. So basically Pass by Value and Pass by Reference determines if it is a Global or Local variable?

"When you pass by value to a function (for example), it will copy the value stored in the variable you pass and if you change the copied variable it will not change the variable in the function that called the function.

However, when you pass by reference, you pass the address of variable, rather then the variable itself. So when you change the variable in the function, it WILL be changed in the function that called this function

Pass by value = Cant be changed globally
Pass by reference = Can be changed globally?
Passing by value and passing by reference are two concepts that don't really have anything to do with the concept of scope.

I'm sorry for posting assuming you knew a programming language with a C-like syntax, but you didn't really say what language you did.

Imagine you have a module - which is the same as a function. A function is a certain section of reusable code which can contain its own variables. If you have two functions; Foo and Bar, those can contain their own variables, declared inside them.
Even if they're name the same thing, the variables in each function have no connection to each other.

Pseudo-code:

Code: Select all

Function Foo
start
     Integer Var1
end

Function Bar
start
     Integer Var1
end
See? Both are called Var1, but they are two different variables. When called, they can contain two different values and they will never have anything to do with each other. Their scope is their function. When the functions start, those variables are created, and when they end, they are destroyed.

However, if you declare a variable outside of those functions. Like, put a variable right above the topmost function, that variable is available to both functions. If one modifies it, it will be modified in the other if it uses it afterwards.

You will only confuse yourself if you start mixing passing by value and reference into this stuff, because you can pass the global variable to a function by value, and modify that value in there, without modifying the global variable.

However, if you want Passing by Value and Passing by Reference explained, just say so.

User avatar
hackerdude
On the way to fame!
On the way to fame!
Posts: 29
Joined: 16 Sep 2009, 16:00
14
Contact:

Post by hackerdude »

Thanks, I think I understand better now. I will read the chapter on Pass by Value and Pass by reference again. Im absorbing as much as I can right now in this class.

So functions are what helps modules operate on, you can use multiple modules filled with functions to do what you want your program to do, would a module file be compared to a .dll file?..

Parameters are whats in between the functions parenthesis? like say

Code: Select all

 moduleGetNumber ( 1 + 2 = Parameter)?
and finally If I had two variables named the same, in different modules, even though they were local variables, would that not cause a possibility for an exploit on the program? since both variables have the same exact name?

also plz dont apologize for useing C syntax, I eventually want to learn that, and I just appricaite your help right now

User avatar
f4Gg0t_43
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 245
Joined: 13 Sep 2008, 16:00
15
Contact:

Post by f4Gg0t_43 »

Passing by value, just passes the value of the variable, like

Code: Select all

int a = 5
function(a)
is the same as

Code: Select all

function(5)
You are just passing the value of the variable. Passing by reference is different, you give the function the address (where the variable is stored in memory) of the variable, so instead of just getting the value, you have access to the actual variable. It has nothing to do with scope though.

EDIT: I suggest learning a programming language, so you can actually try stuff out yourself, it makes it a lot easier (and fun) just experimenting with stuff.

User avatar
f4Gg0t_43
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 245
Joined: 13 Sep 2008, 16:00
15
Contact:

Post by f4Gg0t_43 »

hackerdude wrote:So functions are what helps modules operate on, you can use multiple modules filled with functions to do what you want your program to do, would a module file be compared to a .dll file?..

Code: Select all

Function is another name for ________. 
 
A) 
 
Variable  
B) 
 
Sub class  
C) 
 
Module  
The answer for this question is C, Module. I think you are mistaking "Module" for "Class".

User avatar
hackerdude
On the way to fame!
On the way to fame!
Posts: 29
Joined: 16 Sep 2009, 16:00
14
Contact:

Post by hackerdude »

f4Gg0t_43 wrote:Passing by value, just passes the value of the variable, like

Code: Select all

int a = 5
function(a)
is the same as

Code: Select all

function(5)
You are just passing the value of the variable. Passing by reference is different, you give the function the address (where the variable is stored in memory) of the variable, so instead of just getting the value, you have access to the actual variable. It has nothing to do with scope though.

EDIT: I suggest learning a programming language, so you can actually try stuff out yourself, it makes it a lot easier (and fun) just experimenting with stuff.
I'm in my 2nd programming class, the peusdocode I am useing is that same as what they learn in the military for computer science. Just trying to upgrade my knowledge. I have to learn this programming class before I ma able to learn a programming language, I have a book on Python though I am looking at taking a double course for free.. check this out its free to learn you just dont get credits

Code: Select all

http://ocw.mit.edu
they have a nioe python class on there but i want to get the basics first

User avatar
f4Gg0t_43
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 245
Joined: 13 Sep 2008, 16:00
15
Contact:

Post by f4Gg0t_43 »

hackerdude wrote:I have to learn this programming class before I ma able to learn a programming language
Why?

User avatar
hackerdude
On the way to fame!
On the way to fame!
Posts: 29
Joined: 16 Sep 2009, 16:00
14
Contact:

Post by hackerdude »

f4Gg0t_43 wrote:
hackerdude wrote:I have to learn this programming class before I ma able to learn a programming language
Why?
Because that is how the college course is designed, and once I learn these basics I should be able to understand all languages somewhat. This is what college students in America are being tought, if thats what you care about.

Isnt puesdocode like the first thing to learn? There is 12 credits of college class needed b4 diving into a new language. Does not matter the language, you just have to understand how stuff works.

Locked