/* This program demonstrates * 1. Pass by Reference * 2. Default Arguments * * Pass by Reference * Global variables are one solution when we want to keep the changes made in * a function. However, since global variables are stored in a different place, * they are a significant security risk, which makes global variables a bad * programming practice. * The solution is to use reference variables. A reference variable is just a * new nickname added to the old variable. When we declare a reference variable, * C++ does not allocate space for it. Instead it just attaches a new name to * the old one. So, any changes made to the reference variable affects the * original variable as well. * We can declare a reference variable by prefixing the variable with an '&' * when we declare it. Reference variables cannot be assigned values or expressions, * only other variables. * For example, * int x=5; * int &y = x; //OK, y is a new reference for x * int &z = 5; // Not OK. 5 is not a variable. * int &val = x+5; // NOt Ok. x+5 is not a variable. * int &num = &y; // OK. * * When we use a reference variable as a formal parameter, a new variable is * not created when a function is called. Instead, a reference to the actual * parameter is created. So, any changes made to the formal paramter in the * function is saved when we return from the function. * The concept is called "pass by reference". Passing be reference enables us * to send back multiple pieces of information from a function. * * Default arguments * If we happen to have a function that takes a parameter, but that parameter * happens to have a particular value MOST (if not all) of the time, then we can * make the parameter default. * We make a parameter default by specifying a value for the parameter during * function DECLARATION. * If a function has a default parameter, it can be left out during the function * call. If a value is specified for the parameter, then the given value is used * in the function. If the parameter is left out, then the default value is used. * The default parameter should be at the very end of the parameter list. This * is because we cannot skip over parameters in a function call. For example, * we cannot have a function call like doStuff(par1, par2, , par3); * If we decide to leave out a parameter, it should be at the end. * * A function could have multiple default parameters, but they should all be at * the end of the parameter list. * Also, if a function has multiple default parameters, we can leave out all * of the parameters, or a continuous list of parameters from the end. */ #include #include using namespace std; //swap 2 variables' values, using pass by value void swapVal(int x, int y); //swap 2 variables' values, using pass by reference void swapRef(int &r, int &s); // function with default argument double area(double rad1, double rad2, double pi = 3.14); int main() { int a,b; cout<<"Enter 2 values: "; cin>>a>>b; cout<<"Pass by value:\n"; cout<<"In main:\nBefore swap:\na = "<