Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variable not passing from function to main code

Status
Not open for further replies.

Dubbemiah

Programmer
Apr 2, 2005
6
US
Hi folks. I'm pretty new to pascal 7.0 and desire help if any are willing.
What I'm trying to do:
When a user presses "a", the cursor will move 5 spaces to the right. If I replace the last 'x' in the last 'gotoxy' with '10', the program works. But I need the function to return the value of X. Here is the code

program stupid;
uses crt;
var
arrowkey : char;
x, y : integer;
tmp : char;

function MoveCursorX(X : Integer): Integer;
begin
x := (x+5);
end;

begin
x:=5;
y:=5;
gotoxy(x,y);
arrowkey := readkey;
if arrowkey = chr(65) then
MoveCursorX(X);
gotoxy(x,y);
readln(tmp);
end.


Thanks in advance for your help.
 
You need to set the result of the function;

e.g.

function MoveCursorX(X : Integer): Integer;
begin
X := (X+5);
MoveCursorX := X;
end;

In the main code, you can then use the function as follows;

if arrowkey = chr(65) then X=MoveCursorX(X);
gotoxy(X,Y);


 
Just to clarify, Stackdump is correct in giving you a much more appropriate way to use functions.

But what you've done is a little complicated and worth understanding since it's pretty educational about how pascal works.

You've created a "function", which in Pascal (contrast with C!!) is something that returns a value. If you don't want to return a value, you'd usually create a procedure (equivalent of C's void functions). But you've called the function as though it were a procedure. Later versions of Turbo Pascal allow this, because it can be helpful where you have a function that does something as well as returning something; sometimes you might want it to do its stuff but you don't care what it returns (frankly it's bad practice to use functions this way).

Your function then defines a parameter "X", which takes precedence, locally, over the global variable "X".

So what you're doing is passing a function a value equal to the global variable "X", and putting it in the local parameter with the same name. You then change the local parameter, and return from the function without returning any value at all. The main program ignores the value that wasn't returned (!) and the global variable still contains the original value...

Hope that helps to explain what was going on. Stackdump's approach should fix it anyway.
 
Alternatively:

procedure MoveCursorX(var X : Integer);
begin
x := (x+5);
end;


and then you can keep the rest of your code as it is, since this will modify the "global" variable as described by lionelhill.


Hope this helps.




[vampire][bat]
 
Thanks everyone.

earthandfire, I tried your procedure, but it is still not working for me. My cursor isn't moving when I press "A". Is there something you missed or I missed? Current code with your modification:

program stupid;
uses crt;
var
arrowkey : char;
x, y : integer;
tmp : char;

procedure MoveCursorX(var X : Integer);
begin

x := (x+5);
end;



begin
x:=5;
y:=5;
gotoxy(x,y);
arrowkey := readkey;
if arrowkey = chr(65) then
MoveCursorX(X);
gotoxy(x,y);
readln(tmp);
end.
 

Works ok for me. Are you pressing A (rather than a)?

It may be that your particular compiler is case sensitive, so x and X are not the same thing. Try changing variable X to x throughout as below;

program stupid;
uses crt;
var
arrowkey : char;
x, y : integer;
tmp : char;

procedure MoveCursorX(var x : Integer);
begin

x := (x+5);
end;

begin
x:=5;
y:=5;
gotoxy(x,y);
arrowkey := readkey;
if arrowkey = chr(65) then
MoveCursorX(x);
gotoxy(x,y);
readln(tmp);
end.
 
Change

if arrowkey = chr(65) then

to

if upcase(arrowkey) = chr(65) then

Then it will not matter whether you press 'A' or 'a'.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top