For starters, don't worry about 'SUBS' and only focus on 'FUNCTIONS'.
'FUNCTIONS' can do everything a 'SUB' can do, but can optionally accept parameters.
You create a new 'FUNCTION' when you see a pattern in your code, and you figure you can shorten it (or simplify it) by moving some common functionality out of your current code block into a separate block that will have a particular purpose.
Consider this:
// Main Loop
WHILE running = TRUE
// Rotate player '10' degress
player_angle = player_angle + 10.0
// Make sure new player angle is within the range of (0 to 359.99999)
IF player_angle < 0.0 then player_angle = player_angle + 360.0
IF player_angle >= 360.0 then player_angle = player_angle - 360.0
// Rotate enemy '-20' degrees
enemy_angle = enemy_angle - 20.0
// Make sure new enemy angle is within the range of (0 to 359.99999)
IF enemy_angle < 0.0 then enemy_angle = enemy_angle + 360.0
IF enemy_angle >= 360.0 then enemy_angle = enemy_angle - 360.0
WEND
END
You then notice that for both the player and the enemy angle, you want to ensure that the new angles are within a certain range. Both checks look very similar, so you decide to create a 'FUNCTION' to do the range checking for ANY angle, such as:
// Main Loop
WHILE running = TRUE
player_angle = player_angle + 10.0
player_angle = Angle_Clamp(player_angle)
enemy_angle = enemy_angle - 20.0
enemy_angle = Angle_Clamp(enemy_angle)
WEND
END
FUNCTION Angle_Clamp: angle
// Ensure 'angle' is not below 0.0
WHILE angle < 0.0
INC angle, 360.0
WEND
// Ensure 'angle' is below 360.0
WHILE angle >= 360.0
DEC angle, 360.0
WEND
RETURN angle
ENDFUNCTION
The function accepts as a parameter the angle you want to clamp, and returns the new clamped value.
Now, your main loop is much cleaner and easier to read.
And, you now have a useful function to use from now on anytime you want to ensure an angle is in a valid range.