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;    
}


以上是Howtouseavariablefromotherfunctions的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>