At the beginning of our experience in cooking, we usually don’t start with difficult or too complex recipes, instead, we start with some very simple ones that help us to gather experience and knowledge in the art of cooking…
Let’s suppose by just a moment that, due to our great and long experience in the kitchen, we are hired to cook a new dish: Turkey breast in plum sauce, the recipe is the following:
As we can see, there is a step that is not well explained, and it is the preparation of the plum sauce, it only tells us that we have to do it, but not how, it must have a recipe by itself.
We could include the plum sauce’s recipe within our new recipe, but this can make it very complicated and difficult to follow. Instead, we can use the plum’s sauce recipe separated and, once finished, include it in the rest of the process.
The previous example is very similar to a concept used in programming, known as subprograms, which are blocks of instructions “separated” from the main program, that allows us to execute small parts of the complete process.
Subprograms work the same way a recipe does within another: allow us to obtain some pieces of processing that are necessary to our entire “recipe”. So that we can make use of them at any moment during the process of our program.
- Have a name that identifies them
- Can receive zero or more initial values that are known as parameters (or arguments)
- Can return final data or not
To define a subprogram we must use the following syntax:
And to use it within our programs, we do it the following way:
Let’s suppose for a moment that we must make a program that instructs a robot in how to cross a street that has traffic lights, buy a coffee in the coffee shop and cross the street back to the start point.
A very simple way to do this would be writing a single algorithm with all the steps that are necessary in order to accomplish the task, and that way our program would be something similar to this:
However, we can notice that crossing a street process is twice in our algorithm, it could be much better to write that process in a separate subprogram and just make use of it by calling it from our main program.
Even more, we could write a separated algorithm that explains the robot in how to buy a coffee…
In the process of creating a subprogram, it might appear the need of using some variables if we want to store some data while the execution of the subprogram is running.
Those variables, created within a subprogram, can only be used within his scope, and are known as local variables. In the other hand, the variables defined in the main program can be used during the entire program and its subprograms; these are known as global variables
- Subprograms are set of instructions, separated from the main program, that allow us to obtain the execution of some specific functionality
- They are based on the principle known as “Divide and conquer”. It tells us that we can split a big problem into smaller problems, and will get the big solution by solving the small parts of it.
- Subprograms have a name that identifies them, can receive zero or more initial data, known as parameters, and could return final data
- Subprograms with returning data are usually known as functions, and procedures are the ones who don’t return anything
- Variables defined inside a subprogram can be only used within it. They are known as local variables
- Global variables are those that are defined in the main program, they can be used during all the program and its subprograms