Presentation is loading. Please wait.

Presentation is loading. Please wait.

Functions.

Similar presentations


Presentation on theme: "Functions."— Presentation transcript:

1 Functions

2 Where are we now? Midterm: now Simple types of variables Array
structured programming now Simple types of variables 3 program structures cin(>>)/cout(<<) Array Functions (subprograms) (File I/O)

3 Review: Problem Solving Process
Define and analyze the problem What is the input & output? What constraints must be satisfied? What information is essential? Design an algorithm What steps must be done? Wirte down the solution steps in detail Implement a program - programming or coding. Compile, test, and debug the program - testing. Document and maintain the program. Problem solving phase Implementation phase

4 Top-down analysis A complex problem is often easier to solve by dividing it into several smaller parts (subproblems), each of which can be solved by itself. This is called top-down programming. Let’s take the example of Diamand drawing!

5 Example: Diamond Pattern
Input: nothing Output: print out a diamond pattern * * * * * * * * * * * * * * * * * * * * * * * * *

6 Example: top-down analysis
Output: print out a diamond pattern print out the upper half  upper triangular form print out the lower half  lower triangular form

7 Example: upper triangular pattern
row 1: print 4 spaces, 1 star; row 2: print 3 spaces, 3 stars; row 3: print 2 spaces, 5 stars; row 4: print 1 space, 7 stars; row 5: print 0 spaces, 9 stars; * * * * * * * * * * * * * * * * * * * * * * * * *

8 Example: top-down refinement
row 1: print (5-row) spaces, (2*row - 1) stars; row 2: print (5-row) spaces, (2*row - 1) stars; row 3: print (5-row) spaces, (2*row - 1) stars; row 4: print (5-row) spaces, (2*row - 1) stars; row 5: print (5-row) spaces, (2*row - 1) stars; For each ‘row’ (more exactly from row 1 to 5), always do Print 5-row spaces Print 2*row-1 stars It’s a loop: pseudo-code * * * * * * * * * * * * * * * * * * * * * * * * * int row; row=1; while(row<=5){ print 5-row spaces; print 2*row-1 stars; row=row+1; }

9 Example: algorithm int row, space, star; row=1; int row;
while(row<=5){ space=1; while(space<=5-row) { cout << " "; space=space+1; } star=1; while(star<=2*row-1) { cout << "*"; star=star+1; cout << endl ; row=row+1; int row; row=1; while(row<=5){ 5-row spaces; 2*row-1 stars; row=row+1; }

10 Introduction to Functions
The subproblems are solved by subprograms, called functions in C++. main() is the first function, then executes these functions so that the original problem is solved.

11 Advantages of Functions
separate the concept (what is done) from the implementation (how it is done). make programs easier to understand. make programs easier to modify. can be called several times in the same program, allowing the code to be reused.

12 Function Input and Output
results Input parameters

13 C++ Functions C++ allows the use of both pre-defined and user-defined functions. Predefined functions (e.g., cin, cout, rand, etc.) are usually grouped into specialized libraries (e.g., iostream, cstdlib, cmath, etc.)

14 Mathematical Functions
#include <cmath> double log(double x) natural logarithm double log10(double x) base 10 logarithm double exp(double x) e to the power x double pow(double x, double y) x to the power y double sqrt(double x) positive square root of x double ceil(double x) smallest integer not less than x double floor(double x) largest integer not greater than x double sin(double x), cos(double x), tan(double x), etc...

15 How to use a pre-defined function: Distance Between Two Points
Output:distance Input: two points: (x1,y1), and (x2,y2) Distance = sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1)) x1,y1, x2, y2 are input data, so we are done!

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!

17 How to define a function?
A function definition has following syntax: <type> <function name>(<parameter list>){ <local declarations> <sequence of statements> }

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.

19 Input absoluteValue Output 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; } Input if (value < 0) absvalue = -value; else absvalue = value; absoluteValue Output

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;

21 Function Prototype The function prototype declares the interface, or input and output parameters of the function, leaving the implementation for the function definition. (It therefore separates the concept from the implementation!) The function prototype has the following syntax: <type> <function name>(<type list>);

