/* This program demonstrates * 1. void functions * 2. Global variables * 3. Scope of Global and Local variables * 4. Passing by value * * void functions * Void functions are used mainly for printing purposes, or when we don't need * to send a value back. So, the keyword "void" is used for the return type. * We cannot attach a variable/value to the return statement in a void function * but we can still say "return". This will stop the execution of the function * and go back to the line after the function call. * When we call a void function, it cannot be an R-value or L-value. The function * call should be a line by itself. * * Local variables * Any variables created in the function (including the parameters) are considered * LOCAL to the function. They are created when the function is called, and * destroyed when we return from the function. Variables in main have no idea * that these variables exist and are not affected by any changes made to the * local variables. Similarly, the local variables have no idea that the variables * in main exist. * * Pass by value * When we make a function call in main, we pass parameters into the function. * The parameters in main are called "actual parameters". These can be variables, * constants or literals. * The parameters in the function are called "formal parameters". These have * to be variables or constants. When a function is called, the value of the * actual parameter is copied into the formal parameter. The formal parameters * are variables that are local to the function. They are created when the * function is called and destroyed when we return from the function. This * concept is called "pass by value". * * Global Variables * When we pass by value, the formal parameters are created for the function. * Any changes we make to the formal parameters will not affect the actual * parameters. If we want the changes to be preserved across several function * calls, we can create global variables. * Global variables are declared OUTSIDE any function. They are available from * when they are declared until the end of the program (last line of code). * Any changes made to global variables will be preserved across all the * function calls. * */ #include #include using namespace std; //Function declarations go above main //This function will return the square of a number double readAndSquare(); //This function will print a rectangular block of characters void printBlock(int lines, char p); int num = 15; // global variable //This function takes no parameters and returns nothing void changeNum(); //swap 2 variables' values, using pass by value void swap(int x, int y); int main() { double ans; /* calling the square function - no parameters * function calls can be placed anywhere we place an R-value * Here, the code in the function definition is run once for * eahc call of the readAndSquare function */ ans = (readAndSquare() + readAndSquare() ) / 2; cout<<"The answer is "<>num; cout<<"Enter the print character: "; cin>>printChar; //Calling the printBlock function - no return printBlock(num, printChar); cout<<"In main, local num: "<< num <>a>>b; cout<<"In main:\nBefore swap:\na = "<>val; return val * val; } /* Functions do not always have to return a value * This function will print a square block of the given character, and * once it's done printing, it has fulfilled its purpose. So, it has * nothing to send back to main */ void printBlock(int lines, char p) { for(int i = 0; i