Function Help

Hello, I am currently practising functions here and am a bit confused onto the output of my program. I am returning string functions here, and as you can see I am just outputting it by using "cout". Anyways, this particular program is not really an assignment, but just practice.

This is what happens:

1. I enter a word for the first part: "Hello, are you you?".

- This gets placed in the variable enter which is "good".
Then we get to the big line on line 17.

We first stumble upon the first function "hello()" which in the function, says "Please enter another word here" and then returns that word to the function "hello()". Let's say I enter earth. Then goes to "hello1("Narn"). Because there is already a word inside here, this gets passed on to the parameter and literally returns the parameter, so it should just display "Narn" then we get to the third part, which is "hello1(enter)" which should just display "good" because again, we entered that for the variable "enter" already so it should just return that to the function "hello1(enter)" and lastly, the "hello()" function which again, requires you to enter another word and will return that word. Let's say I enter Mars.

Anyways.. sorry for that explanation but this is my problem. After I enter all of that, I get the output of: "mars and Narn and good and earth". Why is this? Isn't it supposed to be "earth and Narn and good and mars"? Isn't the code supposed to read from left to right? That's my confusion really. Thank you.


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
32
33
#include <iostream>
#include <string>
using namespace std;

// Function Prototype
string hello(); // Has to return something.
string hello1(string); // Has to return something.

int main()
{
	string enter;
	string hold;
	string hold1;
	cout << "Hello, how are you? ";
	cin >> enter;

	cout << hello() << " and " << hello1("Narn") << " and " << hello1(enter) << " and " << hello() << endl;

	return 0;
}
string hello()
{
	string test;
	cout << "Please enter another word here: ";
	cin >> test;
	return test;
	cout << "This does not display\n";
}
string hello1(string san)
{
	string awesome = san;
	return san;
}
you are trying to do too much at once.
there is no need to put everything on one line.

http://en.cppreference.com/w/cpp/language/eval_order
there is no concept of left-to-right or right-to-left evaluation in C++. This is not to be confused with left-to-right and right-to-left associativity of operators: the expression f1() + f2() + f3() is parsed as (f1() + f2()) + f3() due to left-to-right associativity of operator+, but the function call to f3 may be evaluated first, last, or between f1() or f2() at run time.


let's break your statement
1
2
3
4
5
6
7
8
9
cout
	<< hello()
	<< " and "
	<< hello1("Narn")
	<< " and "
	<< hello1(enter)
	<< " and "
	<< hello()
	<< endl;
you input «earth» and then «mars»
but your output was «mars ... earth» because the `hello()' on line 8 was evaluated before the `hello()' on line 2.
So you first inputted the last word of your sentence.

I'm not sure, so would assume that this is actually undefined behaviour. Try to avoid constructions like cout << some_function_that_calls_cout()
Huh, that's really weird. So really it's just undefined behavior that can't be fixed, I tried using your new code in my program and it still gave me that same output, nothing really changed. According to your reference, I can't do anything about this, correct? This is the breaked up statement, just copying yours. Gave me same output; weird behavior:

"mars and Narn and good and earth"

Continued..

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
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <string>
using namespace std;

// Function Prototype
string hello(); // Has to return something.
string hello1(string); // Has to return something.

int main()
{
	string enter;
	string hold;
	string hold1;
	cout << "Hello, how are you? ";
	cin >> enter;

	cout << hello() << " and " << hello1("Narn") << " and " << hello1(enter) << " and " << hello() << endl; // Same as the bottom statement.
	
	/*cout << // Same as top statement, just broken up.
		hello() 
		<< " and " 
		<< hello1("Narn") 
		<< " and " 
		<< hello1(enter) 
		<< " and " 
		<< hello() 
		<< endl;*/

	return 0;
}
string hello()
{
	string test;
	cout << "Please enter another word here: ";
	cin >> test;
	return test;
	cout << "This does not display\n";
}
string hello1(string san)
{
	string awesome = san;
	return san;
}


I also wanted to test this further in regards to the reference but instead of using string functions, I wanted to use number to make it easier, and you can imagine, the same behavior occurred, that the last function was being used in the first function, and the first function in the last.

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
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <string>
using namespace std;

// Function Prototype
int hello(); // Has to return something.
int hello1(int); // Has to return something.
int hello_test();

int main()
{
	int enter;
	int hold;
	int hold1;
	cout << "Enter the first number please? ";
	cin >> enter;

	cout << hello() << " and " << hello1(20) << " and " << hello1(enter) << " and " << hello_test() << endl;

	return 0;
}
int hello()
{
	int test;
	cout << "Please enter another number here: ";
	cin >> test;
	return test;
	cout << "This does not display\n";
}
int hello1(int san)
{
	int awesome = san;
	return san;
}
int hello_test()
{
	int test;
	cout << "Please enter another word here: ";
	cin >> test;
	return test;
	cout << "This does not display\n";
}


Your solution was right, I shouldn't really use cout here, but I decided to return all the value from the functions to specific variable, and then use the same technique for the strings and this game, it actually gave me the correct output.

"earth and Narn and good and mars"
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
32
33
34
35
36
37
38
39
#include <iostream>
#include <string>
using namespace std;

// Function Prototype
string hello(); // Has to return something.
string hello1(string); // Has to return something.

int main()
{
	string enter;
	string a, b, c, d;
	cout << "Hello, how are you? ";
	cin >> enter;

	a = hello();
	b = hello1("Narn");
	c = hello1(enter);
	d = hello();
	
	// cout << hello() << " and " << hello1("Narn") << " and " << hello1(enter) << " and " << hello() << endl;

	cout << a << " and " << b << " and " << c << " and " << d << endl;

	return 0;
}
string hello()
{
	string test;
	cout << "Please enter another word here: ";
	cin >> test;
	return test;
	cout << "This does not display\n";
}
string hello1(string san)
{
	string awesome = san;
	return san;
}


Topic archived. No new replies allowed.