A downloadable game

Download NowName your own price

The draw_lightning function is a recursive function. Recursion is a programming technique in which a function calls itself within its own body.

In the case of the draw_lightning function, it calls itself for two smaller line segments if displace is greater than or equal to detail. Each such call divides the line segment into two smaller segments. This process is continued recursively until displace becomes less than detail. Then the function draws a line between the given points using draw_line.

Recursion is a powerful tool in programming that allows for an elegant solution to many problems that can be divided into smaller, similar problems. However, it should be used with caution, as improper use of recursion can lead to problems such as stack overflow.

"Stack Overflow", is an error that occurs in computer programs when the stack, a data structure used to store information about function calls, exceeds its maximum capacity.

The stack is used to store information about function calls, such as local variables and return addresses.  Each function call adds a new frame to the stack, and the end of the function removes this frame. If the program  calls too many functions without ending them (for example, as a result of recursion that never ends), the stack can exceed its maximum capacity and cause a stack overflow error.

In such a case, the program usually terminates with an error. In some operating systems, it may also cause a memory protection error, as the program tries to write data beyond the memory space allocated for the stack.

The draw_lightning function draws a lightning bolt. If displace is less than detail, it draws a straight line from the starting point to the end point. Otherwise, it calculates the midpoint between the starting and ending points, adds a random displacement to it, and then recursively draws two new lightning bolts: one from the starting point to the midpoint, and another from the end point to the midpoint. The displacement for each new lightning bolt is half the displacement of the previous lightning bolt, which makes the new lightning bolts increasingly detailed.

Download

Download NowName your own price

Click download now to get access to the following files:

soyerdev - Thunder generator-a recursive function.yyz 16 kB

Leave a comment

Log in with itch.io to leave a comment.