====== Functions ====== ===== Function Parameters and Arguments ===== **Function parameter** is the variable used in the function header. It works in the same way as a variable defined inside the function. The only difference is that they are **initialized** with the values given in the function call. void doSomething(int x, int y){ std::cout< **Argument** are the values given to the function in the function call. doSomething(5, 6); ==== Invocation by Value ==== When a function is called; * The parameters of the function are created as variables. * The values of the arguments are **copied** into these variables. Calling a function in this way is called **calling by value**. ==== Invocation by Reference ==== If you need to refresh your knowledge about references, [[en:cs:cpp:common:reference|references]] see page. Class types in the standard library are not suitable for calling by value. Because these types are costly to copy. If we use references in the function parameter, these references are bound to the appropriate arguments when the function is called. No new object is created. void printValue(std::string& y) // type changed to std::string& { std::cout << y << '\n'; } // y was destroyed here int main() { std::string x { "Hello, world!" }; printValue(x); // x is now passed to the reference parameter y as reference (cheap) return 0; } Calling by reference allows us to pass arguments to a function without making copies of those arguments each time the function is called. Calling by reference allows us to operate on the argument. We know that copying takes place when calling by value. Therefore changes inside the function do not affect the argument. In call by reference, the argument itself is passed to the function, so changes made inside the function affect the argument. Of course our reference should not be ''const''. Because we want to modify the argument in the function. void addOne(int& y) // y depends on the real object x { ++y; // this replaces the actual x object } int main() { int x { 5 }; std::cout << "value = " << x << '\n'; addOne(x); std::cout << "value = " << x << '\n'; // x has been changed. return 0; } Taken from [[en:cs:cpp:common:function|UCH Viki]]. https://wiki.ulascemh.com/doku.php?id=en:cs:cpp:common:function