Exercise 4.1.2. Write a function named print_out that prints all the whole numbers
from 1 to N. Test the function by placing it in a program that passes a number n
to print_out, where this number is entered from the keyboard. The print_out
function should have type void; it does not return a value. The function can be
called with a simple statement:
print_out(n);
Example
I have no clue were to begin. I know how to declare a function. But I don't understand how to make it prints out the numbers from 1 to N. I also don't know how to call it. Any help? ty I tried google etc
Mainly, you can take Example 4.1 - Triangle-Number Function, and change the function of int triangle(int n) to void print_out(int n) In the for loop, just use cout << i << endl;
I have a first printing edition. Okay, here is a small program, that uses a function, with the number requested sent to it. All you have to do is create the for loop in the function to print out the numbers 1 to whatever the user typed as the input. I can't add anymore to the program without doing the whole thing for you, and then you wouldn't have really learned anything.
#include <iostream>
usingnamespace std;
void function(int);
int main()
{
int result;
cout << "Enter your number: ";
cin >> result;
function( result); // The number entered being sent to the function
cout << "Program now ends.." << endl << endl;
return 0;
}
void function( int num) // Function understands number entered as the variable num
{
cout << "Now make a for loop to 'cout' the numbers from 1 to " << num;
cout << "\n\nand your program is done!\n\n";
// when this finishes being printed to screen, control is sent back to main()
}