22 You can place a function definition in front of main()
You can place a function definition in front of main(). In this case there is no need to provide a function prototype for the function, since the function is already defined before its use. The function definition can be placed anywhere in the program after the function prototypes. C++ programs usually have the following form: // include statements // function prototypes // main() function // user-defined functions

23 Absolute Value (2nd version)
#include <iostream> using namespace std; int absolute (int); // function prototype for absolute() int main(){ int x, y, diff; cout << "Enter two integers (separated by a blank): "; cin >> x >> y; diff = absolute( x - y); cout << "The absolute difference between " << x << " and " << y << " is: " << diff << endl; return 0; } int absolute(int fx){ int abs; if (fx >= 0) abs=fx else abs=-fx; return abs;

24 How to use or call a function?
A function call has the following syntax: <function name>(<parameter list>) There is a one-to-one correspondence between the parameters in a function call and the parameters in the function definition.

25 Summary with Absolute Value Example
#include <iostream> using namespace std; int absolute (int); // function prototype for absolute() // int absolute (int fx); int main(){ int x, y, diff; cout << "Enter two integers (separated by a blank): "; cin >> x >> y; diff = x-y; diff = absolute(diff); cout << "The absolute difference between " << x << " and " << y << " is: " << diff << endl; return 0; } // Define a function to take absolute value of an integer int absolute(int fx) { int abs; if (fx >= 0) abs=fx; else abs= -fx; return (abs); function call, argument formal parameter final result of the function

26 ‘Void’ type functions A function returns a single result, but a void type function does not return anything (but does something, a sub-program)! (‘main’ is just a function that has no formal parameters for the moment )

27 Example (void): Printing Cards
The main() program which calls printcard() #include <iostream> using namespace std; void printcard(int); // function prototype int main(){ int c1, c2, c3, c4, c5; // pick cards . . . // print cards printcard(c1); printcard(c2); printcard(c3); printcard(c4); printcard(c5); // find score // print score }

28 (This is a void function - a function that does not return a value)
A function that prints the card (J) given the card number (11) as input: void printcard(int cardnum){ if(cardnum==1) cout << "A"; else if(cardnum>=2 && cardnum<=10) cout << cardnum; else if(cardnum==11) cout << "J"; else if(cardnum==12) cout << "Q"; else if(cardnum==13) cout << "K"; } (This is a void function - a function that does not return a value)

29 Example: Total length of a triangle
Output: total-length Input: three points: A=(x1,y1), B=(x2,y2), and C=(x3,y3) Total-length = length(AB) + length(AC) + length(AB) Length(AB) = dist(A,B); Length(AC) = dist(A,C); Length(BC) = dist(B,C);

30 Example: Total length of a triangle
#include <iostream> #include <cmath> using namespace std; double dist(double, double, double, double); int main(){ double x1, y1, // coordinates for point 1 x2, y2, // coordinates for point 2 x3, y3, // coordinates for point 3 l12,l23,l13,length; cout << "Enter x & y coordinates of the 1st point: "; cin >> x1 >> y1; cout << "Enter x & y coordinates of the 2nd point: "; cin >> x2 >> y2; cout << "Enter x & y coordinates of the 3rd point: "; cin >> x3 >> y3;

31 Two different variables How to communicate? Pass by value!
l12 = dist(x1,y1,x2,y2); l23 = dist(x2,y2,x3,y3); l13 = dist(x1,y1,x3,y3); length = l12 + l23 + l13; return 0; } // Function for computing the distance between 2 pts double dist(double fx1, double fy1, double fx2, double fy2) { double dist; dist = sqrt( (fx2-fx1)*(fx2-fx1) + (fy2-fy1)*(fy2-fy1) ); return dist; Two different variables How to communicate? Pass by value! exactly the same! // Function for computing the distance between 2 pts double dist(double x1, double y1, double x2, double y2) { double dist; dist = sqrt( (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) ); return dist; }

32 Pass by value

33 Passing parameters by Value
This is by default and desirable behavior! An important fact to remember about parameter passing by value is that changes to the parameters inside the function body have no effect outside of the function. You may think that calling parameters and formal parameters are just different variables.

