Howtouseavariablefromotherfunctions
int calculateX()
{
int x;
cout<<"Enter value for x: ";
cin>>x;
return x;
}
int anotherFunction()
{
/////
}
int main()
{
calculateX();
anotherFunction();
return 0;
}
This is an example on a code. How do can I possibly use the input from the user in the calculateX() function into the function anotherFunction() ?
回答
You have to use function parameters:
int calculateX()
{
int x;
cout<<"Enter value for x: ";
cin>>x;
return x;
}
int anotherFunction(int x)
{
/////
}
int main()
{
int x = calculateX();
anotherFunction(x);
return 0;
}