How can I make a variable always equal to the result of some calculations?

前端 未结 12 1755
傲寒
傲寒 2021-02-02 05:34

In math, if z = x + y / 2, then z will always change whenever we replace the value of x and y. Can we do that in programming

12条回答
  •  既然无缘
    2021-02-02 05:54

    1. You create a function for that.
    2. You call the function with the appropriate arguments when you need the value.

    int z(int x, int y)
    {
       return (x + y);
    }
    
    
    int x;
    int y;
    
    // This does ot work
    // int z{x + y};
    
    cin >> x;
    cin >> y;
    cout << z(x, y);
    

提交回复
热议问题