Jump to content
Developer Wiki and Function Reference Links ×

Predefine Function?


Recommended Posts

I got the following test to work using sub-procedures. I get confused about where to put things when there are so many end's... lol

Procedure ProSub;

VAR

id:LONGINT;

result:LONGINT;

gA, gB: BOOLEAN;

A,B,C,D: REAL;

FUNCTION DED_ProSub(VAR id:LONGINT):BOOLEAN;

BEGIN

{ initialize dialog creation }

id := CreateLayout('Procedure/Sub-Procedure',TRUE,'Create','Cancel');

{ Menu 1 }

CreateStaticText(id,4,'Type:',-1);

CreateCheckBox(id,5,'A.');

CreateCheckBox(id,6,'B.');

{ Layout Dialog }

SetFirstLayoutItem(id,4);

SetRightItem(id, 4, 5, 0, 0);

SetRightItem(id, 5, 6, 0, 0);

DED_ProSub:=VerifyLayout(id);

END;

PROCEDURE DD_ProSub(VAR item:LONGINT; data:LONGINT);

BEGIN; { SET DEFAULTS }

CASE item OF

SetupDialogC: BEGIN

SetItemEnable(5,TRUE);

SetItemEnable(6,TRUE);

END;

1:BEGIN

IF ItemSel(5) THEN gA:= TRUE ELSE gA:= FALSE;

IF ItemSel(6) THEN gB:= TRUE ELSE gB:= FALSE;

END;

END;

END;

{ Begin Define Sub-Procedures ------------------------------- }

Procedure Sub;

BEGIN

Rect(A,B,C*2,D*2);

END;

Procedure Sub2;

BEGIN

Rect(A,B,C*4,D*4);

END;

{ End Define Sub-Procedures -------------------------------- }

BEGIN

IF DED_ProSub(id) THEN BEGIN

result:=RunLayoutDialog(id,DD_ProSub);

END;

IF (result=1) THEN BEGIN

A:=0;

B:=0;

C:=50;

D:=50;

IF gA THEN BEGIN

Sub2;

END;

IF gB THEN BEGIN

Sub;

END;

END;

END;

RUN(ProSub);

Link to comment

To avoid confusion with begin and ends, use indentation. There are different styles with this, but all my commercial programming life, I tab (typically 8 spaces) indented after any begin and reverted back before the end.

eg. but only using 4 spaces. Programming style is largely personal style so I would format this differently than you have done, but there are some things that are good practice/corporate style rather than just personal style.

One thing you could have done is create a single function sub and passed a variable as a scaling factor.

Procedure ProSub;

VAR
   id      : LONGINT;
   result  : LONGINT;
   gA, gB  : BOOLEAN;
   A,B,C,D : REAL;

FUNCTION DED_ProSub(VAR id:LONGINT):BOOLEAN;
BEGIN
   { initialize dialog creation }
   id := CreateLayout('Procedure/SubProcedure',
TRUE,'Create','Cancel');

   { Menu 1 }
   CreateStaticText(id,4,'Type:',-1);
   CreateCheckBox(id,5,'A.');
   CreateCheckBox(id,6,'B.');

   { Layout Dialog }
   SetFirstLayoutItem(id,4);

   SetRightItem(id, 4, 5, 0, 0);
   SetRightItem(id, 5, 6, 0, 0);

   DED_ProSub:=VerifyLayout(id);
END;

PROCEDURE DD_ProSub(VAR item:LONGINT; data:LONGINT);
BEGIN { SET DEFAULTS }
   CASE item OF
       SetupDialogC:
       BEGIN
           SetItemEnable(5,TRUE);
           SetItemEnable(6,TRUE);
       END;

       1:
       BEGIN
           IF ItemSel(5) THEN gA:= TRUE ELSE gA:= FALSE;
           IF ItemSel(6) THEN gB:= TRUE ELSE gB:= FALSE;
       END;
   END;
END;

{ Begin Define Sub-Procedures ------------------------------- }

Procedure Sub;
BEGIN
   Rect(A,B,C*2,D*2);
END;

Procedure Sub2;
BEGIN
   Rect(A,B,C*4,D*4);
END;

{ End Define Sub-Procedures -------------------------------- }

BEGIN
   IF DED_ProSub(id) THEN
   BEGIN
       result:=RunLayoutDialog(id,DD_ProSub);
   END;

   IF (result=1) THEN
   BEGIN
       A:=0;
       B:=0;
       C:=50;
       D:=50;

       IF gA THEN
       BEGIN
           Sub2;
       END;

       IF gB THEN
       BEGIN
           Sub;
       END;
   END;
END;

RUN(ProSub);

Link to comment

Hey Josh! Thanks a lot for pointing out that section in the language guide. "subroutines" is exactly what I was trying to accomplish.

Ian, thanks for your help. I'm going to try to keep my scripts more organized using the indentation method you have shared with us.

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...