34 Example 0 For example, consider the following code:
int sum(int a, int b){ a = a + b; return a; } void main(){ int x, y, z; x = 3; y = 5; z = sum(x,y); What is the value of x, y, and z at the end of the main() program?

35 The answer: 3, 5, and 8. Even though the value of parameter a is changed, the corresponding value in variable x does not change. This is why this is called pass by value. The value of the original variable is copied to the parameter, but changes to the value of the parameter do not affect the original variable. In fact, all information in (local) variables declared within the function will be lost when the function terminates. The only information saved from a pass by value function is in the return statement.

36 Example 1 An example to show how the function does not affect a variable which is used as a parameter: // Test the effect of a function // on its parameter #include <iostream> using namespace std; void Increment(int Number) { Number = Number + 1; cout << "The parameter Number is: " << Number << endl; }

37 void main() { int I = 10; //parameter is a variable Increment(I); cout << "The variable I is: " << I << endl; //parameter is a constant Increment(35); //parameter is an expression Increment(I+26); }

38

39 Example 2 // Print the sum and average of two numbers
// Input: two numbers x & y // Output: sum - the sum of x & y // average - the average of x & y #include <iostream> using namespace std; void PrintSumAve ( double, double ); int main ( ) { double x, y; cout << "Enter two numbers: "; cin >> x >> y; PrintSumAve ( x , y ); }

40 void PrintSumAve (double no1, double no2) {
double sum, average; sum = no1 + no2; average = sum / 2; cout << "The sum is " << sum << endl; cout << "The average is " << average << endl; }

41 Data areas after call to PrintSumAve() :

42 Example 3 //Compute new balance at a certain interest rate
//Inputs: A positive balance and // a positive interest rate //Output: The new balance after interest was posted #include <iostream> using namespace std; double new_balance(double balance, double rate); /* Returns the balance in a bank account after adding interest. For example, if rate is 5.0, then the interest rate is 5% and so new_balance(100, 5.0) returns */

43 int main(){ double interest, balance; cout << "Enter balance (positive number): "; cin >> balance; if (balance <= 0.0) cout <<"Balance not positive; stopping" << endl; else { cout <<"Enter interest rate (positive number): "; cin >> interest; if (interest <= 0.0) cout << "Interest not positive; stopping" << endl; else cout <<"New balance = $" << new_balance(balance, interest)<< endl; } return 0;

44 // New balance is computed as balance + balance * %rate
double new_balance(double balance, double rate) { double interest_fraction, interest; interest_fraction = rate / 100; interest = interest_fraction * balance; return (balance + interest); }

45 Summary Function parameters (like variables)
Function type (type of the function name) Return statement (the last statement) Finishes the function Return the value to the caller Understanding the ‘flow control’ between ‘main’ and a function Communication: pass by value Empty ‘void’ type

46 Input: formal parameters
Similar to (local) variables

47 Output: return statement
The final result (that should be returned to the calling expression), the end of the ‘sub-programme’ Recommended to use only one ‘return’ as the last statement of the function (for good structure of the programmes) It can in fact appear any places (close friends of ‘break’ and ‘continue’  Example: int max(int a, int b) {if (a>b) return a; else return b;} or int max(int a, int b) {int m; if (a>b) m=a; else m=b; return m;} The type should match that of the function

48 Function call: pass by value
Effective parameters (variables) and formal parameters (variables) are completely different We just copy to or initialize the formal parameters from the effective parameters ‘effective parameters’ are ‘originals’, and ‘formal parameters’ are ‘photocopies’! They are different.

49 Pass by Reference

50 Review on Pass by Value:
#include <iostream> using namespace std; void Increment(int Number){ Number = Number + 1; cout << "The parameter Number: " << Number << endl; } void main(){ int I = 10; Increment(I); // parameter is a variable cout << "The variable I is: " << I << endl; Note: replacing ‘I’ by ‘Number’ in the main does not change anything.

51 Pass by Reference: Example 1
To show how the function affects a variable which is used as a parameter: #include <iostream> using namespace std; void Increment(int& Number){ Number = Number + 1; cout << "The parameter Number: " << Number << endl; } void main(){ int I = 10; Increment(I); // parameter is a variable cout << "The variable I is: " << I << endl;

52 Passing Parameters by Reference
To have a function with multiple outputs, we have to use pass by reference. Efficiency for large objects! Reference (address) of parameter is passed to the function, instead of its value. If the function changes the parameter value, the changes will be reflected in the program calling it.

53 Specify the passing of a parameter by reference: T&
<type>& <variable>, … int& x, double& y, char& z The notation T& means a reference to T Very ‘interesting’ notation we will continue to see it later on … ‘reference’ does not exist in C, ramification of low-level pointer. A reference is an alternative name for an object. int y=10; int& x = y; // x and y refer to the same int

54 Pass by value vs. by reference
Pass by value: formal parameters and arguments are different variables. ideal desirable behavior (but not efficient some times) Pass by reference: they are the same variables, but different names! should carefully handled!

55 Example 2 It is possible to use both pass by reference and pass by value parameters in the same function.

56 cout << "Enter two numbers: "; cin >> x >> y;
// Print the sum and average of two numbers // Input: two numbers x & y // Output: sum - the sum of x & y // average - the average of x & y #include <iostream> using namespace std; void SumAve (double, double, double&, double&); void main ( ) { double x, y, sum, mean; cout << "Enter two numbers: "; cin >> x >> y; SumAve (x, y, sum, mean); cout << "The sum is " << sum << endl; cout << "The average is " << mean << endl; } void SumAve(double no1, double no2, double& sum, double& average) { sum = no1 + no2; average = sum / 2;

57 Data areas after call to SumAve:

58 Example 3: sort 3 integers
Input: three numbers First, Second, and Third Output: three numbers in order! Sort 3 numbers, F, S, and T Sort 2 numbers, F,S Sort 2 numbers, S,T

59 Input: three numbers First, Second, and Third
Output: three numbers in order! Sort F, S, and T Sort F,S Sort S,T if (first > second) swap (first, second);

60 // Compare and sort three integers
#include <iostream> using namespace std; void swap (int&, int&); void main ( ) { int first, second, third; // input integers // Read in first, second and third. cout << "Enter three integers: "; cin >> first >> second >> third; if (first > second) swap (first, second); if (second > third) swap (second, third); cout << "The sorted integers are " << first << " , " << second << " , " << third << endl; }

61 // Function for swapping two integers
void swap (int& x, int& y) { int temp; temp = x; x = y; y = temp; }

62 Also possible … #include <iostream> using namespace std;
void sort (int&, int&); Void swap (int&, int&); void main ( ) { int first, second, third; // input integers // Read in first, second and third. cout << "Enter three integers: "; cin >> first >> second >> third; sort(first,second); sort(second,third); sort(first,third); cout << "The sorted integers are " << first << " , " << second << " , " << third << endl; } void sort(int& x, int& y) { if(x>y) swap(x,y); } void swap (int& x, int& y) {

63 Summary int main() int max(int x, int y) {
int xx,yy,mm; // OK with m, x, and y cin >> xx >> yy; mm=max(xx,yy); // functional programming style int xx,yy,mm; max(xx,yy,mm); int xx,yy,mmin,mmax; minmax(xx,yy,mmin,mmax); int max(int x, int y) { int m; if (x>y) m=x; else m=y; return m; } void max(int x, int y, int& m) { void minmax(int x, int y, int& min, int& max) { if (x>y) {min=y; max=x;} else (min=x; max=y;}

64 Example 4: programme tracing
// Pass-by-reference versus pass-by-value example #include <iostream> using namespace std; void One (int a, int b, int& c) { int d; a = 10; b = 11; c = 12; d = 13; cout << "The values of a, b, c, and d in One: "; cout << a << " " << b << " " << c << " " << d << endl; } void Two (int a, int b, int& d) { int c = 0; cout << "The values of a, b, c, and d in Two: ";

65 void main () { int a = 1, b = 2, c = 3, d = 4; One(a, b, c); cout << "The values of a, b, c, and d after One: "; cout << a << " " << b << " " << c << " " << d << endl; Two(a, b, d); cout << "The values of a, b, c, and d after Two: "; }


Download ppt "Functions."

Similar presentations


Ads by Google