Functions, Continued
One more time (with feeling). The multiplier method will now be created in steps as the printOneLine method was previously.
Create the header. It needs a return type which is an integer in this case. It needs an identifier which is multiplier, and it needs parameters so that it can take in the two values to be multiplied. From the time being, we will leave the static modifier in for our discussions but note that it will not be used much once your programs move over into independent classes. To be clear, as long as you are writing one-file programs where the methods are all written in the same file as the main method, and called within the main method, you will need to place static between public (or private) and int (or other data types). But in most C programming, that will not be happening much.
int multiplier( int value1, int value2 )
Create the body. This is simple and easy.
int multiplier( int value1, int value2 )
{
}
Design the function. Write out the function's operations in pseudocode.
int multiplier( int value1, int value2 )
{
// initialize function/variables
// conduct the multiplication
// return the result
}
Implement the function. Finally, fill in the necessary code. Note there are no functions used inside this function but that will sometimes occur in other functions.
int multiplier( int value1, int value2 )
{
// initialize function/variables
int result;
// conduct the multiplication
result = value1 * value2;
// return the result
return result;
}
Once again, there is not much to doing this, but you will need to practice at it. One more thing that you need to know related to this process is what kind of parameters are used in what locations. When you are formally declaring your function header or you are showing the function implementation, you are formally announcing to the compiler what the parameters, the function identifier, the return types, and the scope are going to be. For this reason, these are called formal parameters. However, when you actually use the function, you do not use any data types in the parameter list; you only use the program variables for the multiplication process; these are called actual parameters. It will be important for you to know when to place data types in your parameter lists and when you don't, so you need to familiarize yourself with these terms.
A brief program structure is shown next that exemplifies these terms, and the correct use of parameters. Only the code critical to the creation and use of the function are shown. The Console IO Utility code is not shown.
// Header files
#include "Console_IO_Utility.h"
// Global constants
// none
// Function prototypes
int multiplier( int firstVal, int secondVal );
// Main Program
int main()
{
// initialize program/variables
int oneValue, otherValue, product;
// assignment process, for variables
oneValue = 5;
otherValue = 7:
// multiplication process
// - the function uses ACTUAL parameters
// which DO NOT SHOW data types
product = multiplier( oneValue, otherValue );
// output process
// some output here
// end program
// display program end
}
// Function Implementation
// the function implementation has FORMAL parameters which DO SHOW data types
// the formal parameters (identifiers) don't have to be the same
// as the actual parameters
// the multiplier and main answer variables also don't need to be the same
int multiplier( int firstVal, int secondVal )
{
// initialize function/variables
int answer;
// multiply values
answer = firstVal * secondVal;
// return the result
return answer;
}
The structure and overview has been presented here. Watch this video to see the complete program developed along with the function.