03 - Functions and Procedures

03 - Functions and Procedures - Engineering 101 Quote of...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Engineering 101 Quote of the Day Even when walking in the company of two others, I am bound to be able to learn from them. The good points of the one I copy; the bad points of the other I correct in myself. - Confucius Functions and Procedures Your Weight on Mars Your Weight is the force exerted on you The Universal Law of Gravitation States Your Weight on Mars So the ratio of your weight on another planet to your weight on earth is GmM W= R2 Your weight on earth is m=your mass R=radius M=planet mass G=6.672x10 -11 W GM = = 6.8 10 -12 2 WE gR ( m2 s M )R 2 So if I know the mass of the planet, M, and the radius of the planet, R, I can find the change in my weight. WE = mg N m2/kg2 What do we want the program to do? What is your weight on earth? 165 Weight on Mars is 62.5 Start with the scaffolding #include <iostream> using namespace std; int main( ){ return 0; } 1 Add the Obvious Expresssions #include <iostream> using namespace std; int main( ){ Make sure to add declarations #include <iostream> <iostream> using namespace std; int main( ){ double marsmass = 6.4e23; 6.4e23; double marsrad = 3.4e6; double earthweight; earthweight; double marsratio; marsratio; cout << "What is your weight on earth?" << endl; endl; cin >> earthweight; earthweight; cout << "What is your weight on earth?" << endl; cin >> earthweight; cout << "Weight on mars is " << marsratio*earthweight << endl; return 0; } } cout << "Weight on mars is " << marsratio*earthweight << marsratio* endl; endl; return 0; Add in the equation #include <iostream> <iostream> using namespace std; int main( ){ double marsmass = 6.4e23; 6.4e23; double marsrad = 3.4e6; double earthweight; earthweight; double marsratio; marsratio; cout << "What is your weight on earth?" << endl; endl; cin >> earthweight; earthweight; marsratio = 6.8e-12*marsmass/(marsrad*marsrad); 6.8e-12*marsmass/(marsrad*marsrad); cout << "Weight on mars is " << marsratio*earthweight << marsratio* endl; endl; return 0; } Add comments for the reader #include <iostream> <iostream> // Program written by someone using namespace std; // This computes your weight int main( ){ // if you happen to be on Mars double marsmass = 6.4e23; 6.4e23; // The mass of Mars double marsrad = 3.4e6; // The radius of Mars double earthweight; earthweight; // The weight on Earth double marsratio; marsratio; // Ratio of Mars to Earth cout << "What is your weight on earth?" << endl; endl; cin >> earthweight; earthweight; // compute the weight of the person on Mars marsratio = 6.8e-12*marsmass/(marsrad*marsrad); 6.8e-12*marsmass/(marsrad*marsrad); cout << "Weight on mars is " << marsratio*earthweight << marsratio* endl; endl; return 0; } What if... ...you wanted to add in your weight on Jupiter too? #include <iostream> using namespace std; int main( ){ double marsmass = 6.4e23; double marsrad = 3.4e6; double jupmass = 1.9e27; double juprad = 7.0e7; double earthweight; double marsratio, jupratio; // Program written by someone // This computes your weight // if you happen to be on Mars or Jupiter // The mass of Mars // The radius of Mars // The mass of Jupiter // The radius of Jupiter // The weight on Earth // Ratio of Mars and Jupiter to Earth cout << "What is your weight on Earth?" << endl; cin >> earthweight; // compute the weight of the person on Mars marsratio = 6.8e-12*marsmass/(marsrad*marsrad); 6.8ecout << "Weight on Mars is " << marsratio*earthweight << endl; // compute the weight of the person on Jupiter jupratio = 6.8e-12*jupmass/(juprad*juprad); 6.8ecout << "Weight on Jupiter is " << jupratio*earthweight << endl; return 0; } 2 Avoid Repetition in Programs Repetition leads to errors because there are more opportunities for typos. Repetition makes debugging more difficult because mistakes are repeated. Future users find codes that involve repetition harder to read and harder to understand. understand. Functions and Procedures Functions and procedures encapsulate algorithms so they can be written once and used repeatedly. Also called routines or subroutines. subroutines. Functions versus Procedures Functions return data directly to the caller and take as input copies of data from the caller (call(callby-value) byProcedures and commands do not directly return any value, but operate on data in the caller (call-by-reference). (call-by- Declaring Functions Functions can either be pre-defined library prefunctions or user-defined functions. userfunctions. A function declaration is of the form: T identifier (formal_parameter_list); T is a type that describes the data returned. The formal parameter list is a list of types and identifiers which enumerate the input data and the order in which that data is expected. For example: double x, int y, double z Calling Functions A function call is of the form: f (expression_list); f is a function identifier. The expression list is a list of expressions separated by commas. Each is evaluated and passed to the function as input. The function evaluates to a single data object of a type determined in its declaration. Library Functions and Procedures Very common functions and procedures are often available in libraries. libraries. Libraries are included at the beginning of a program using a preprocessor directive #include <library_name> <library_name> 3 Library Functions and Procedures Libraries described in Appendix B cmath cstdlib cctype iostream fstream string vector Standard math library C standard library (rand) C character types Standard I/O File Streams Character strings Vectors cmath Library If you include the cmath library you can then do important things like double sqrt double abs double sin double floor double ceil double log double pow output (double x); square root (double x); absolute value (double x); sine of x in radians (double x); round down (double x); round up (double x); find the natural log of x (double x, double y); xy input Example Using cmath Library #include <cmath> #include <iostream> using namespace std; int main(){ double x, y; cout << "Input two sides:" << endl; cin >> x >> y; cout << "The hypotenuse is:"; cout << sqrt(x*x + y*y) << endl; return 0; } What does this expression do? log base10 floor( x / pow (10, floor( log10(x)))) 1- returns the last non-zero digit in a number non2- returns the first non-zero digit in a number non3- rounds a number to 2 significant digits 4- rounds a number to 1 significant digit This one is hard... Remember this? How can we use functions to make it simpler? Defining Functions UserUser-defined functions must be defined before they are used. A function definition is of the form: T identifier (formal_parameter_list) compound statement The compound statement is contained in {braces} and is executed whenever the function is called. The definition must have one or more return statements that evaluate to type T. #include <iostream> using namespace std; int main( ){ double marsmass = 6.4e23; double marsrad = 3.4e6; double jupmass = 1.9e27; double juprad = 7.0e7; double earthweight; double marsratio, jupratio; // Program written by someone // This computes your weight // if you happen to be on Mars or Jupiter // The mass of Mars // The radius of Mars // The mass of Jupiter // The radius of Jupiter // The weight on Earth // Ratio of Mars and Jupiter to Earth cout << "What is your weight on Earth?" << endl; cin >> earthweight; // compute the weight of the person on Mars marsratio = 6.8e-12*marsmass/(marsrad*marsrad); 6.8ecout << "Weight on Mars is " << marsratio*earthweight << endl; // compute the weight of the person on Jupiter jupratio = 6.8e-12*jupmass/(juprad*juprad); 6.8ecout << "Weight on Jupiter is " << jupratio*earthweight << endl; return 0; } 4 UserUser-Defined Functions Simplify Code marsratio = 6.8e-12 * marsmass / ( marsrad * marsrad ); 6.8ecout << "Weight on Mars is " << marsratio*earthweight<< endl; UserUser-Defined Functions Simplify Code marsratio = 6.8e-12 * marsmass / ( marsrad * marsrad ); 6.8ecout << "Weight on Mars is " << marsratio*earthweight<< endl; We can write a function called newweight that takes as input the weight of the person on earth, the mass and radius of the planet and returns as output the weight on the new planet. newweight( ) UserUser-Defined Functions Simplify Code marsratio = 6.8e-12 * marsmass / ( marsrad * marsrad ); 6.8ecout << "Weight on Mars is " << marsratio*earthweight<< endl; We can write a function called newweight that takes as input the weight of the person on earth, the mass and radius of the planet and returns as output the weight on the new planet. newweight( double ew ) UserUser-Defined Functions Simplify Code marsratio = 6.8e-12 * marsmass / ( marsrad * marsrad ); 6.8ecout << "Weight on Mars is " << marsratio*earthweight<< endl; We can write a function called newweight that takes as input the weight of the person on earth, the mass and radius of the planet and returns as output the weight on the new planet. newweight( double ew, double pmass, double prad) UserUser-Defined Functions Simplify Code marsratio = 6.8e-12 * marsmass / ( marsrad * marsrad ); 6.8ecout << "Weight on Mars is " << marsratio*earthweight<< endl; We can write a function called newweight that takes as input the weight of the person on earth, the mass and radius of the planet and returns as output the weight on the new planet. planet. double newweight( double ew, double pmass, double prad) UserUser-Defined Functions Simplify Code cout << "Weight on Mars is " << newweight( earthweight, marsmass, marsrad) << endl; Replace the calculation with the function. We can write a function called newweight that takes as input the weight of the person on earth, the mass and radius of the planet and returns as output the weight on the new planet. double newweight( double ew, double pmass, double prad) 5 #include <iostream> using namespace std; int main( ){ double marsmass = 6.4e23; double marsrad = 3.4e6; double jupmass = 1.9e27; double juprad = 7.0e7; double earthweight; double marsratio, jupratio; // Program written by someone // This computes your weight // if you happen to be on Mars or Jupiter // The mass of Mars // The radius of Mars // The mass of Jupiter // The radius of Jupiter // The weight on Earth // Ratio of Mars and Jupiter to Earth #include <iostream> using namespace std; int main( ){ double marsmass = 6.4e23; double marsrad = 3.4e6; double jupmass = 1.9e27; double juprad = 7.0e7; double earthweight; // Program written by someone // This computes your weight // if you happen to be on Mars or Jupiter // The mass of Mars // The radius of Mars // The mass of Jupiter // The radius of Jupiter // The weight on Earth marsratio and jupratio are no longer needed cout << "What is your weight on Earth?" << endl; cin >> earthweight; // compute the weight of the person on Mars cout << "Weight on Mars is " << newweight(earthweight, marsmass, marsrad) << endl; // compute the weight of the person on Jupiter cout << "Weight on Jupiter is " << newweight(earthweight, jupmass, juprad) << endl; return 0; } cout << "What is your weight on Earth?" << endl; cin >> earthweight; // compute the weight of the person on Mars marsratio = 6.8e-12*marsmass/(marsrad*marsrad); 6.8ecout << "Weight on Mars is " << marsratio*earthweight << endl; // compute the weight of the person on Jupiter jupratio = 6.8e-12*jupmass/(juprad*juprad); 6.8ecout << "Weight on Jupiter is " << jupratio*earthweight << endl; return 0; } Writing the Function Definition double newweight( double ew, double pmass, double prad) { Writing the Function Definition double newweight( double ew, double pmass, double prad) { ratio = 6.8e-12 * pmass / ( prad * prad ); 6.8e- } } Braces enclose the body of the function We calculate the ratio inside the function. Writing the Function Definition double newweight( double ew, double pmass, double prad) { double ratio; ratio = 6.8e-12 * pmass / ( prad * prad ); 6.8e} Writing the Function Definition double newweight( double ew, double pmass, double prad) { double ratio; ratio = 6.8e-12 * pmass / ( prad * prad ); 6.8ereturn ew*ratio; } This ratio is only defined inside the function, and so it must be declared in the function. The function returns the ratio times the weight on earth. This is a double. 6 #include <iostream> using namespace std; int main( ){ double marsmass = 6.4e23; double marsrad = 3.4e6; double jupmass = 1.9e27; double juprad = 7.0e7; double earthweight; // Program written by someone // This computes your weight // if you happen to be on Mars or Jupiter // The mass of Mars // The radius of Mars // The mass of Jupiter // The radius of Jupiter // The weight on Earth #include <iostream> // Program written by someone using namespace std; // This computes your weight if you happen to be on Mars or Jupiter // The function newweight takes earth weight, planet mass and planet radius and returns weight on other planet double newweight( double ew, double pmass, double prad) { double ratio; ratio = 6.8e-12 * pmass / ( prad * prad ); 6.8ereturn ew*ratio; } int main( ) { double marsmass = 6.4e23; double marsrad = 3.4e6; double jupmass = 1.9e27; double juprad = 7.0e7; double earthweight; cout << "What is your weight on Earth?" << endl; cin >> earthweight; // compute the weight of the person on Mars cout << "Weight on Mars is " << newweight(earthweight, marsmass, marsrad) << endl; // compute the weight of the person on Jupiter cout << "Weight on Jupiter is " << newweight(earthweight, jupmass, juprad) << endl; return 0; } // The mass of Mars // The radius of Mars // The mass of Jupiter // The radius of Jupiter // The weight on Earth cout << "What is your weight on Earth?" << endl; cin >> earthweight; // compute the weight of the person on Mars cout << "Weight on Mars is " << newweight(earthweight, marsmass, marsrad) << endl; // compute the weight of the person on Jupiter cout << "Weight on Jupiter is " << newweight(earthweight, jupmass, juprad) << endl; return 0; } Functions Provide Structure int main( ){ double A = 1.0, B; double C = 5.0; B = C; B = B + 3; B = B * A; double D = 3.0, E; E = D; E = E + 3; E = E * A; return 0; } Functions Provide Structure int main( ){ double A = 1.0, B; double C = 5.0; B = C; B = B + 3; B = B * A; double D = 3.0, E; E = D; E = E + 3; E = E * A; return 0; } Functions eliminate repetition Functions Provide Structure int main( ){ double A = 1.0, B; double C = 5.0; B = f(C, A); double D = 3.0, E; E = f(D, A); return 0; } Functions eliminate repetition Functions Provide Structure int main( ){ double A = 1.0, B; double C = 5.0; B = f(C, A); double D = 3.0, E; E = f(D, A); return 0; } 7 Functions Provide Structure double f( double X, double Y){ double Z = X; Z = Z + 3; return Z * Y; } int main( ){ double A = 1.0, B; double C = 5.0; B = f(C, A); double D = 3.0, E; E = f(D, A); return 0; } output type Functions Provide Structure double f( double X, double Y){ double Z = X; Z = Z + 3; return Z * Y; } int main( ){ double A = 1.0, B; double C = 5.0; B = f(C, A); double D = 3.0, E; E = f(D, A); return 0; } input types Definition Or Declaration Must Proceed Function Call double f( double X, double Y){ double Z = X; Z = Z + 3; return Z * Y; } int main( ){ double A = 1.0, B; double C = 5.0; B = f(C, A); double D = 3.0, E; E = f(D, A); return 0; } Function Definition Definition Or Declaration Must Proceed Function Call Function double f( double X, double Y); int main( ){ double A = 1.0, B; double C = 5.0; B = f(C, A); double D = 3.0, E; E = f(D, A); return 0; } Declaration Function Calls Function Calls Definition Or Declaration Must Proceed Function Call Function double f( double X, double Y); int main( ){ double A = 1.0, B; double C = 5.0; B = f(C, A); double D = 3.0, E; E = f(D, A); return 0; } double f( double X, double Y){ double Z = X; Z = Z + 3; return Z * Y; } Declaration Definition Or Declaration Must Proceed Function Call double f( double X, double Y); int main( ){ double A = 1.0, B; double C = 5.0; B = f(C, A); double D = 3.0, E; E = f(D, A); return 0; } double f( double X, double Y){ double Z = X; Z = Z + 3; return Z * Y; } Function Calls Function Definition Note that Z is declared in the function definition, definition, not with the function declaration 8 Functions vs. Procedures Functions are like calling a technician to help step you through the procedure to fix your computer. You provide information to the tech. The tech provides guidance, but doesn't change anything in your computer. Functions vs. Procedures Procedures are like dropping your computer off at a computer repair shop. You hand over control of the computer. By the time you get the computer, chips have been swapped, parts have been changed, and the computer is either fixed or damaged beyond hope. Functions vs. Procedures Functions directly return information (the technician talking); procedures do not (you just pick up the machine when done). Functions do not change things in the caller's environment (you have to do all the repair work); procedures can directly affect the caller's environment for better or worse (the computer is fixed or destroyed). Functions Return Information, Procedures Do Not You can declare a procedure that will not return a value by using void instead of an output type void identifier (formal_parameter_list); The definition of the procedure may then have return statements without any associated expression to return. Simplest Procedure void PrintSomething( ){ cout << "Now I am in the procedure" << endl; return; } int main( ){ cout << "I am in the main." << endl; PrintSomething( ); return 0; } Simplest Procedure void PrintSomething( ){ cout << "Now I am in the procedure" << endl; return; } int main( ){ cout << "I am in the main." << endl; PrintSomething( ); I am in the main. return 0; } 9 Simplest Procedure void PrintSomething( ){ cout << "Now I am in the procedure" << endl; return; } int main( ){ cout << "I am in the main." << endl; PrintSomething( ); I am in the main. return 0; Now I am in the procedure } Pass-byPass-by-value vs. Pass-byPass-by-reference Functions only pass-by-value. This copies pass-by-value. the data into the function and makes it impossible to change the value in the caller's environment. Procedures may both pass-by-value and passpass-bypassby-reference. Variables passed-by-reference by-reference. passed-byallow access to the data in the caller's environment. Pass-byPass-by-value vs. Pass-byPass-by-reference How the data is passed is shown in the formal parameter list. Data passed by reference is marked with an ampersand (&). void TimesTen (double & n){ n = n * 10.0; return; } Output: 40 int main( ){ double n = 4.0; TimesTen(n); cout << n; return 0; } Would it be best written as a function or a procedure? Case 1 A: Swap values between two identifiers B: Calculate the time in London given the time in Ann Arbor Case 2 A: Given a name provide a phone number B: Sort a list of numbers 1- Both Functions 2- A Function, B Procedure 3- A Procedure, B Function 4- Both Procedures Functions vs. Procedures Caller Caller Functions vs. Procedures A 156 B 20 C D 156 E 20 int main( ){ int A=156, B=20, C, D=156, E=20; C = Function(A, B); Procedure(D,E); etc. int main( ){ int A=156, B=20, C, D=156, E=20; C = Function(A, B); Procedure(D,E); etc. 10 Functions vs. Procedures Caller A 156 B 20 C Functions vs. Procedures Caller D 156 E 20 C A 156 B 20 int Function (int X, int Y) { return X+Y; } int Function (int X, int Y) { return X+Y; } void Procedure (int & X, int & Y) { X=X+Y; return; } D 156 E 20 int main( ){ int A=156, B=20, C, D=156, E=20; Function(A, C = Function(A, B); Procedure(D,E); etc. int main( ){ int A=156, B=20, C, D=156, E=20; C = Function(A, B); Procedure(D,E); Procedure(D,E); etc. Functions vs. Procedures Caller A 156 B 20 C Functions vs. Procedures Caller D 156 E 20 A 156 B 20 C 176 int Function (int X, int Y) { return X+Y; } X Y 156 20 void Procedure (int & X, int & Y) { X=X+Y; return; } int Function (int X, int Y) { return X+Y; } X Y 156 20 void Procedure (int & X, int & Y) { X=X+Y; return; } D 156 E 20 int main( ){ int A=156, B=20, C, D=156, E=20; C = Function(A, B); Procedure(D,E); etc. int main( ){ int A=156, B=20, C, D=156, E=20; C = Function(A, B); Procedure(D,E); etc. Functions vs. Procedures Caller A 156 B 20 C 176 Functions vs. Procedures Caller D 156 E 20 A 156 B 20 C 176 int Function (int X, int Y) { return X+Y; } void Procedure (int & X, int & Y) { X X=X+Y; return; Y } int Function (int X, int Y) { return X+Y; } void Procedure (int & X, int & Y) { X X=X+Y; return; Y } D 176 E 20 int main( ){ int A=156, B=20, C, D=156, E=20; C = Function(A, B); Procedure(D,E); etc. int main( ){ int A=156, B=20, C, D=156, E=20; C = Function(A, B); Procedure(D,E); etc. 11 Functions vs. Procedures Caller A 156 B 20 C 176 int Function (int X, int Y) { return X+Y; } void Procedure (int & X, int & Y) { X=X+Y; return; } D 176 E 20 Returning Data Explicitly and via Pass-byPass-by-Reference Can Cause Side Effects One can actually create mixed functionfunctionprocedures that both return values and pass-bypass-byreference. These can lead to strange effects and are discouraged int main( ){ int A=156, B=20, C, D=156, E=20; C = Function(A, B); Procedure(D,E); etc. Example Procedure 1 What are the outputs? void doitnow(int D, int &E, double &F){ E=E+D; F=F*E; return; } A 1 2 10.0 B 1 3 15.0 C 1 2 15.0 Example Procedure 2 What are the outputs? void doitnow(int D, int E, double &F){ E=E+D; F=F*E; return; } A 1 2 10.0 B 1 3 15.0 C 1 2 15.0 int main ( ){ int A=1, B=2 double C=5.0; doitnow(A,B,C); cout << A << endl; cout << B << endl; cout << C << endl; return 0; } int main ( ){ int A=1, B=2 double C=5.0; doitnow(A,B,C); cout << A << endl; cout << B << endl; cout << C << endl; return 0; } Example Procedure 3 What are the outputs? void doitnow(int E, int &D, double &F){ E=E+D; F=F*E; return; } A 1 2 10.0 B 1 3 15.0 C 1 2 15.0 Functions vs. Procedures Scope of Variables When you declare an identifier in a procedure or function either explicitly or by passing it in via the parameter list, it supercedes any previous declarations. declarations. Identifiers inside the function or procedure cover up identifiers declared outside the function or procedure. int main ( ){ int A=1, B=2 double C=5.0; doitnow(A,B,C); cout << A << endl; cout << B << endl; cout << C << endl; return 0; } 12 Functions vs. Procedures Scope of Variables int f(int B, int A){ return B*A*A; } int main ( ){ int A=2, B=3, C=4; C = f(A, B); cout << C << endl; return 0; } B3 C4 Functions vs. Procedures Scope of Variables A 2 int f(int B, int A){ return B*A*A; } int main ( ){ int A=2, B=3, C=4; C = f(A, B); cout << C << endl; return 0; } Functions vs. Procedures Scope of Variables A 2 int f(int B, int A){ B3 B2 return B*A*A; C4 A3 } int main ( ){ int A=2, B=3, C=4; C = f(A, B); cout << C << endl; return 0; } Functions vs. Procedures Scope of Variables A 2 int f(int B, int A){ B3 B2 return B*A*A; C18 A 3 } int main ( ){ int A=2, B=3, C=4; C = f(A, B); cout << C << endl; return 0; } Functions vs. Procedures Scope of Variables A 2 int f(int B, int A){ B3 B2 return B*A*A; C18 A 3 } int main ( ){ int A=2, B=3, C=4; C = f(A, B); cout << C << endl; return 0; } Functions vs. Procedures Scope of Variables A 2 int f(int B, int A){ B3 B2 return B*A*A; C18 A 3 } void p(int C, int B, int &A){ A=C*B*B; return; } int main ( ){ int main ( ){ int A=2, B=3, C=4; int A=2, B=3, C=4; C = f(A, B); p(A, B, C); cout << C << endl; cout << C << endl; return 0; return 0; } } 13 Functions vs. Procedures Scope of Variables A 2 int f(int B, int A){ B3 B2 return B*A*A; C18 A 3 } A 2 void p(int C, int B, int &A){ B3 C4 A=C*B*B; return; } Functions vs. Procedures Scope of Variables A 2 int f(int B, int A){ B3 B2 return B*A*A; C18 A 3 } A 2 void p(int C, int B, int &A){ B3 C2 A=C*B*B; C 4 B 3 return; } A int main ( ){ int main ( ){ int A=2, B=3, C=4; int A=2, B=3, C=4; C = f(A, B); p(A, B, C); cout << C << endl; cout << C << endl; return 0; return 0; } } int main ( ){ int main ( ){ int A=2, B=3, C=4; int A=2, B=3, C=4; C = f(A, B); p(A, B, C); cout << C << endl; cout << C << endl; return 0; return 0; } } Functions vs. Procedures Scope of Variables A 2 int f(int B, int A){ B3 B2 return B*A*A; C18 A 3 } A 2 void p(int C, int B, int &A){ B3 C2 A=C*B*B; C18 B 3 return; } A Functions vs. Procedures Scope of Variables A 2 int f(int B, int A){ B3 B2 return B*A*A; C18 A 3 } A 2 void p(int C, int B, int &A){ B3 C2 A=C*B*B; C18 B 3 return; } A int main ( ){ int main ( ){ int A=2, B=3, C=4; int A=2, B=3, C=4; C = f(A, B); p(A, B, C); cout << C << endl; cout << C << endl; return 0; return 0; } } int main ( ){ int main ( ){ int A=2, B=3, C=4; int A=2, B=3, C=4; C = f(A, B); p(A, B, C); cout << C << endl; cout << C << endl; return 0; return 0; } } Example Procedure 4 What are the outputs? void doitnow(int B, int C, double &A){ B=B+C; A=A*B; return; } A 7 7 5.0 B 7 2 15.0 C 1 2 15.0 Next Lecture File Streams int main ( ){ int A=1, B=2 double C=5.0; doitnow(A,B,C); cout << A << endl; cout << B << endl; cout << C << endl; return 0; } 14 ...
View Full Document

This note was uploaded on 05/04/2010 for the course ENGIN 101 taught by Professor Jeffringenberg during the Winter '07 term at University of Michigan.

Ask a homework question - tutors are online