Turing with Variables--Please Help
Author |
Message |
unicorn-ellie
|
Posted: Tue Nov 12, 2013 6:18 pm Post subject: Turing with Variables--Please Help |
|
|
Hi, I'm a student in ICS20. We just finished learning graphics and now we're doing variables. I have two questions:
-Why are some variables declared before the procedures?
-Why are other variables declared inside procedure display/main program for var?
Thank you in advance. |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
Tony

|
Posted: Tue Nov 12, 2013 7:04 pm Post subject: RE:Turing with Variables--Please Help |
|
|
It's a matter of "scope" -- that is, where is that variable visible from. Variables created inside a procedure exist only in that procedure. |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
 |
Dreadnought
|
Posted: Tue Nov 12, 2013 7:08 pm Post subject: Re: Turing with Variables--Please Help |
|
|
Variables must be declared before they are referenced in the program, hence a variable must be declared before a procedure it.
In general it's good practice to restrict the scope of variables (the part of the code in which they exist and can be referenced) as much as possible. This keeps your code cleaner and allows variable names to be reused without name clashing (declaring a variable with a name that is already in use).
These kind of things don't really matter when you are writing small programs, however once your programs start to get big you'll see that organisation and cleanliness of code is very important (especially when reading code you haven't written).
EDIT: Tony makes a good point which I forgot |
|
|
|
|
 |
unicorn-ellie
|
Posted: Tue Nov 12, 2013 7:11 pm Post subject: Re: Turing with Variables--Please Help |
|
|
Well, this is the example code:
Turing: |
% Declaration Section
var number : int
% Title
procedure title
locate (1, 30)
put "More Math Functions "
end title
%Introduction
procedure introduction
title
locate (3, 1)
put "This program demonstrates two new math functions that are very useful."
end introduction
% User Input
procedure userInput
locate (5, 1)
put "Please enter a number: " ..
get number
end userInput
% Process & Output
procedure display
var answer : real
locate (9, 1)
answer := number div 7
put number, " div ", 7, " = ", answer
answer := number mod 7
put number, " mod ", 7, " = ", answer
end display
% Main Program
introduction
userInput
display
% End of program
|
Can you explain why some variables are declared before the procedures and why some variables are declared after the procedure display?
Mod Edit:
Please wrap you code in either of the following in order to preserve whitespace (indentation) and to highlight the syntax.
code: |
[syntax="turing"] ... code ... [/syntax]
[code] ... code ... [/code ]
|
|
|
|
|
|
 |
Dreadnought
|
Posted: Tue Nov 12, 2013 7:25 pm Post subject: Re: Turing with Variables--Please Help |
|
|
The variable number is used in multiple procedures (userInput and display) so it must be declared before both procedures so that it is in scope (visible) for both procedures.
The variable answer is only used in the display procedure so it is a good idea to declare it inside the procedure so that it cannot be used outside the display procedure. |
|
|
|
|
 |
Raknarg

|
Posted: Thu Nov 14, 2013 11:31 am Post subject: RE:Turing with Variables--Please Help |
|
|
Along with what Dreadnought said, sometimes it's just for organizational purposes. For instance, in this program you could declare all variables at the beginning. But as we pointed out, not all of them were used across the program, so it makes more sense to encapsulate data that actually goes together. |
|
|
|
|
 |
Tony

|
Posted: Thu Nov 14, 2013 4:33 pm Post subject: RE:Turing with Variables--Please Help |
|
|
If, for example, there is a lot of counting going on in the program, you want to avoid having
code: |
var counter_procedure1, counter_procedure2, counter_procedure3,... : int
|
Each procedure can have it's own variable called "counter" and not have to worry about what any other procedure is doing. |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
 |
|
|