Slide 16 How to use a pre-defined function Distance between two points #include <iostream> #include <cmath> // contains sqrt( using namespace std int main()i double xl yl x2, y2 // coordinates for point 1 &2 double dist // distance between points cout <<Enter x &y coordinates of the lst point: i cout/<<"Enter x y coordinates of the 2nd point: dist=/sqrt(x2-x1)*(x2-×1)+(y2-y1)*(y2-y1)); cout n The distance is dist < endl return/o They are also function calls
Slide 16 How to use a pre-defined function: Distance Between Two Points #include <iostream> #include <cmath> // contains sqrt() using namespace std; int main(){ double x1, y1, x2, y2; // coordinates for point 1 & 2 double dist; // distance between points cout << "Enter x & y coordinates of the 1st point: "; cin >> x1 >> y1; cout << "Enter x & y coordinates of the 2nd point: "; cin >> x2 >> y2; dist = sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1)); cout << "The distance is " << dist << endl; return 0; } They are also function calls!
Slide 17 How to define a function? a function definition has following syntax <type> <function name>(<parameter list>)i local declarations> <sequence of statements>
Slide 17 How to define a function? A function definition has following syntax: <type> <function name>(<parameter list>){ <local declarations> <sequence of statements> }
Slide 18 Function A function returns a single result (that's the type of the function) One of the statements in the function body should have the form return <expression> (it's good to use ONLY one at last!) The value passed back by return should have the same type as the function
Slide 18 A function returns a single result (that’s the type of the function) One of the statements in the function body should have the form: return <expression>; (it’s good to use ONLY one at last!) The value passed back by return should have the same type as the function
Slide 19 int main(( int value nt absvalue cout < Enter integer cin > value Input f (value 0) absolute value absvalue -value else absvalue value; out < The absolute value i < absvalue < endl Output return 0
Slide 19 int main(){ int value; int absvalue; cout << "Enter integer: "; cin >> value; if (value < 0) absvalue = -value; else absvalue = value; cout << "The absolute value is " << absvalue << endl; return 0; } if (value < 0) absvalue = -value; else absvalue = value; absoluteValue Input Output
Slide 20 Example: Absolute Value(1st version) #include <iostream> using name space stdi int absolute(int fx)i int absi if(fx >=0) abs=fx else abs =-fxi return abs int main(( int x, y, diff, diff cout < Enter two integers (separated by a blank):i diff aiff absolute (diff)i cout <s iThe absolute difference between l << d"<<y<<"i: diff < endl return O
Slide 20 Example: Absolute Value (1st version) #include <iostream> using namespace std; int absolute(int fx){ int abs; if (fx >= 0) abs=fx else abs = -fx; return abs; } int main(){ int x, y, diff,adiff; cout << "Enter two integers (separated by a blank): "; cin >> x >> y; diff = x – y; adiff=absolute(diff); cout << "The absolute difference between " << x << " and " << y << " is: " << adiff << endl; return 0; }