Jump to content
Developer Wiki and Function Reference Links ×

Function Variables


Ben624

Recommended Posts

Can anyone tell me why I can't pass a discrete value to a user-defined procedure or function? For instance, I have written the following function to set minimum and maximum allowable values in a dialog field:

PROCEDURE LimitInput(VAR field : INTEGER; min, max : REAL);

VAR

fldInput : REAL;

TxtOK : BOOLEAN;

BEGIN

TxtOK := GetEditInteger(dlogID,field,fldInput);

IF fldInput > max THEN

setEditInteger(dlogID,field,max);

IF fldInput < min THEN

setEditInteger(dlogID,field,min);

END;

Why can't I use this by specifying the min and max when I call the procedure? eg: LimitInput(kLED1, 0, 35). If I try this, the compiler tells me that I can only pass variables to the procedure, not discrete values. Is that really true? Do I need to declare variables strictly to pass values to my procedure? I can pass values directly to vectorScript's native functions and procedures. What makes mine different?

Thanks!

Ben

Link to comment

Ben,

You are passing kLED1, which seems like a constant, to the procedure and in your declaration the first argument "field" is a variable. You need to pass a variable to the procedure such as:

VAR

varInt: INTEGER;

BEGIN

........

varInt:= kLED1;

LimitInput(varInt,0,35);

........

END;

or change the declaration to:

PROCEDURE LimitInput(field : INTEGER; min, max : REAL);

Link to comment
By putting the Var at the beginning of the list of parameters, you are defining them all as variables. That means that you can't pass a discrete value to them. Put the variable parameters at the end of the list.

Procedure LimitInput(min,Max:Real; Var field:Integer);

Pat, that is only true if the parameters are all the same type and declared in the same parameter group.

eg. procedure XYZ(var A, B, C :Integer);?????{ all are VARs }

eg. procedure XYZ(var A :Integer; B, C :Integer);?????{ only A is a VAR }

but I do it as you do with the VARs at the end, as a convention.

eg. procedure XYZ(B, C :Integer; var A :Integer);?????{ input in the front, answers in the back }

I think it looks better.

I thought the VAR was required in the function definition. Thanks again!
It is only required if you want the function/procedure to return any modifications it made to your variable. Miguel is right, you cannot pass a constant value to a VAR parameter. The procedure has nowhere to put the new value on return.

One other pitfall that I often run into and is somewhat tricky to catch, but I'm getting faster with repeat infractions...

When you write two functions/procedures and one calls the other, if the other one returns a VAR parameter then it MUST be a VAR in the first.

eg.

???procedure XYZ(var A :Integer);

...

???procedure ABC(B :Integer);

???Begin

??????XYZ (B);?????{ will not work as B is not a VAR parameter and XYZ is expecting one }

???End; { ABC }

rather write:

???procedure ABC(var B :Integer);?????{ needs to be a VAR if it will be passed to another procedure }

???Begin

??????XYZ (B);?????{ now it will not work }

???End;???{ ABC }

If you write enough code, you will be cursed with the same message you got earlier, but it will be a little harder to track down. Hopefully you'll not spend as much time as I have in the past looking for the cause.

Raymond

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...