This preview shows page 1. Sign up to view the full content.
Unformatted text preview: Engineering 101 Engineering 101
Lecture 05 9/18/07
Functions and Procedures Quote of the Day 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 Your Weight on Mars Your Weight on Mars Your Weight is the force exerted on you The Universal Law of Gravitation States GmM W= 2 R m=your mass R=radius M=planet mass G=6.672x1011 N Your weight on earth is WE = mg Your Weight on Mars Your Weight on Mars So the ratio of your weight on another planet to your weight on earth is W GM −12 = = 6.8 ×10 2 WE gR ( m2 s ) M 2 R 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. What is your weight on earth? 165 Weight on Mars is 62.5 What do we want What do we want the program to do? #include <iostream> using namespace std; int main( ){ Start with the scaffolding Start with the scaffolding } return 0; #include <iostream> using namespace std; int main( ){ Add the Obvious Expresssions Add the Obvious Expresssions cout << “What is your weight on earth?” << endl; cin >> earthweight; } cout << “Weight on mars is ” << marsratio*earthweight << endl; return 0; #include <iostream> using namespace std; int main( ){ double marsmass = 6.4e23; double marsrad = 3.4e6; double earthweight; double marsratio; cout << “What is your weight on earth?” << endl; cin >> earthweight; Make sure to add declarations Make sure to add declarations } cout << “Weight on mars is ” << marsratio*earthweight << endl; return 0; #include <iostream> using namespace std; int main( ){ double marsmass = 6.4e23; double marsrad = 3.4e6; double earthweight; double marsratio; cout << “What is your weight on earth?” << endl; cin >> earthweight; 1 marsratio = 6.8e12 * marsmass / marsrad * marsrad; What goes here? What goes here? 2 marsratio = 6.8e12 * marsmass / (marsrad * marsrad); 3 marsratio = 6.8e12 * (marsmass / marsrad * ? marsrad); cout << “Weight on mars is ” << marsratio*earthweight << 4 marsratio = (6.8e12 * marsmass / marsrad * marsrad); endl; } return 0; W GM = = 6.8 ×10 −12 WE gR 2 ( m2 s ) M R2 #include <iostream> using namespace std; int main( ){ double marsmass = 6.4e23; double marsrad = 3.4e6; double earthweight; double marsratio; cout << “What is your weight on earth?” << endl; cin >> earthweight; 1 marsratio = 6.8e12 * marsmass / marsrad * marsrad; What goes here? What goes here? 2 marsratio = 6.8e12 * marsmass / (marsrad * marsrad); 3 marsratio = 6.8e12 * (marsmass / marsrad * ? marsrad); cout << “Weight on mars is ” << marsratio*earthweight << 4 marsratio = (6.8e12 * marsmass / marsrad * marsrad); endl; } return 0; W GM = = 6.8 ×10 −12 WE gR 2 ( m2 s ) M R2 #include <iostream> using namespace std; int main( ){ double marsmass = 6.4e23; double marsrad = 3.4e6; double earthweight; double marsratio; cout << “What is your weight on earth?” << endl; cin >> earthweight; marsratio = 6.8e12*marsmass/(marsrad*marsrad); cout << “Weight on mars is ” << marsratio*earthweight << endl; return 0; We are almost done! We are almost done! } #include <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; // The mass of Mars double marsrad = 3.4e6; // The radius of Mars double earthweight; // The weight on Earth double marsratio; // Ratio of Mars 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); cout << “Weight on mars is ” << marsratio*earthweight << endl; return 0; Add comments for the reader Add comments for the reader What if… 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); cout << “Weight on Mars is ” << marsratio*earthweight // compute the weight of the person on Jupiter jupratio = 6.8e12*jupmass/(juprad*juprad); cout << “Weight on Jupiter is ” << jupratio*earthweight return 0; << endl; << endl; } News Flash! Announcing… The First… Semiannual… Chad’s Lab Sections
1. Student teams give presentations in lab section 2. Lab sections pick their best presentation and put it online 3. Chad and Mike pick the best presentation from all their sections 4. Semifinalists give their presentations in lecture Mike’s Lab Sections 402 403 405 401 404 406 Section votes Section votes Section votes Section votes Section votes Section votes Chad votes Mike votes 5. Whole class plus a judging panel picks the overall winner Lecture votes + Judging Panel Participation Participation Round one presentations in your lab section are part of your lab participation grade You will present twice during the term You will be on a different team each time Teams are posted on CTools Team Coordination Team Coordination Meet your team early tomorrow is the first official meet/greet Team size varies depending on lab section size More people definitely does not mean less work Presentation Format Presentation Format 15 minutes total Each student on your team must speak for approximately the same amount of time Content should be relevant to the lectures being covered on the exam Creative format is highly encouraged – winning lab section presentations will be placed on CTools Competition Extra Credit Competition Extra Credit First round winners receive 2% extra credit on the exam Second round winners receive additional 3% extra credit on the exam Third (final) round winners receive additional 5% extra credit on the exam Extra credit can’t boost you over 100% Avoid Repetition in Programs 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. Functions and Procedures Functions and Procedures Functions and procedures encapsulate algorithms so they can be written once and used repeatedly. Also called routines or subroutines. Functions versus Procedures Functions versus Procedures Functions return data directly to the caller and take as input copies of data from the caller (callbyvalue) Procedures and commands do not directly return any value, but operate on data in the caller (callbyreference). Declaring Functions Declaring Functions Functions can either be predefined library functions or userdefined functions. 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 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. Very common functions and procedures are often available in libraries. Libraries are included at the beginning of a program using a preprocessor directive #include <library_name> Library Functions and Library Functions and Procedures Libraries described in Appendix B
cmath cstdlib cctype iostream fstream string vector Library Functions and Library Functions and Procedures
Standard math library C standard library (rand) C character types Standard I/O File Streams Character strings Vectors If you include the cmath library you can then do important things like double sqrt (double x); square root double abs (double x); absolute value double sin (double x); sine of x in radians double floor (double x); round down double ceil (double x); round up double log (double x); find the natural log of x double pow (double x, double y); xy output input cmath Library cmath Library Example Using cmath Library 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? What does this expression do? log base10 floor( x / pow (10, floor( log10(x)))) 1 returns the last nonzero digit in a number 2 returns the first nonzero digit in a number 3 rounds a number to 2 significant digits 4 rounds a number to 1 significant digit This one is hard… What does this expression do? What does this expression do? floor( x / pow (10, floor( log10(x)))) 1 returns the last nonzero digit in a number 2 returns the first nonzero digit in a number 3 rounds a number to 2 significant digits 4 rounds a number to 1 significant digit Consider Recall x=2456 103 = 1000 log10(2456)=3.390…. 104 = 10000 floor(3.390….)=3 pow(10,3) = 1000 2456/1000 = 2.456 floor(2.456) = 2.0 Defining Functions Defining Functions Userdefined 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. Remember this? How can we use functions to make it simpler?
#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); cout << “Weight on Mars is ” << marsratio*earthweight // compute the weight of the person on Jupiter jupratio = 6.8e12*jupmass/(juprad*juprad); cout << “Weight on Jupiter is ” << jupratio*earthweight return 0; << endl; << endl; } UserDefined Functions Simplify UserDefined Functions Simplify Code
marsratio = 6.8e12 * marsmass / ( marsrad * marsrad ); cout << “Weight on Mars is ” << marsratio*earthweight<< endl; UserDefined Functions Simplify UserDefined Functions Simplify Code
marsratio = 6.8e12 * marsmass / ( marsrad * marsrad ); cout << “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( ) UserDefined Functions Simplify UserDefined Functions Simplify Code
marsratio = 6.8e12 * marsmass / ( marsrad * marsrad ); cout << “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 ) UserDefined Functions Simplify UserDefined Functions Simplify Code
marsratio = 6.8e12 * marsmass / ( marsrad * marsrad ); cout << “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) UserDefined Functions Simplify UserDefined Functions Simplify Code
marsratio = 6.8e12 * marsmass / ( marsrad * marsrad ); cout << “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. double newweight( double ew, double pmass, double prad) UserDefined Functions Simplify UserDefined 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) #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); cout << “Weight on Mars is ” << marsratio*earthweight // compute the weight of the person on Jupiter jupratio = 6.8e12*jupmass/(juprad*juprad); cout << “Weight on Jupiter is ” << jupratio*earthweight return 0; << endl; << endl; } #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; } Writing the Function Definition Writing the Function Definition
double newweight( double ew, double pmass, double prad) { } Braces enclose the body of the function Writing the Function Definition Writing the Function Definition
double newweight( double ew, double pmass, double prad) { ratio = 6.8e12 * pmass / ( prad * prad ); } We calculate the ratio inside the function. Writing the Function Definition Writing the Function Definition
double newweight( double ew, double pmass, double prad) { double ratio; ratio = 6.8e12 * pmass / ( prad * prad ); } This ratio is only defined inside the function, and so it must be declared in the function. Writing the Function Definition Writing the Function Definition
double newweight( double ew, double pmass, double prad) { double ratio; ratio = 6.8e12 * pmass / ( prad * prad ); return ew*ratio; } The function returns the ratio times the weight on earth. This is a double. #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 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; } #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 ); return ew*ratio; } int main( ) { double marsmass = 6.4e23; double marsrad = 3.4e6; double jupmass = 1.9e27; double juprad = 7.0e7; double earthweight; // 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 ofthe person on Jupiter cout << “Weight on Jupiter is ” << newweight(earthweight, jupmass, juprad) << endl; return 0; Project 2 Project 2 Next Lecture Next Lecture More Procedures / Side Effects ...
View
Full
Document
 Fall '07
 JeffRingenberg

Click to edit the document details