/* This program demonstrates * 1. Introduction to functions from the caller perspective * 2. Introduction to functions from the callee perspective * * 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() { /* Here, we demonstrate the trigonometric functions. * They take the value of the angle in radians. * We loop on the user choosing from a menu of options * the loop runs, asking for the angle and trigonometric function * every time. */ double angle; char option; do { cout<<"Enter the angle :"; cin>>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 "<