Not able to 'Function'

Hi have been struggling to understand how functions are working.

Have written the following code to try and pass a value to my function to print out Hello that number of times.

It is definitely passing the value to the function as it prints hello out that number of times however something is very wrong as what follows the 'hello's' is not pretty!!

I have a feeling it has something to do with my not having some kind of 'return' after my function code - but I have tried all types of different 'returns' to no avail - none seem to compile ----HELP!

I think I have missed something big - have read a couple of beginner pieces on functions but how to translate them into what i want to do, even the simplest thing seems to elude me.

As ever, any help much appreciated,

Dan.

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
26
27
28
29
30
31
// fun with functions
#include <iostream>
#include <string>

using namespace std;


string say_hello(int times)
	{
	while (times > 0)
	{
	string greeting;
	greeting = "Hello! ";
	cout << greeting;
	times--;
	}
	
	}

int main ()
{
int in;


cout << "Enter the number of times you want it?\n";
cin >> in;
cout << say_hello(in);

return 0;
}
You declare say_hello() to return a string to the calling function, but you don't
actually write a "return" statement in the function, so what it returns is undefined.
Then, on line 27, you print out that undefined garbage.

You want say_hello() to output something to the screen, but not return
something to the calling function, right? They are two different things.
Change the function type from "string" to "void". And then remove the "cin <<" in front of "say_hello(in);".

If you want a string function example, this is one possibility:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>

string say_hello(string name)
{
return("Hello "+name+"!");
}

int main()
{
string name;
std::cout << "Please enter your name:";
getline(std::cin,name);
std::cout << say_hello(name);

return 0;
}
Last edited on
Okay, many thanks between you both i'm starting to get it.
You want say_hello() to output something to the screen, but not return
something to the calling function, right? They are two different things.
This was exactly the point of my confusion 'outputing' something to the console versus 'outputing something from the function back to main.

Also thanks to
hanst99 (16)
since now without needing to 'return' anything back to main, my function needs to be of type void.

Have the following working well now - will try to get to grips with the next exercise variation,

Thanks,

Dan.

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
26
27
28
29
// fun with functions
#include <iostream>
#include <string>

using namespace std;


void say_hello(int times)
	{
	while (times > 0)
	{
	string greeting;
	greeting = "Hello! ";
	cout << greeting;
	times--;
	}
	}

int main ()
{
int in;

cout << "Enter the number of times you want it?\n";
cin >> in;

say_hello(in);

return 0;
}
Topic archived. No new replies allowed.