Passing function arguments in C++
adib 11/22/2020
In C++ programming all you can do with passing parameters is in three general ways:
1.Pass by value      2.Pass by reference      3.Pass by pointer     
1.Pass by value
The most common way of passing arguments to functions is passing args by their value. That means when you call the function, those args will be replaced with those you've called the function with. This is the only action.
// a function that will do some modification void func(int a, int b, int c) { // modifying arguments that will take no effect a = 1; b = 2; c = 3; } int main() { // 1st argument to pass int var1 = 4; // 2nd argument to pass int var2 = 5; // 3rd argument to pass int var3 = 6; // calling the function func(var1, var2, var3); cout << "Here are variables passed by value to function : " << var1 << " , " << var2 << " , " << var3 << endl; }
2.Pass by Reference
But another way of passing args to functions or in another term, (returning more than one value at the end of the function if needed) at the end of the function is passing by reference.
The best reason to use pass by reference is not the term that if you want to update your values, then use it!; but its that you can return more than one variable at the end of function.because whether values are updated or not, it can be unimportant for you as a developer.and thats why its not the most important reason for passing by reference. in another term you can have multiple results.
// reference passing function void func(int& a, int& b) { // do some modification on variables a += 1; b += 1; } int main() { // 1st argument to pass int var1 = 4; // 2nd argument to pass int var2 = 5; // calling the function func(var1, var2); // results of modifications cout << "Here are variables passed by value to function : " << var1 << " , " << var2 << endl; }
3.Pass by pointer
The last way of doing this hard job(passing args!!) is passing by pointers. The main cause of doing this confusion can be the situation which we could pass the address instead of the content.
When you pass by reference, indeed you are having access to the content; so doing that. But when you have access to the address of those memory blocks( by saying you have access is that its under your control that during runtime which block is which ), or you are more convinient to do so, you pass by pointer. in this way also, you can rely on updating of variables.
More on usage for this type of passing can be found when you work with microcontrollers and microprocessors. Pointers that are defined in function definition, can be of any type. For example if you want to work on a byte of an integer, not all the 4bytes(in our common compilers atleast), you can pass a character pointer. And this is the power of C++ for projects working with extremely defined memory architecture and also those with a neat mapping of memory.
// passing args by pointer void func(int* a, int* b) { *a = 3; *b = 3; } int main() { // 1st reference variable int ref1 = 15; // 2nd reference variable int ref2 = 5; // 1st argument int& var1 = ref1; // 2nd argument int& var2 = ref2; // this is how we call a function that is passed by pointer func(&var1, &var2); // results cout << "Here are variables passed by value to function : " << var1 << " , " << var2 << endl;