user11296360
user11296360

Reputation:

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!

Upvotes: 0

Views: 664

Answers (4)

sohel14_cse_ju
sohel14_cse_ju

Reputation: 2521

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

Upvotes: 0

Vlad Feinstein
Vlad Feinstein

Reputation: 11311

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

Upvotes: 1

Peter Lee
Peter Lee

Reputation: 136

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..

Upvotes: 0

&#214;&#246; Tiib
&#214;&#246; Tiib

Reputation: 10979

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

Upvotes: 1

Related Questions