// Example with functions // These #include statements also declare information at "file scope" #include #include #include // create global variable int number1 = 10; // function declaration or function prototype // this declaration has "file scope" which is often // the same as being a "global" identifier // function definition - include the code for the function // calculate the root of the polyinomial equation ax^2+bx+c = 0 // // root1 = (-b + sqrt(b^2 - 4ac))/2a // // return a value of zero for an imaginary root float getRoot1(int a, int b, int c) { cout << "Number1: " << number1 << endl; float t1, t2; t1 = pow(b, 2) - 4*a*c; // check for imaginary root if (t1 < 0) { cout << "Imaginary root" << endl; t2 = 0; } else { t2 = (-b + sqrt(t1)) / 2*a; } return t2; } int main() { float getRoot2(int, int, int); int c1, c2, c3; float root1, root2; int number1 = 7; cout << "Number1: " << number1 << endl; cout << "Starting Main" << endl; cout << "Enter in the 3 coeffiecients: "; cin >> c1 >> c2 >> c3; // function call root1 = getRoot1(c1, c2, c3); root2 = getRoot2(c1, c2, c3); cout << "The first root is: " << root1 << endl; cout << "The second root is: " << root2 << endl; cout << "Ending Main" << endl; // send the value of zero back the Operating System return 0; // The value of zero indicates normal execution of the program // The program "terminated normally" // Another function that is that same as "return 0;" is "exit(int);" exit(0); // exit() can be used at any location in your program to stop // execution. // "return" is a C++ operator; while "exit()" is a library function } // function definition - include the code for the function // calculate the root of the polyinomial equation ax^2+bx+c = 0 // // root2 = (-b - sqrt(b^2 - 4ac))/2a // // return a value of zero for an imaginary root float getRoot2(int a, int b, int c) { float t1, t2; t1 = pow(b, 2) - 4*a*c; // check for imaginary root if (t1 < 0) { cout << "Imaginary root" << endl; t2 = 0; } else { t2 = (-b - sqrt(t1)) / 2*a; } return t2; }