简体   繁体   中英

How to define a variable in a function and access and change it in another function?(c++)

I would like to know how to define a variable in one function and access and change it in another function. For example:

#include <iostream>

void GetX()
{
   double x = 400;
}

void FindY()
{
   double y = x + 12;
}

void PrintXY()
{
   std::cout << x;
   std::cout << y;
}

int main()
{
   GetX();
   FindY();
   PrintXY();
}

How would I access these variables from all the functions? (Obviously for this to work in real life I wouldn't need so many functions, but I think this is a nice simple example). Thanks in advance for your help!

Use function parameters to pass values to functions and return values to return results:

#include <iostream>

double GetX()
{
    return 400;
}

double FindY(double x)
{
    return x + 12;
}

void PrintXY(double x, double y)
{
    std::cout << x;
    std::cout << y;
}

int main()
{
    double x = GetX();
    double y = FindY(x);
    PrintXY(x, y);
}

Since the question was tagged with C++ , here is another option:

#include <iostream>
class Sample
{
public:
    void FindY()
    {
        y = x + 12;
    }

    void PrintXY()
    {
        std::cout << x;
        std::cout << y;
    }
private:
    double x = 400, y;
};

int main()
{
    Sample s;
    s.FindY();
    s.PrintXY();
}

1st, make x, y as static , so that these exist when the function returns.. 2nd, get reference, modify or do something outside the function..

#include <iostream>

double &GetX()
{
   static double x = 400;
   return x;
}

double &FindY( double x )
{
    static double y;
    y = x + 12;
    return y;
}

void PrintXY(double x, double y ) 
{
   std::cout << x;
   std::cout << y;
}

int main()
{
   double &x = GetX();
   double &y = FindY( x );
   // Now you can modify x, y, from Now On..
   // .....

   PrintXY( x, y );
}

By the way, I donot recommend this style of code..

You want to define a variable in one function : That means you are making the variable local to that function.

You want to access and change that local variable from another function. This is not usual. Technically possible but can be done with better resource management/design.

*You can make the variable your class member and play with it.

*You can share a variable by making it global as well.

*In Tricky way :

double &GetX()
{
    static double x = 400;
    return x;
}

// We are accessing x here to modify y
// Then we are modifying x itself
// Pass x by reference
double &AccessAndChangeX(double& x)
{
    static double y;
    y = x + 12; // We are accessing x here and using to modify y.

    // Let's modify x
    x = 100;
    return y;
}

void PrintXY(double x, double y)
{
    std::cout << x;
    std::cout << y;
}

int main()
{
    double &x = GetX(); // Take the initial value of x. 400.
    double &y = AccessAndChangeX(x);

    //Print initial value of x and value of y(generated using x)
    PrintXY(x, y);  

    // X was modified while AccessAndChangeX(x). Check if x was changed!
    std::cout << "\n" << "What is the value of x now : " << GetX();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM