Easier, Faster, Funner
Simplify, Simplify, Simplify. You have now seen the creation of a couple of programs that follow your commands. You have seen simple text pictures drawn by a program, and this was done with only three functions. Things look pretty good so far. However, one thing that was kind of a problem was the fact that in order to accomplish these tasks you would need to copy and paste all those functions like printAsterisk and printSpace repeatedly in order to add more asterisks or spaces to your program. Wouldn't it be easier if you could reduce the number of lines of code and the amount of text manipulation that you have to implement?
Answer: Of course. Especially if someone wants you to start making other shapes likes diamonds, rectangles, and octagons.
What are we improving? To create more complicated shapes, you could continue to use the tools you have. Using the tools you have seen up to this point would allow you to implement all the tasks with which you might be challenged. However, in Computer Science, the goal is not always just to solve a problem or create a program. In fact, there are cases where just solving the problem or creating the solution is simply not good enough. As you have read before, program code that is not easy to read and/or easy to maintain and improve, is never good enough. You heard it here again.
You want to create programs that are easy to develop, easy to read, and easy to maintain and improve. Using parameters in the kinds of programs you have been working on is once again going to reduce the number of required functions for each action, and it is going to make the code easier to understand. For example, you could type printAsterisk(); and then copy and paste it several times to accomplish the task of printing several asterisks. But wouldn't it be easier to just tell the computer "Print three asterisks", or "Print seven asterisks", etc? Wouldn't it be easier to use only one function that prints a number of asterisks specified by you, the programmer? This concept, discussed briefly in the previous chapter, is what you will look at next. Two changes will be required: 1) you will need a new function since the previous functions conduct a specific activity, and this cannot be changed without changing the function itself, and 2) you will be using parameters you observed previously in the new function.
What's a parameter? A parameter provides information to a function that is needed for the function to do its work. For example, if you have hired a car painter, you will need to tell him which car to paint, and which color it needs to be painted. A function is just like a car painter, or any other service person for that matter, in that it will consistently and competently take care of its dedicated task, but there are times it will need to know some things to accomplish that.
In the case of the printAsterisk function, its task was to print an asterisk and it did not need any further information to conduct its task. So, to print five asterisks, the printAsterisk function would be used as shown here.
printAsterisk();
printAsterisk();
printAsterisk();
printAsterisk();
printAsterisk();
However, if another function called printAsterisks (notice the plural word) was called, you would expect it to print more than one asterisk, simply by reading the name of the function. Side note: this is the definition of good self-documentation. However, you have to ask the question, "How can it know how many asterisks to print?", and you will have asked a great question. The answer is that you will pass it a parameter that lets it know, as shown here.
printAsterisks( 5 );
That was easy. Any time you need to use -- and later on to create -- a function, you will need to think about what the function will do if left to its own devices, and what information you will need to provide to it. Providing information to a function is called "passing" the information to it, and since the values within the parentheses are called parameters, it is said that you are "passing one or more parameters".
First side note. Since a copy of the value is passed in the parameter list, we say that this is a copy parameter, or that the action was "pass by copy". This is one of the ways C programs pass parameters and we will need to further discuss this later.
Second side note. For this and most educational examples, it was more clear to use a constant value (i.e., "5"), also called a literal value. However, even though literals may be used initially, virtually all functions should be called with variable parameters for clarity and readability of the code. The following would be the appropriate way to call this function:
// Given that "numberOfAsterisks" represents the value 5,
// We can call the function as follows:
printAsterisks( numberOfAsterisks );
Watch this video and then write your own rectangle-making function with the functions that use parameters. You will again need the asterisks.h file from Chapter 1c.
Watch this video and then write your own triangle-making function with the functions that use parameters.
Now write your own program that creates a diamond using these new functions. You can use numbers as parameters if you wish, or you can use variables as you saw in the videos; remember that you will have to change the number in the variable for each different length line of spaces or asterisks. Either way, this is not difficult but your practice with writing these small programs is very important.
Finally write your own program that creates an octagon using these new functions. Again, it is up to you whether you use numbers or variables in your function parameters. The important thing is to keep practicing with program writing.