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.8e12*marsmass/(marsrad*marsrad); 6.8e12*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.8e12*marsmass/(marsrad*marsrad); 6.8e12*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.8e12*marsmass/(marsrad*marsrad); 6.8ecout << "Weight on Mars is " << marsratio*earthweight << endl; // compute the weight of the person on Jupiter jupratio = 6.8e12*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(callbyvalue) byProcedures and commands do not directly return any value, but operate on data in the caller (callbyreference). (callby Declaring Functions
Functions can either be predefined library prefunctions or userdefined 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 nonzero digit in a number non2 returns the first nonzero 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
UserUserdefined 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.8e12*marsmass/(marsrad*marsrad); 6.8ecout << "Weight on Mars is " << marsratio*earthweight << endl; // compute the weight of the person on Jupiter jupratio = 6.8e12*jupmass/(juprad*juprad); 6.8ecout << "Weight on Jupiter is " << jupratio*earthweight << endl; return 0; } 4 UserUserDefined Functions Simplify Code
marsratio = 6.8e12 * marsmass / ( marsrad * marsrad ); 6.8ecout << "Weight on Mars is " << marsratio*earthweight<< endl; UserUserDefined Functions Simplify Code
marsratio = 6.8e12 * 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( ) UserUserDefined Functions Simplify Code
marsratio = 6.8e12 * 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 ) UserUserDefined Functions Simplify Code
marsratio = 6.8e12 * 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) UserUserDefined Functions Simplify Code
marsratio = 6.8e12 * 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) UserUserDefined 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.8e12*marsmass/(marsrad*marsrad); 6.8ecout << "Weight on Mars is " << marsratio*earthweight << endl; // compute the weight of the person on Jupiter jupratio = 6.8e12*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.8e12 * 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.8e12 * pmass / ( prad * prad ); 6.8e} Writing the Function Definition
double newweight( double ew, double pmass, double prad) { double ratio; ratio = 6.8e12 * 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.8e12 * 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 } PassbyPassbyvalue vs. PassbyPassbyreference
Functions only passbyvalue. This copies passbyvalue. the data into the function and makes it impossible to change the value in the caller's environment. Procedures may both passbyvalue and passpassbypassbyreference. Variables passedbyreference byreference. passedbyallow access to the data in the caller's environment. PassbyPassbyvalue vs. PassbyPassbyreference
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...
View
Full Document
 Winter '07
 JeffRingenberg
 Return statement, Void type, C standard library, marsratio

Click to edit the document details