### Fun with Functions part 3

Write a function titled say_hello() that outputs to the screen "Hello"

★ Modify the function so that it takes an integer argument and says hello a number of times equal to the value passed to it.

★★ Make another function that takes two integers arguments and then returns an integer that is the product of the two integers.
(i.e., integer1: 4, Integer2: 5 returns: 20)

★★★ Make a function called half() that takes an integer argument. The function must print the number it received to the screen, then the program should divide that number by two to make a new number. If the new number is greater than zero the function then calls the function half() passing it the new number as its argument. If the number is zero or less than the function exits

Call the function half() with an argument of 100, the screen output should be
100
50
25
...
...
1.

I'm doing the third part to this excercise. I keep getting a failed response when I debug it. Could someone just take a gander at my function Half()? I'd greatly appreciate anyone's imput! thanks

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061`` ``````// Fun with Functions.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include #include #include using namespace std; void Say_hello(unsigned int x) //one integer argument { for(unsigned int i = 0; i < x; i ++) { cout << "hello\n"; //repeats output of "hello" } } void Say_Hello_Again(unsigned int x, unsigned int y) //two integer argument { for(unsigned int i = 0; i <= x*y; i ++) { cout << "hello\n"; //repeats output of "hello" equal to the product of the two integers } } void half(signed int x) { cin >> x; cout << x << "\n"; x/2; if (x/2 > 0) { half(x); } if(x/2 <= 0) { exit; } } class KeepRunning { public: ~KeepRunning() { system("pause"); Sleep(5); } }; int _tmain(int argc, _TCHAR* argv[]) { KeepRunning kr; half(x); /*Say_hello(50);*/ //Say_Hello_Again(2,50); system("PAUSE"); } ``````
I don't think you want to use exit here. This keyword terminates the process, and all you want to do is return from the function.

if(x/2 <= 0)
{
return;
}
It underlines the `half(x);` in red, and says it's undefined. I can't give it a value yet because the user is supposed to do that.
glancing quickly at this, I'd rather see something more like ...

int half(int x) // returns the integer value of x
{
cin >> x;
cout << x << "\n";
if (x/2 > 0)
return x/2;
else return x;
}

Well, just a thought.
You may wish to set x to 0 before returning if <0, however.
Last edited on
 ``12345678910111213141516171819202122232425`` ``````#include void half(int n); void half(int n) { std::cout<>n; std::cout<
ok! thanks for all the imput so far!

@usandfriends

`if(n!=0)` The excercise wants to call the function half() if "n" is greater than 0 so I think it should be `if(n > 0)`?

Can I have the
 ``12`` ``````std::cout<<"Number to keep cutting in half? "; std::cin>>n;``````

within the function? The excercise says "the function must print the number it receives". Does that mean the user imputs a number and it reprints it?

One last question, when I call half() within the 'if' statement, shouldn't it ask me what number I want to imput again? Because it should start at the beginning of the function... I guess if you put the question inside the function like I did it would... but when I run the program it just repeats the number I imput
Nevermind about those questions! thanks to all who helped! You all were very helpful. Happy New years! I got it to work yay
Topic archived. No new replies allowed.