Author |
Message |
Mr. T
|
Posted: Sun Jan 30, 2005 6:28 pm Post subject: procedure problems |
|
|
everytime i use a procedure, the variables inside the procedure (ints) continue at the number where they left off...how would i make the numbers reset themselves? |
|
|
|
|
|
Sponsor Sponsor
|
|
|
person
|
Posted: Sun Jan 30, 2005 6:37 pm Post subject: (No subject) |
|
|
do something like
var x: int := 0 |
|
|
|
|
|
Mr. T
|
Posted: Sun Jan 30, 2005 6:40 pm Post subject: (No subject) |
|
|
person wrote: do something like
var x: int := 0
i did that a that the beginning of my prog, do i haveta write it each time i use the procedure?!?! |
|
|
|
|
|
Chaos_Theory
|
Posted: Sun Jan 30, 2005 6:45 pm Post subject: (No subject) |
|
|
Pwned wrote: person wrote: do something like
var x: int := 0
i did that a that the beginning of my prog, do i haveta write it each time i use the procedure?!?!
no put it as teh first line of teh prcedure, thatw ay everytime teh procedure is run, it will reset to 0 |
|
|
|
|
|
Mr. T
|
Posted: Sun Jan 30, 2005 6:47 pm Post subject: (No subject) |
|
|
thnx |
|
|
|
|
|
basketball4ever
|
Posted: Sun Jan 30, 2005 8:12 pm Post subject: (No subject) |
|
|
to have good form in a program, you need local and global variables.
A local variable is a variable inside a procedure, while a global is outside. You ONLY use global variables if the variable is used in more than one procedure. So if you're thing has more than once using whatever variable you're using... set it as global then in the beginning of each procedure that you need it to reset... do this:
code: |
%you're global variables
var yourvariable : int :=0
proc examplenum1
yourvariable := 0
%ur code
end examplenum1 |
|
|
|
|
|
|
md
|
Posted: Sun Jan 30, 2005 9:13 pm Post subject: (No subject) |
|
|
actually you should only use global varialbes if there is no way you could write hte program without them, it is espetially important to keep loop variables local though. |
|
|
|
|
|
wtd
|
Posted: Mon Jan 31, 2005 3:11 am Post subject: (No subject) |
|
|
basketball4ever wrote: You ONLY use global variables if the variable is used in more than one procedure.
Bzzzzt! Wrong! But thank you for playing our game.
This is where you should be using argument passing.
code: | procedure greeting (name : string)
put "Hello " + name
end greeting
procedure parting (name : string)
put "Bye " + name
end parting
var test_name : string := "Bob"
greeting (test_name)
parting (test_name) |
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
Token
|
Posted: Mon Jan 31, 2005 12:04 pm Post subject: hmm |
|
|
well considering that you usually use a procedure to execute a large group of code just put in the beginning of the procedure x:= 0 for example
code: |
var y, add, c : int
procedure background
y := 0
add := 0
c := 0
loop
for i : 1 .. maxx - 50
c := c + 1
drawfillbox (i + add, y, i + add + 50, y + 50, red + c)
add := add + 50
delay (50)
end for
end background
background
|
so now each time you run background it resets color and add |
|
|
|
|
|
basketball4ever
|
Posted: Mon Jan 31, 2005 12:08 pm Post subject: (No subject) |
|
|
wtd wrote: basketball4ever wrote: You ONLY use global variables if the variable is used in more than one procedure.
Bzzzzt! Wrong! But thank you for playing our game.
This is where you should be using argument passing.
code: | procedure greeting (name : string)
put "Hello " + name
end greeting
procedure parting (name : string)
put "Bye " + name
end parting
var test_name : string := "Bob"
greeting (test_name)
parting (test_name) |
yeh... and ur using the var test_name more than one procedure... i dont get what you're saying mr.wtd |
|
|
|
|
|
wtd
|
Posted: Mon Jan 31, 2005 1:52 pm Post subject: (No subject) |
|
|
basketball4ever wrote: wtd wrote: basketball4ever wrote: You ONLY use global variables if the variable is used in more than one procedure.
Bzzzzt! Wrong! But thank you for playing our game.
This is where you should be using argument passing.
code: | procedure greeting (name : string)
put "Hello " + name
end greeting
procedure parting (name : string)
put "Bye " + name
end parting
var test_name : string := "Bob"
greeting (test_name)
parting (test_name) |
yeh... and ur using the var test_name more than one procedure... i dont get what you're saying mr.wtd
No, I'm passing the value of "test_name" into each of those procedures. The prodedure itself knows nothing about the outside world, which is exactly the way it should be.
When I write:
code: | procedure greeting (name : string)
put "Hello " + name
end greeting |
And then look at this, I don't need to look outside the procedure to understand it. I know that it gets a string, which it calls "name", tacks "Hello " onto the front of that name, then prints it to the screen.
If I want to use this procedure elsewhere, I can copy and paste just it, and I know it'll work just fine.
Now, if I rewrite that using your logic...
code: | var name : string
procedure greeting
put "Hello " + name
end greeting |
Now, when I look at this procedure, what type is "name"? I ask because that could make '"Hello " + name' break. I could just look at the external variable declaration to find out... but what if it's gotten seperated from the procedure and is mixed in with hundreds of lines of code?
Now, there's also the issue of calling it. Your way I have to write out two separate lines.
code: | name := "Bob"
greeting |
My way:
Mine is more flexible and easier to understand. Someone reading the first example has no idea that changing "name" changes the effect "greeting" has. |
|
|
|
|
|
basketball4ever
|
Posted: Mon Jan 31, 2005 1:59 pm Post subject: (No subject) |
|
|
yes i know how to do the passing procedure paramter pass thingy x_X.
But for many instances,to save lines and all... y not just use global variables. ??? |
|
|
|
|
|
md
|
Posted: Mon Jan 31, 2005 3:52 pm Post subject: Re: hmm |
|
|
Token wrote: well considering that you usually use a procedure to execute a large group of code just put in the beginning of the procedure x:= 0 for example
code: |
var y, add, c : int
procedure background
y := 0
add := 0
c := 0
loop
for i : 1 .. maxx - 50
c := c + 1
drawfillbox (i + add, y, i + add + 50, y + 50, red + c)
add := add + 50
delay (50)
end for
end background
background
|
so now each time you run background it resets color and add
so what would happen if your procedure called another procedure that also used y? it would reset y, and the first procedure would be fubar'd.
Unless a variable is too large to pass as a parameter it should ALWAYS be local to a procedure. This is esptially important with loop counters.
wtd wrote:
...
code: |
name := "Bob"
greeting
|
My way:
Mine is more flexible and easier to understand. Someone reading the first example has no idea that changing "name" changes the effect "greeting" has.
wtd's way is not only the better way it is also the proper way. If you want to continue programming you will eventually need to learn how to do things in such a way that other people can read and understand your code.
basketball4ever wrote:
yes i know how to do the passing procedure paramter pass thingy x_X.
But for many instances,to save lines and all... y not just use global variables. ???
It's better to write good code, then to to save a few lines by needlessly complicating your code. |
|
|
|
|
|
Viper
|
Posted: Sat Feb 05, 2005 1:24 pm Post subject: (No subject) |
|
|
you could use an if statement (couldent he? ) something like
if num=777 then
num:=0
end if |
|
|
|
|
|
|