/* This program demonstrates * 1. Pass by Reference * 2. Function Overloading * * 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. * * Function Overloading * While it is not possible to have multiple variables with the same name in the * same scope, it is possible to have multiple functions with the same name * as long as * 1. They have a different number of parameters * 2. If they have the same number of parameters, then the two functions should * have at least 1 parameter of a different (incompatible) type. * 3. If they have the same number and types of parameters, then the ordering * of the parameters should provide a way to distinguish between the functions * If we fail on all 3, then then the compiler is unable to decide which function * is being called. This is called "ambiguous invocation" and it might not be a * syntax error, but it is not guaranteed to call the right function and might * give you the wrong answer. * */ #include #include using namespace std; //swap 2 variables' values, using pass by value void swap(int x, int y); //swap 2 variables' values, using pass by reference void swapRef(int &r, int &s); // We declare 3 functions called "volume" // However, each of them takes a different number of parameters // 1. find the volume of a sphere - pi is a default argument here double volume(double radi, double pi = 3.14); /* In the previous line, we declare the function volume with pi as a default parameter. * If the function call in main passed a value for pi, the passed value is used. * If the function call left out the parameter that corresponds to the parameter * pi, then 3.141 is used as the value of pi. * Please note that pi is the last parameter. */ // 2. find the volume of a cylinder or cone double volume(double radius, double height, double pi, char shape); // 3. find the volume of a 4d rectangular prism double volume( double l, double w, double h, double d4); int main() { int a,b; cout<<"Enter 2 values: "; cin>>a>>b; cout<<"In main:\nBefore swap:\na = "<>radius; cout<<"Enter height: "; cin>>height; // calls the sphere's volume cout<<"Volume (pi = 3.141) "<< volume(radius, 3.141)<