简体   繁体   中英

C++ How works set of function's argument value?

I am learning C++ and I have code:

float x, y;

namespace X {
    void SetMyX(float p) {
        x = p;
    }
    void SetMyY(float p) {
        y = p;
    }
    namespace XY {
        void Set(float p = 0.0f) {
            x = p;
            p = y;
        }
    }
}

int main() {
    X::SetMyX(5.4f);
    std::cout << "x = " << x << " y = " << y << std::endl;
    X::SetMyY(4.1f);
    std::cout << "x = " << x << " y = " << y << std::endl;
    X::XY::Set();
    std::cout << "x = " << x << " y = " << y << std::endl;
    return 0;
}

And output is:

x = 5.4 y = 0
x = 5.4 y = 4.1
x = 0 y = 4.1

And I figure out variable p sets value of x and then y sets value of p ? But, how it is possible? In C++ you can also set value of function's argument or what? It's just weird for me because I thought you can't just "editing" entered value and you must add another variable like result or something like that, or just return sum of two arguments, but change them?

Yes, the argument p is a completely normal variable and you can do everything with it.

This will also work in many other languages like C, Java, Javascript or Python.

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