When writing large programs, you may run into two problems:
1) the program may turn out to be too large and incomprehensible;
2) you will have to use the same piece of code in different places of the program.
Real programmers break their program into small pieces - subroutines (procedures). Each subroutine solves a small task.
In general terms, the description of the subroutine is as follows:
def name():
commands
Procedure commands are indented. A procedure can contain any constructs within itself.
The name of the procedure (
name
) can be anything, consisting of Latin characters and numbers, but must begin with a letter or underscore (_) and not contain a space.
If you want to execute all the commands that a procedure contains, then simply specify the name of this procedure in the program with parentheses. For example, for the name() procedure described above, the call would look like this.
defname(): # First, we describe the procedure. This must be done before the main program
commands
name() # At this point, specifying the name of the procedure, the commands described in the procedure above will be executed.
down
down
name() # commands from the procedure will be executed here again.
In fact, by creating a procedure, we teach the robot to execute new commands that are not included in its command system!