/* This program demonstrates * 1. Nested Loops * 2. Deriving loop conditions from patterns * 3. Introduction to functions from the caller perspective * 4. Introduction to functions from the builder perspective * * Deriving loop conditions and formulae in general from examples. * Sometimes, it not obvious how many times a loop should run. * In this case, we can write out a few examples, and derive a formula for * the number of iterations from those examples. * * Functions * Functions are small modules of code. They are considered to be a unit * consisting of a few lines of code that solves a particular problem. * Functions usually serve only one pruprose. For example, we can write a * function to find out if a number is prime, and aother to find the area * of a circle, but we will ideally never combine the two. * * C++ already offers a variety of pre-defined (pre-implemented) functions * through libraries. To use a particular function, we just need to include * the library that contains the function. For example, in this program, we * will use the functions in the math library. * * cmath is a C++ library that contains several functions that will * solve select math problems. * You can look up the cmath reference at cplusplus.com * * User(Programmers, as the "users" of C++) Defined Functions * Programmers can also write their own functions. These are called user-defines * functions. Here, we are the user (for C++), just like we call someone running * our program, the user. * * Functions have the following syntax: * return_type name ( list of arguments) * 1. The name of the function is whatever we are calling this function. Function * names are identifiers, so we should follow the rules and conventions for * naming identifiers, just like we do with variables. * 2. The list of arguments is a comma separated list of variables that are sent * into the function. These are also called parameters. Each of the parameters * requires its own type, even if they are all the same type. * 3. The return_type is the type of data that the function is going to send back * to the place it was called from. * * Writing/Using a function is done in 3 steps. * 1. Declaration/Prototyping: This is the process of telling C++ that we are * going to write a function. The declaration contains the return type, name * and the list of arguments, followed by a semi-colon. The names of the * arguments are optional. Only the types are required at this stage. The * declaration should be placed BEFORE the main function. * * 2. Defining the function: This is the process of actually writing the function. * The definition typically goes AFTER main. While defining the function, we * should match the function name, return type, and the number, order and * types of the parameters with the function declaration. The names of the * parameters do not have to match. * * 3. Function Call: This is where we use the function we just wrote. The function * call is usually in main/some other function. Once again, we have to match * the function name and the number, order and types of the parameters. The * value returned from the function should be stored into a variable of a * compatible type. To call function, we use the name, and give values to * the parameters (using literals, variable or constants). */ #include #include // for the math functions using namespace std; //function declaration or function prototype - placed outside main //This function will find one root of a quadratic equation double findRoot(double a, double b, double c); int main() { int size; cout<<"Enter the letter size: "; cin>>size; /* Print H * For the H, we see some space between teh 2 stems. We have to put the * spaces in there ourselves. The *'s will not be automatically spaced out. * As usual, we complete one row before moving on to the next. * * 0 * * * 1 * * * 2 ***** * 3 * * * 4 * * * * 5/2 => 2. 7/2 => 3. 9/2 =>4 middle row -> size/2 * If size =5, 3 space. IF size=7, 5 space. If size=9, 7 spaces. Number of sapce => size-2 * * middle row -> row full of * * any other row -> *, then size-2 spaces, then * */ cout<<"\nPrinting H\n"; for(int r=0; r>num; cout<<"Enter the exponent: "; cin>>exp; ans1 = sqrt (num); // call the sqrt funtion to get the square root cout<<"The square root of "<>angle; cout<<"S - Sine\nC - Cosine\nT- Tangent\nE- Exit\nEnter your choice: "; cin>>option; switch(option) { case 'S': cout<< sin(angle); break; case 'C': cout<>x2>>x>>one; root = findRoot(x2, x, one); // function call //parameters here - actual parameters cout<<"The larger root is "<