Exercise 4.1.2 (C++ without fear)

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
Last edited on
declare the function . define the function and call the function in the main () .
Can somebody please do the exercise for me? I've read over functions but still not really good with it.
@Noob1337

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;
Whitenite1, I checked Example 4.1, it was not Triangle Number Function :\ - I'm using the 2nd edition if that helps
Last edited on
@Noob1337

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>

using namespace 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()
}
Last edited on
Thank you soo much! That was the most helpfull response I've gotten yet. Much appreciated :)
Topic archived. No new replies allowed.