Lecture+06 - Engineering 101 Engineering 101 Lecture 6...

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 Engineering 101 Lecture 6 9/20/07 More Procedures / Side Effects Quote of the Day Quote of the Day The essence of knowledge is, having it, to apply it; ­ Confucius not having it, to confess your ignorance. Functions Provide Structure 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 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 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 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 Provide Structure 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; } Functions Provide Structure Functions Provide Structure output type 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 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 Function Calls Definition Or Declaration Must 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 Definition Or Declaration Must 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 Function Calls Function Definition Definition Or Declaration Must 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; } Note that Z is declared in the function definition, not with the function declaration What does the function do? int ticktock( int time) { return time/60; } 1- Returns the time in minutes converted to hours. 2- Returns the time in minutes converted to seconds. 3- Changes value in time to hours from minutes. 4- Changes value in time to seconds from minutes. What does the function do? int ticktock( int time) { return time/60; } 1- Returns the time in minutes converted to hours. 2- Returns the time in minutes converted to seconds. 3- Changes value in time to hours from minutes. 4- Changes value in time to seconds from minutes. Functions vs. Procedures 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 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 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, 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 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 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; } Simplest Procedure 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 } Functions only pass­by­value. This copies 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 pass­by­reference. Variables passed­by­ reference allow access to the data in the caller’s environment. Pass­by­value Pass­by­value vs. Pass­by­reference Pass­by­value Pass­by­value vs. Pass­by­reference How the data is passed is shown in the formal parameter list. Data passed by reference is marked with an ampersand (&). int main( ){ double n = 4.0; TimesTen(n); cout << n; return 0; } void TimesTen (double & n){ n = n * 10.0; return; } Output: 40 Would it be best written as a Would it be best written as a function or a procedure? Given two operations determine if each is better written as a function or a procedure. A: Swap values between two identifiers B: Calculate the time in London given the time in Ann Arbor 1- Both Functions 2- A Function, B Procedure 3- A Procedure, B Function 4- Both Procedures A: Swap values between two identifiers B: Calculate the time in London given the time in Ann Arbor 1- Both Functions 2- A Function, B Procedure 3- A Procedure, B Function 4- Both Procedures 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 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 Caller Caller Functions vs. Procedures int main( ){ int A=156, B=20, C, D=156, E=20; C = Function(A, B); Procedure(D,E); etc. Caller Caller Functions vs. Procedures D 156 E 20 A 156 B 20 C int main( ){ int A=156, B=20, C, D=156, E=20; C = Function(A, B); Procedure(D,E); etc. Caller Caller Functions vs. Procedures int Function (int X, int Y) { return X+Y; } D 156 E 20 A 156 B 20 C int main( ){ int A=156, B=20, C, D=156, E=20; C = Function(A, B); Procedure(D,E); etc. Caller Caller Functions vs. Procedures int Function (int X, int Y) { return X+Y; } void Procedure (int & X, int & Y) { X=X+Y; return; } D 156 E 20 A 156 B 20 C int main( ){ int A=156, B=20, C, D=156, E=20; C = Function(A, B); Procedure(D,E); etc. Caller Caller Functions vs. Procedures int Function (int X, int Y) { return X+Y; }X Y 156 20 A 156 B 20 C 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. Caller Caller Functions vs. Procedures int Function (int X, int Y) { return X+Y; }X Y 156 20 A 156 B 20 C 176 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. Caller Caller Functions vs. Procedures int Function (int X, int Y) { return X+Y; } void Procedure (int & X, int & Y) { X X=X+Y; return; Y } D 156 E 20 A 156 B 20 C 176 int main( ){ int A=156, B=20, C, D=156, E=20; C = Function(A, B); Procedure(D,E); etc. Caller Caller Functions vs. Procedures 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 A 156 B 20 C 176 int main( ){ int A=156, B=20, C, D=156, E=20; C = Function(A, B); Procedure(D,E); etc. Caller Caller Functions vs. Procedures int Function (int X, int Y) { return X+Y; } void Procedure (int & X, int & Y) { X=X+Y; return; } D 176 E 20 A 156 B 20 C 176 int main( ){ int A=156, B=20, C, D=156, E=20; C = Function(A, B); Procedure(D,E); etc. News Flash! News Flash! Created by Raph Koster, the architect of Star Wars Galaxies and Ultima Online “Every world is a web server, and every object has a URL. You can script an object so that it feeds RSS, XML, or HTML to a browser. [Make] high score tables, objects that email you, player profile pages right on the player. Every object can also browse the Web: a chat bot can chatter headlines from an RSS feed, a newspaper with real headlines can sit on your virtual desk, game data could come from real world data...” http://metaplace.com/ 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 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 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 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 1 void doitnow(int D, int &E, double &F){ E=E+D; F=F*E; return; } A 1 B 2 C 5.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 1 void doitnow(int D, int &E, double &F){ E=E+D; F=F*E; return; } D E F 1 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; } A 1 B 2 C 5.0 Example Procedure 1 void doitnow(int D, int &E, double &F){ E=E+D; F=F*E; return; } D E F 1 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; } A 1 B 3 C 5.0 Example Procedure 1 void doitnow(int D, int &E, double &F){ E=E+D; F=F*E; return; } D E F 1 int main ( ){ int A=1, B=2 double C=5.0; A doitnow(A,B,C); cout << A << endl; 1 cout << B << endl; B cout << C << endl; 3 C return 0; 15.0 } Example Procedure 1 int main ( ){ int A=1, B=2 double C=5.0; A doitnow(A,B,C); cout << A << endl; 1 cout << B << endl; B cout << C << endl; 3 C return 0; 15.0 } 1 3 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; } 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; } Example Procedure 2 void doitnow(int D, int E, double &F){ E=E+D; F=F*E; return; } A 1 B 2 C 5.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 2 void doitnow(int D, int E, double &F){ E=E+D; F=F*E; return; } D E F 1 2 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; } A 1 B 2 C 5.0 Example Procedure 2 void doitnow(int D, int E, double &F){ E=E+D; F=F*E; return; } D E F 1 3 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; } A 1 B 2 C 5.0 Example Procedure 2 void doitnow(int D, int E, double &F){ E=E+D; F=F*E; return; } D E F 1 3 int main ( ){ int A=1, B=2 double C=5.0; A doitnow(A,B,C); cout << A << endl; 1 cout << B << endl; B cout << C << endl; 2 C return 0; 15.0 } Example Procedure 2 int main ( ){ int A=1, B=2 double C=5.0; A doitnow(A,B,C); cout << A << endl; 1 cout << B << endl; B cout << C << endl; 2 C return 0; 15.0 } 1 2 15.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 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 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 void doitnow(int E, int &D, double &F){ E=E+D; F=F*E; return; } A 1 B 2 C 5.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 void doitnow(int E, int &D, double &F){ E=E+D; F=F*E; return; } E D F 1 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; } A 1 B 2 C 5.0 Example Procedure 3 void doitnow(int E, int &D, double &F){ E=E+D; F=F*E; return; } E D F 3 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; } A 1 B 2 C 5.0 Example Procedure 3 void doitnow(int E, int &D, double &F){ E=E+D; F=F*E; return; } D E F 3 int main ( ){ int A=1, B=2 double C=5.0; A doitnow(A,B,C); cout << A << endl; 1 cout << B << endl; B cout << C << endl; 2 C return 0; 15.0 } Example Procedure 3 int main ( ){ int A=1, B=2 double C=5.0; A doitnow(A,B,C); cout << A << endl; 1 cout << B << endl; B cout << C << endl; 2 C return 0; 15.0 } 1 2 15.0 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. Identifiers inside the function or procedure cover up identifiers declared outside the function or procedure. Functions vs. Procedures Functions vs. Procedures Scope of Variables 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; } Functions vs. Procedures Scope of Variables A 2 int f(int B, int A){ B3 C4 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; } Functionsvs. 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; } } Functions vs. Procedures 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; } 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 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; } } Functions vs. Procedures 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; } } Functions vs. Procedures 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; } } 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 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 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 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 4 void doitnow(int B, int C, double &A){ B=B+C; A=A*B; return; } A 1 B 2 C 5.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 4 void doitnow(int B, int C, double &A){ B=B+C; A=A*B; return; } B C A 1 2 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; } A 1 B 2 C 5.0 Example Procedure 4 void doitnow(int B, int C, double &A){ B=B+C; A=A*B; return; } B C A 3 2 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; } A 1 B 2 C 5.0 Example Procedure 4 void doitnow(int B, int C, double &A){ B=B+C; A=A*B; return; } B C A 3 2 int main ( ){ int A=1, B=2 double C=5.0; A doitnow(A,B,C); cout << A << endl; 1 cout << B << endl; B cout << C << endl; 2 C return 0; 15.0 } Example Procedure 4 int main ( ){ int A=1, B=2 double C=5.0; A doitnow(A,B,C); cout << A << endl; 1 cout << B << endl; B cout << C << endl; 2 C return 0; 15.0 } 1 2 15.0 Now… Now… About those side effects Returning Data Explicitly and Returning Data Explicitly and via Pass­by­Reference Can Cause Side Effects One can actually create mixed function­ procedures that both return values and pass­ by­reference. These can lead to strange effects and are discouraged int badFoo( int & a){ a = a + 1; return a; } Example of Problems with Example of Problems with Mixed Function/Procedures int main( ){ int x=3, y=3; cout << badFoo(x) + badFoo(x) << “ “<< x << endl; cout << 2 * badFoo(y) << “ “ << y << endl; return 0; } int badFoo( int & a){ a = a + 1; return a; } Example of Problems with Example of Problems with Mixed Function/Procedures x3 y3 int main( ){ int x=3, y=3; cout << badFoo(x) + badFoo(x) << “ “<< x << endl; cout << 2 * badFoo(y) << “ “ << y << endl; return 0; } int badFoo( int & a){ a = a + 1; return a; } Example of Problems with Example of Problems with Mixed Function/Procedures x3 a y3 int main( ){ int x=3, y=3; cout << badFoo(x) + badFoo(x) << “ “<< x << endl; cout << 2 * badFoo(y) << “ “ << y << endl; return 0; } int badFoo( int & a){ a = a + 1; return a; } Example of Problems with Example of Problems with Mixed Function/Procedures x4 a y3 int main( ){ int x=3, y=3; cout << badFoo(x) + badFoo(x) << “ “<< x << endl; cout << 2 * badFoo(y) << “ “ << y << endl; return 0; } int badFoo( int & a){ a = a + 1; return a; } Example of Problems with Example of Problems with Mixed Function/Procedures x4 a y3 int main( ){ int x=3, y=3; cout << 4 + badFoo(x) << “ “<< x << endl; cout << 2 * badFoo(y) << “ “ << y << endl; return 0; } int badFoo( int & a){ a = a + 1; return a; } Example of Problems with Example of Problems with Mixed Function/Procedures x4 a y3 int main( ){ int x=3, y=3; cout << 4 + badFoo(x) << “ “<< x << endl; cout << 2 * badFoo(y) << “ “ << y << endl; return 0; } int badFoo( int & a){ a = a + 1; return a; } Example of Problems with Example of Problems with Mixed Function/Procedures x5 a y3 int main( ){ int x=3, y=3; cout << 4 + badFoo(x) << “ “<< x << endl; cout << 2 * badFoo(y) << “ “ << y << endl; return 0; } int badFoo( int & a){ a = a + 1; return a; } Example of Problems with Example of Problems with Mixed Function/Procedures x5 a y3 int main( ){ int x=3, y=3; cout << 4 + 5 << “ “<< x << endl; cout << 2 * badFoo(y) << “ “ << y << endl; return 0; } int badFoo( int & a){ a = a + 1; return a; } Example of Problems with Example of Problems with Mixed Function/Procedures x5 y3 int main( ){ int x=3, y=3; cout << 9 << “ “<< x << endl; cout << 2 * badFoo(y) << “ “ << y << endl; return 0; } int badFoo( int & a){ a = a + 1; return a; } Example of Problems with Example of Problems with Mixed Function/Procedures x5 y3 int main( ){ int x=3, y=3; cout << 9 << “ “<< 5 << endl; cout << 2 * badFoo(y) << “ “ << y << endl; return 0; } int badFoo( int & a){ a = a + 1; return a; } Example of Problems with Example of Problems with Mixed Function/Procedures x5 y3 95 int main( ){ int x=3, y=3; cout << 9 << “ “<< 5 << endl; cout << 2 * badFoo(y) << “ “ << y << endl; return 0; } int badFoo( int & a){ a = a + 1; return a; } Example of Problems with Example of Problems with Mixed Function/Procedures x5 a y3 95 int main( ){ int x=3, y=3; cout << badFoo(x) + badFoo(x) << “ “<< x << endl; cout << 2 * badFoo(y) << “ “ << y << endl; return 0; } int badFoo( int & a){ a = a + 1; return a; } Example of Problems with Example of Problems with Mixed Function/Procedures x5 a y4 95 int main( ){ int x=3, y=3; cout << badFoo(x) + badFoo(x) << “ “<< x << endl; cout << 2 * badFoo(y) << “ “ << y << endl; return 0; } int badFoo( int & a){ a = a + 1; return a; } Example of Problems with Example of Problems with Mixed Function/Procedures x5 a y4 95 int main( ){ int x=3, y=3; cout << badFoo(x) + badFoo(x) << “ “<< x << endl; cout << 2 * 4 << “ “ << y << endl; return 0; } int badFoo( int & a){ a = a + 1; return a; } Example of Problems with Example of Problems with Mixed Function/Procedures x5 a y4 95 int main( ){ int x=3, y=3; cout << badFoo(x) + badFoo(x) << “ “<< x << endl; cout << 8 << “ “ << y << endl; return 0; } int badFoo( int & a){ a = a + 1; return a; } Example of Problems with Example of Problems with Mixed Function/Procedures x5 y4 95 int main( ){ int x=3, y=3; cout << badFoo(x) + badFoo(x) << “ “<< x << endl; cout << 8 << “ “ << 4 << endl; return 0; } int badFoo( int & a){ a = a + 1; return a; } Example of Problems with Example of Problems with Mixed Function/Procedures x5 y4 95 int main( ){ 84 int x=3, y=3; cout << badFoo(x) + badFoo(x) << “ “<< x << endl; cout << 8 << “ “ << 4 << endl; return 0; } int badFoo( int & a){ a = a + 1; return a; } Example of Problems with Example of Problems with Mixed Function/Procedures x5 y4 95 int main( ){ 84 int x=3, y=3; cout << badFoo(x) + badFoo(x) << “ “<< x << endl; cout << 2 * badFoo(y) << “ “ << y << endl; return 0; } Next Lecture Next Lecture Michigan Lecture Competition! Phase One ...
View Full Document

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

Ask a homework question - tutors are online