help me

fuction x(); in main can not declare...plsz help me ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<iostream>
using namespace std;
int z()
{
  int a=1;
}
int x(int a)
{
  cout<<a;
}
int main()
{
  x();
}
Last edited on
Hello phongvants123,

On line 7 you declare the function "x" as taking one parameter, but on line 13 you call the function with no parameter. The function definition and call do not match.

And if you call function "z" the value of "a" will be lost when the function ends. If you would need to use the value stored in "a" in function "z" you will need to do something different.

Hope that helps,

Andy
that's the point...i want i need to use the value stored in "a" in function z...could u recomment the good way for me
beside after refix the code still does not work..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<iostream>
using namespace std;
int z()
{
  int a=1;
}
int x(int a)
{
  cout<<a;
}
int main()
{
  x(int a);
}
Hello phongvants123,

A little better except line 13 is now a function proto type not a function call.

Give this a read http://www.cplusplus.com/doc/tutorial/functions/

This should help you understand that functions "z" and "x" both return an int,, but neither function is returning anything. And i function "z" the value of "a" is lost when the function ends.

You should compile the program and try to fix any errors yo can and post the errors that you do not understand.

When I tried to compile the program here, the gear icon in the top right of the code box, I received warnings about functions "z" and "x" not returning a value.

Give the link a read and see if you can figure it out and we can go from there.

Hope that helps,

Andy
the content and the link which were given by andy are so crystal clear
i really apreciate it
hope to see u again:)) dear andy
Hello phongvants123,

Now that you understand better this should make more sense:

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;  // <--- Best not to use.

void z(int& a)  // <--- Changed to void return type. Passed "a" by reference.
{
	a = 1;
}

void x(int a)  // <--- Changed to void return type.
{
	std::cout << a << std::endl;
}

int main()
{

	int a{ 10 };

	x(a);  // <--- Prints the first value of "a" defined in main.
	z(a);  // <--- Changes the value of "a" defined in main.
	x(a);  // <--- Prints the changed value of "a" defined in main.

	return 0;  // <--- Not needed, but good programming.
}


Hope that helps,

Andy
Topic archived. No new replies allowed.