Functions
Last updated
Last updated
Functions can be defined with a custom name and must return a value.
All Xylo programs require a root
function in order to render images. The root
function must return a shape type.
The square
function in this example simply returns a square that has been scaled by 100 pixels along the x and y axes. Since the default square is 2x2, the returned shape's dimensions will be 200x200.
Thus, this code will render a 200x200 white square in the center of the image.
Functions can also be defined with a list of custom parameters. Let's say, for instance, we wanted a rect
function with custom width and height parameters.
The rect
function can now take in width
and height
values and pass half their values into the s
function, which scales the square on the x and y axes.
We need to divide both values by two because the default square is 2x2. Since we are passing in 200 for width and 100 for height, it scales the 2x2 square by 100 on the x axis and 50 on the y axis, resulting in a 200x100 rectangle.
The language allows for two or more functions to be defined with the same name. For instance, say we had two shape
functions, one which renders a square and the other a circle. Which shape would be rendered?
The answer is that, each time the function is called, there is a 50% chance it will render a square and a 50% chance it will render a circle. This allows generated images to have random outputs!
Try running this code multiple times. You will see that it randomly chooses between two different image outputs each time it is ran.
Let's say we want to change things up a bit. We want the code to also render a triangle a third of the time. We can simply add another function definition.
Now, the square will be rendered â…“ of the time, the circle â…“ of the time, and the triangle the other â…“ of the time.
There's one additional feature to function randomness: weights. Weights allow us to define exactly how often we want certain outputs to be selected.
Say, for instance, we want the square to be rendered ½ the time, the circle ¼ the time, and the triangle ¼ the time.
We can use the @
symbol to define custom weights like so. Since 2 + 1 + 1 = 4
, the square will be rendered 2 out of 4 times, the circle 1 out of 4, and the triangle 1 out of 4.
Since the default weight for a function is already one, we don't need to define the weights for the second and third shape definitions.
We can also make this code more flexible by passing in a size
argument to scale each shape by.
Thus, our refactored code will be the following:
Now every 100 times the function is called, about 50 will be squares, about 25 will be circles, and about 25 will be triangles, on average.
This will generate 100 shapes, calling a random version of the shape
function each time.
As you can see, the output is different each time the program is ran. This allows for an incredible number of possible outputs (~5.15×1047)!
We can demonstrate this with a more advanced use case by arranging the shapes on a 10x10 grid across the image. For more information, check out the and sections.