need a quick pointer.

I'm trying to teach myself c++ and am a little shaky on how functions work. I made this block of code to experiment what the "()" part of the functionName (); is for but i keep getting an error code on line 20 (c2679)

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

using namespace std;

int main()
{
	int x;
	int test();


	cout << x << endl;

	system("PAUSE");
	return 0;
}

int test()
{
	int x;
	cin >> x >> "\n";
	return x;
}
Your error is on Line 21. The >> "\n" part doesn't make any sense, just delete it. Also your main function wasn't told to expect a function named 'test', so you'll want to either move the definition to someplace above main or you'll want to add a forward declaration. And finally you never save to value returned from 'test()' to 'x', you should change Line 9 to x = test();.
okay, i got it to work though im a bit confused. im trying to pass int x through the int test function. as i understand it, i put it inside the "()" of the function but its not doing what i want. this trial and error learning is bugging the fuck out of me.

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

using namespace std;

int test(int x)
{

	cin >> x;
	return x;
}

int main()
{

	int x;
	test(int x);


	cout << x << endl;

	system("PAUSE");
	return 0;
}


what wrong here? it says type name isn't allowed.
When you have "int" next to the variable name, that means you're trying to declare a variable. You do this correctly on line 16.
But on line, 17, x is now an already-existing variable, so you should just pass x.

1
2
int x = 0;
test(x);


But all of that aside, please realise that there is no point in even passing the variable x into your test function, actually, because it's immediately overwritten by the cin >> x;

I would suggest looking up the term "scope" for C++. The variable called x inside of your test function is local to that function. In other words, the x declared in main() has nothing to do with the x declared in test().

Since you are returning a value from your test function, you should have a way to save that variable.

Try this instead:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;
int test()
{
	int x;
	cin >> x;
	return x;
}

int main()
{
	int x;
	x = test();
	cout << x << endl;

	system("PAUSE");
	return 0;
}

Let me know if I can make anything I said clearer.
Last edited on
Maybe emphasis that it is a local variable used to get the value which is then returned to the value in the main() by not using the same label x?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;
int test()
{
	int s;  // local variable
	cout << "Enter a number ";
	cin >> s;
	return s;   // return the value in s
}

int main()
{
	int x;
	x = test();  // place the returned value into x
	cout << "You entered " << x << endl;

	//system("PAUSE");
	return 0;
}
sorry, this code was probably really confusing as far as purpose wise. After some tutorials, i was really confusing what is supposed to be put in the parenthesis of functions so i was devising a simple code to let me play with it and get a firmer grasp on it but in the course of doing so, couldn't even produce the code i wanted. I think I figured it out though.

TL:DR; this wasn't a code block for practical use. :P
Topic archived. No new replies allowed.