Subscribe Bookmark RSS Feed

Creating Graph Boxes within User-Defined Functions

In the example script below, the first four lines just create a window with a star in it - no problem.  Next however, I want to create a user-defined function ("fn_Star") to enable me to introduce parameters that will (a) modify the look of the star and (b) allow me to call it multiple times.  However, when I try to do that as shown in the second part of the script, the Graph Box I create within the function doesn't recognize the variables I define earlier within that same function (the error in the log tells me that Chart2 doesn't know what X2 is).

// First part;

X1 = [0, .1, 1, .1, 0, -.1, -1, -.1];

Y1 = [1, .1, 0, -.1, -1, -.1, 0, .1];

Chart1 = GraphBox(FrameSize(300, 300), XScale(-1, 1), YScale(-1, 1), Fill Color( "green" ); Polygon( X1, Y1 ));

new window("Window1", Chart1);

// Second part;

fn_Star = function({}, {default local},

   X2 = [0, .1, 1, .1, 0, -.1, -1, -.1];

   Y2 = [1, .1, 0, -.1, -1, -.1, 0, .1];

   Chart2 = GraphBox(FrameSize(300, 300), XScale(-1, 1), YScale(-1, 1), Fill Color( "green" ); Polygon( X2, Y2 ));

   new window("Window2", Chart2);

);

fn_Star;

// End of script;

Why is this please?  And what is the easiest way I can modify the function definition to make it do what I want?

Many thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
Solution

It works if deleting {default local}, from the function. I am not sure why, but maybe the new window object, as it exists outside the function context, needs a global db variable.

3 REPLIES
Solution

It works if deleting {default local}, from the function. I am not sure why, but maybe the new window object, as it exists outside the function context, needs a global db variable.

So it does - how very interesting!  I confess I've got into the habit of using {default local} for all of my function definitions simply with a view to keeping everything as tidy as possible, and hadn't bothered to check to see if it made any difference in this instance.  Many thanks!

David_Burnham

Super User

Joined:

Jul 13, 2011

For some bizarre reason a recent email notification draw my attention to this posting, even though it is now only and answered.  However, I can't say I like the idea of getting rid of "default local", which seems fundamental to the concept of a function.  I think the problem is simpler - functions require parentheses i.e. to call the user-defined function without arguments you write:

fn_Star()

This seems to fix the problem.

-Dave