Examples: User-Defined Functions
The following examples will all have similar
formatting: there will be a problem statement, an extraction of pertinent
information, and finally the function itself with comments following.
While it is always more beneficial to implement functions interactively,
this is difficult when reading notes. Therefore, I stress that you
read the problem statement, and try the problem yourself. If you
get stuck, take a look at some of the discussion and continue. It
will hurt you in the end to simply read this page in a non-interactive
fashion.
Example 1: square()
Write a function that returns the square
of a given double value.
The statement is quite clear in terms
of the description of the function.
With this information, we can write the
function.
// Version 1
double square(double num) {
double temp = num * num;
return temp;
}
There is actually no need to assign
to a temporary variable and then return the variable. We can simplify
this function a bit:
// Version 2
double square(double num) {
return num * num;
}
In the second version, we utilize the
fact that we can return an expression from a function.
Example 2: maximum()
Write a function that accepts two integers
and returns the maximum value of the two integers.
The statement is clear.
With this information, we can write the
function.
int maximum(int A, int B)
{
if (A > B) return A;
return B;
//By default, if A is not max, then B is.
}
Note that the arguments passed are
separated by commas with each having their data type specified.
Some questions.
What code is executed if the two values
are equal?
How would you change the above function
to return the minimum of the two integers?
Example 3: charToInt()
Write a function that returns the integer
value of a given character.
Again, the statement is quite clear
in terms of the description of the function.
We write the function.
int charToInt(char c) {
return (int)(c); // Only a
cast is required here.
}
This is actually a trivial function,
however, it is a good exercise.
Example 4: power2()
Write a function that returns the power
of two value of a given integer exponent.
Here the statement is a little ambiguous
in terms of the return type.
We write the function. The function
body may be a bit difficult; a loop must be used.
int power2(int exp) {
int count;
int result = 1;
for (count = 0; count < exp;
count++)
result *= 2;
return result; // Only a cast
is required here.
}
The loop body shows that we continually
multiply some result variable by 2. The initialization of the result
variable is very important; this logic will not work unless it is initialized
to 1.
Example 5: printError()
Write a function that prints the message
"ERROR: problem here." to the screen.
This is a perfect opportunity to use
a void function.
The function:
void printError() {
cout << "ERROR: problem
here." << endl;
return;
}
Note that I have included a return
statement although it can be inserted at the end of the function body;
remember it is optional in void functions.