Creating and Using More Complicated functions
The functions with which you worked in the previous topic were pretty simple. They did a simple output process, and yet even though it was simple, it improved the quality of the program, it improved the readability of the program, and it reduced the potential for program errors. You will next consider a function that actually does some work for your program, and returns a result, but before you do that, consider the following.
The factory metaphor. You can think of a function as "factory" that does things for you. If you are a car manufacturer, you can call upon a tire manufacturer to send you tires. Their tire factory will create tires, but it needs to know how many tires to create. You order five tires for your new car, and the factory gives you five new tires.
However, sometimes you need to do a little more processing than just creating or displaying some number of things. Sometimes your function factory might need to take in some information, process it for you, and give it back to you in a new form. For example, say that your program needed two integers multiplied together so you could get the result, which is the mathematical product of the two. In this case, you need a place to put the necessary information in, and a place to get the necessary result out.
The question is, what is it that needs to go in and/or out? This leads to the developmental questions, which are: “What is the same?” and “What is different?” In the case of a multiplier function, the multiplication process will always occur; that remains the same. However, the two numbers will vary with each use of the process; these will be different. When we can identify the things that will be different for each use of the function then we will have identified what the function needs to know to do its job, as well as what it doesn’t need to know. Since we have identified the two numbers as the “different” part, we will know to provide those values to the functions as parameters. You have seen these previously but now you can see how the design process needs to be applied to identify them.
The function's name will be multiplier and it will accept two parameters and provide one function output result, as shown here.
// define variables
int value_1, value_2, product;
// assign the values
value_1 = 5;
value_2 = 4;
// conduct the process
product = multiplier( value_1, value_2 );
// output the results
conIO.printString( "The result is: " + product );
conIO.printEndline();
You should be able to read this code segment (and all code segments) like a book. Some values (i.e.,value_1 and value_2) are declared and initialized; a multiplication process is conducted on the two values, and the results of that multiplication are assigned to another variable called product; and finally, the result is output with some leading text so the user can see (and understand) the result. Note that this process uses some of the input/output functions you were provided in earlier topics.
Here is what the process might look like at a function factory that is generalized for a variety of inputs and outputs.
Here is what the process might look like specifically at the multiplier function factory.
As long as you think of the function as a little factory with data (i.e., necessary information) coming in the right, and the results going out the left, you will be able to use functions just fine. As you recall from the printing functions, some functions do not output things to the left; they just take some kind of action in the program without creating a processed result. Java allows this with all functions, but as you noticed above, when functions are actually designed not to output anything, they are called void functions, and in general, these are called procedures.
The functions that were used to output asterisks and spaces were void functions. functions can return any individual data type that you want them to, but note that functions can only return one thing. This is an important thing to remember.
Watch this video to extend your understanding of the concept of functions and functional abstraction.