How to change the value of a parameter within the procedure
Author |
Message |
firefreak113
|
Posted: Wed Feb 05, 2014 10:31 pm Post subject: How to change the value of a parameter within the procedure |
|
|
What is it you are trying to achieve?
I am trying to change the x and y values of a ball on the screen by changing the parameter value in the procedure that updates the ball's position.
What is the problem you are having?
When I compile, the compiler tells me that the parameters x and y that I am trying to change are not variables. I know this, but it still seems like it should work, since variables in parameters can still be used, for instance, this is valid:
Turing: |
var simonsName:string:="Simon"
procedure printName(name:string)
put name
end printName
printName(simonsName)
|
Here I can use the name parameter by printing it.
Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
Turing: |
process update (x,y,dx,dy,c: int)
drawfilloval(x,y, 5, 5,c )
delay(5)
View.Update
cls
x: =(x+dx )
y: =(y+dy )
if(x- 5)=0or (x+ 5)= maxx then
dx: =(- 1)*dx
dy:=Rand.Int (- 1, 1)
elsif(y- 5)=0or (y+ 5)= maxy then
dy: =(- 1)*dy
dx:=Rand.Int (- 1, 1)
end if
end update
|
When I try to compile, I get errors where I set x equal to x+dx and the same for y:=(y+dy),dx:=(-1)*dx, dy:=Rand.Int(-1,1), dy:=(-1)*dy and dx:=Rand.Int(-1,1)
The error message is "Left side of assignment is not a variable and hence cannot be assigned to
Please specify what version of Turing you are using
Turing 4.1.1
P.S. I just started learning turing two days ago
 |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
Raknarg

|
Posted: Wed Feb 05, 2014 10:46 pm Post subject: RE:How to change the value of a parameter within the procedure |
|
|
this is probably what you're looking for. Try running this:
Turing: |
var myName : string := "Raknarg"
proc yourNameIsBob (var name : string)
name := "Bob"
end yourNameIsBob
yourNameIsBob(myName)
put myName
|
This is called passing by reference, rather than passing by value |
|
|
|
|
 |
Tony

|
Posted: Thu Feb 06, 2014 1:12 am Post subject: Re: How to change the value of a parameter within the procedure |
|
|
firefreak113 @ Wed Feb 05, 2014 10:31 pm wrote:
process update(x,y,dx,dy,c:int)
drawfilloval(x,y,5,5,c)
delay(5)
View.Update
cls
Before you run into problems... presumably the only reason for having this be process instead of procedure is that you would fork this process and have something else execute concurrently. What happens when "process update" draws the oval, but while it waits (delay) a different process gets to it's clear-screen stage? |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
 |
firefreak113
|
Posted: Thu Feb 06, 2014 8:00 am Post subject: Re: How to change the value of a parameter within the procedure |
|
|
This is the only process that clears the screen, and it is being forked from a loop. |
|
|
|
|
 |
J-son
|
Posted: Thu Feb 06, 2014 11:23 pm Post subject: Re: How to change the value of a parameter within the procedure |
|
|
Turing: |
process update (x,y,dx,dy,c: int)
drawfilloval(x,y, 5, 5,c )
delay(5)
View.Update
cls
x: =(x+dx )
y: =(y+dy )
if(x- 5)=0or (x+ 5)= maxx then
dx: =(- 1)*dx
dy:=Rand.Int (- 1, 1)
elsif(y- 5)=0or (y+ 5)= maxy then
dy: =(- 1)*dy
dx:=Rand.Int (- 1, 1)
end if
end update
|
:-: If your 'x' variable is not a global variable, it will not refresh your screen anyways.
:-: So you cannot assign it. It is not a variable, really.
:-: When you have the vars in the brackets at the top, by the proc name, I think those are references.
:-: Those would be needed for items such as, I can't think of any right now.
:-: You can easily fix your problem by removing the sub vars at the top and using your global variables instead. E.g, what your ball's co-ordinates (x,y) are.
If you want to clear screen use a cls after View.Update, after the code that draws. You don't need a process. |
|
|
|
|
 |
|
|