Description
The purpose of this challenge is to write basic functions in a guided manner. The challenge will walk the programmer through writing functions in increasing levels of complexity. Be sure to read the comments in the provided code.
Requirements
Write all your code for the following steps in one file as one program. This program doesn’t really do anything except illustrate various ways to write functions. Each of the items below are independent of each other – they are not steps to accomplishing one program.
- Create a function prototype void say_hello(). Also add its matching function definition.
// Create the function prototype for say_hello() here. code goes here int main() { // Call the function say_hello below code goes here } // Definitions go here void say_hello() { // Write the code below that displays "Hello, functions" code goes here }
- Look at the new say_something function calls in main() below. Examine how they are being called to determine how the prototype and definition should be written. Pay close attention to the number of parameters, and the types of the parameters being passed to help you figure out how to write the function. It may be helpful to check out the Sample Output at the end of the instructions.
// Prototypes from Step 1 will be here already // Add a new prototype here for the new function code goes here for new prototype int main() { // Function call from Step 1 should already be here // Add these new function calls for Step 2 say_something("Peace"); // shows Peace say_something("Love"); // shows Love say_something("Harmony"); // shows Harmony } // Add new function definition for Step 2 below code here - add new definition for Step 2 here // Function definition for Step 1 is here
- Look at the new double_it function calls in main() below. Examine how they are being called to determine how the prototype and definition should be written. Pay close attention to the number of parameters, and the types of the parameters being passed to help you figure out how to write the function. Especially, notice how the function is being called as part of cout (Remember, cout displays values — that should tell you something about the return type of the double_it function). It may be helpful to check out the Sample Output at the end of the instructions.
// Prototypes from Step 1-2 will be here already // Add a new prototype here for the new function code goes here for new prototype int main() { // Function calls from Step 1-2 should already be here // Add this code for Step 3 cout << "Doubled: " << double_it(1000) << endl; // shows 2000 cout << "Doubled: " << double_it(-2) << endl; // shows -4 } // Add new function definition for Step 3 below code here - add new definition for Step 3 here // Function definitions for Step 1-2 are here
- Look at the new add function calls in main() below. Examine how they are being called to determine how the prototype and definition should be written. Pay close attention to the number of parameters, and the types of the parameters being passed to help you figure out how to write the function. Especially, notice how the function is being called as part of cout (Remember, cout displays values — that should tell you something about the return type of the add function). It may be helpful to check out the Sample Output at the end of the instructions.
// Prototypes from Step 1-3 will be here already // Add a new prototype here for the new function code goes here for new prototype int main() { // Function calls from Step 1-3 should already be here // Add this code for Step 4 cout << "Sum: " << add(10, 20, 40) << endl; // shows 70 cout << "Sum: " << add(5, 4, -2) << endl; // shows 7 } // Add new function definition for Step 4 below code here - add new definition for Step 4 here // Function definitions for Step 1-3 are here
- Look at the new sequence function calls in main() below. Examine how they are being called to determine how the prototype and definition should be written. Pay close attention to the number of parameters, and the types of the parameters being passed to help you figure out how to write the function. Especially, notice how the function is NOT being called as part of cout — that should tell you something about the return type of the sequence function. It may be helpful to check out the Sample Output at the end of the instructions.
// Prototypes from Step 1-4 will be here already // Add a new prototype here for the new function code goes here for new prototype int main() { // Function calls from Step 1-4 should already be here // Add this code for Step 5 // shows 20 21 22 23 24 25 separated by spaces on same line sequence(21, 25); // shows 207 208 209 210 separated by spaces on same line sequence(207, 210); } // Add new function definition for Step 5 below code here - add new definition for Step 5 here // Function definitions for Step 1-4 are here
- Look at the new show_date function calls in main() below. Examine how they are being called to determine how the prototype and definition should be written. Pay close attention to the number of parameters, and the types of the parameters being passed to help you figure out how to write the function. Especially, notice how the function is NOT being called as part of cout — that should tell you something about the return type of the show_date function. This function conditionally shows a date formatted as “American” or “International” — IMPORTANT: conditionally. It may be helpful to check out the Sample Output at the end of the instructions.
// Prototypes from Step 1-5 will be here already // Add a new prototype here for the new function code goes here for new prototype int main() { // Function calls from Step 1-5 should already be here // Add this code for Step 6 // Shows an "american" date -- 12/25/2000 show_date(12, 25, 2000, 'a'); // Shows an "international" date -- 25/12/2000 show_date(12, 25, 2000, 'i'); } // Add new function definition for Step 6 below code here - add new definition for Step 6 here // Function definitions for Step 1-5 are here
Sample Interaction / Final Output of Steps 1 – 6
The colored text below are not actually displayed on the screen; they simply serve as guides to indicate which parts are displayed by the corresponding steps as described above.
Hello, functions Step 1 Peace Step 2 Love Step 2 Harmony Step 2 Doubled: 2000 Step 3 Doubled: -4 Step 3 Sum: 70 Step 4 Sum: 7 Step 4 21 22 23 24 25 Step 5 207 208 209 210 Step 5 12/25/2000 Step 6 25/12/2000 Step 6
LEGEND
PROGRAM OUTPUT
USER ENTRY
FROM INPUT
HELPER TEXT – NOT ACTUALLY DISPLAYED ON SCREEN
CATALOG ID: CPP-FUNC0001-GUIDED