1 / 49

Programming in C++

Andreas Savva. Programming in C++. Lecture Notes 6 Void Functions (Procedures). The manager of a company. Does he pay the bills? Does he answer the phone? Does he clean the office?. Division of Labor. PAY THE BILLS. CLEAN THE OFFICE. ANSWER THE PHONE. Our programs until now ….

chad
Download Presentation

Programming in C++

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Andreas Savva Programming inC++ Lecture Notes 6 Void Functions (Procedures)

  2. The manager of a company • Does he pay the bills? • Does he answer the phone? • Does he clean the office?

  3. Division of Labor PAYTHE BILLS CLEAN THE OFFICE ANSWER THE PHONE

  4. Our programs until now … • Display messages • Read values for the variables • Calculate expressions • Display results

  5. It would be good … • A part to read input values, • another to calculate results, and • another to display the results

  6. Functions(Subprograms) • Self-contained routines that are identified by a name and have the same structure as main(). • They are executed when they are called (calling their names). • main() is also a function but a special one.

  7. Building my house • I will call: • the architect • thebuilder • theironmonger • thebuilder • theplumber • the electrician • the builder • thepainter • thecarpenter • the designer

  8. Procedures(void Functions) • Functions return a value (see later). • If a function is declared a void it does not return a value, and is called a procedure. • void is a special data-type. int main() { ……… return 0; } void main() { ……… }

  9. Reasons for usingSub-programs • Decrease the size of the program • Program becomes more readable • Decrease of errors

  10. Top-Down Design Include libraries #include <iostream> using namespace std; // Global declaration section ……… void One() { ……… } float Two(float x) { ……… return ??; } void main() { ……… } Declaration section and function definition Main program

  11. Procedure structure void <function name>(<Formal parameters>) { . . . }

  12. Example: * * * * * ******* * * * * * * ******* * * * * * ******* * * * * * * ******* * *** ***** ******* * * * * *** ***** ******* * * * #include <iostream> using namespace std; voidBuilder() { cout << ” * ” << endl; cout << ” * * ” << endl; cout << ” * * ” << endl; cout << ”*******” << endl; cout << ”* *” << endl; cout << ”* *” << endl; cout << ”* *” << endl; cout << ”*******” << endl; cout << endl; } voidGardener(){ cout << ” * ” << endl; cout << ” *** ” << endl; cout << ” ***** ” << endl; cout << ”*******” << endl; cout << ” * ” << endl; cout << ” * ” << endl; cout << ” * ” << endl; cout << endl; } void main(){ Builder(); Builder(); Gardener(); Gardener(); } Executed when we call them

  13. Prototypes #include <iostream> using namespace std; // Global declaration section void one(); float two(float x); ……… int main() { ……… return 0; } void one() { ……… } float two(float x) { ……… return ??; } Prototypes

  14. Exercise 1 • Write the following procedures: • Line – to display 5stars in one line, i.e. ***** • Ex – to display an X of stars, i.e. * * * * * * * * * • Write the main program to display the following shape: ***** * * * * * * * * * * * * * * * * * * *****

  15. Exercise 2 • Write a procedure called “Display”that will prompt the user to enter two integer numbers nandm, and will display all the numbers from nuntil m. i.e. If n = 4,m = 9it will display: 4 5 6 7 8 9 Also, write the main program to call the procedure “Display”. • Draw the flowchart of the above program.

  16. Exercise 3 • Write a procedure called “Sum”that will prompt the user to enter two integer numbers nandm, and will display the sum of all the numbers from nuntil m. i.e. If n = 2, m = 6it will display: 20 since 2 + 3 + 4 + 5 + 6 = 20

  17. Programming inC++ Lecture Notes 7 Value Parameters Andreas Savva

  18. Formal parameters Actual parameters #include <iostream> using namespace std; voidShow (int a, int b, float c) { cout << a << ’ ’ << b << ’ ’ << c; } void main() { Show (5 , 2 , 6); } Parameters (Arguments) • Formal parameters • Actual parameters 5 2 6

  19. Formal parameter Name Example void pets(int cats) { cout << ”I have” << cats << ” kittens\n”; } • Functions are executed when we call them: • pets(6); • pets(4+2*3); I have 6 kittens I have 10 kittens

  20. Memory Add sum x y Output Example #include <iostream> using namesapce std; void Add (int x,int y) { int sum = x + y; cout << x << ” + ” << y << ” = ” << sum); } void main() { Add(5, 2); Add(3*2, 16-2); } 6 5 14 2 20 7 5 + 2 = 7 6 + 14 = 20

  21. Main Program Start Readn,m Add(x,y) Entrance Add(n,m) sum = x + y Finish Display sum Exit Flowchart #include <iostream> using namespace std; void Add (int x,int y) { int sum = x + y; cout << x << ’ + ’ << y << ’ = ’ << sum; } void main() { int n, m; cout << ”Enter two numbers: ”; cin >> n >> m; Add(n, m); }

  22. Exercise 1 • Write a program to ask for the price of a product and display the discount. The discount which is 15% will be calculated and displayed in the procedure “Discount”, that will take the price as a value formal parameter. • Draw the flowchart of the above program.

  23. Exercise 2 • Write a program that will ask the user to enter the base and height of a right-angle triangle and will display its area. The area will be calculated and displayed in the procedure “Area”, that will take the base and height as value formal parameters. Area = (base x height) / 2

  24. Exercise 3 • Write a procedure called “Times” that will take an integer number n and a character and it will display the character n times, i.e. Times(5,’?’)will display: ????? Times(8,’Α’)will display: ΑΑΑΑΑΑΑΑ • Also, write the program that will prompt for the number and the character and will call the procedure “Times”.

  25. Exercise 4 • Write a procedure called “Display”that will take two integer numbers nandm, and will display all the numbers from nuntil m. i.e. Display(4,9) will display: 4 5 6 7 8 9 Also, write the program that will ask the user to enter the two numbers and call the procedure “Display”. • Draw the flowchart of the above program.

  26. Exercise 5 • Write a procedure called “Even”that will take two integer numbers nandm, and will display all the even numbers from nuntil m. i.e. Even(4,13) will display: 4 6 8 10 12 Also, write the program that will ask the user to enter the two numbers and call the procedure “Even”.

  27. Exercise 6 • Write a procedure called “Line”that will take a character ch and an integer number nand will display ch in line n, i.e. Line(’?’,5) will display: line 1 line 2 line 3 line 4 line 5 line 6 line 7 ?

  28. Exercise 7 • Write a procedure called “Position”that will take a character ch and two integer numbers nand m and will display ch in row n, and column m, i.e. Position(’A’,4,7) will display: 123456789 1 2 3 4 5 6 A

  29. Exercise 8 • Write a procedure called “Sum”that will take two integer numbers nandm, and will display the sum of all the numbers from nuntil m. i.e. Sum(2,6) will display: 20 since 2 + 3 + 4 + 5 + 6 = 20

  30. Andreas Savva Programming inC++ Lecture Notes 8 Reference Parameters

  31. Formal parameters Actual parameters #include <iostream> using namespace std; voidFirst (inta, int b, float c) { . . . } void main() { int a = 1, b = 3, c = 7; . . . First (5 , c , a); } Parameters (Arguments) • Formal • Actual

  32. Value Formal parameter Referenceformal parameter Formal parameters • Value formal parameters • Reference (Variable) formal parameters void Display (int x, int&y) { x = x + y; y = x + y; } Display(3, Num);

  33. Display Memory x y Global x y z Output Example: #include <iostream> using namespace std; int x, y, z; void Display (int x, int&y) { x = x + y; y = x + y; z = z + 1; cout << x << ” ” << y << ” ” << z << ’\n’; } void main() { x = 2; y = 5; z = 3; Display(y, x); cout << x << ” ” << y << ” ” << z; } 7 5 9 2 9 5 4 3 2 7 9 4 9 5 4

  34. Display Memory x y Global x y z Output THE CORRECT WAY– Same example The reference formal parameter is a pointer to the address in memory of the actual parameter. #include <iostream> using namespace std; int x, y, z; void Display (int x, int&y) { x = x + y; y = x + y; z = z + 1; cout << x << ” ” << y << ” ” << z << ’\n’; } void main() { x = 2; y = 5; z = 3; Display(y, x); cout << x << ” ” << y << ” ” << z; } 7 5 9 5 4 3 2 7 9 4 9 5 4

  35. Display Memory x y Global Num Output Reference formal parameter: This example will help you to understand. #include <iostream> using namespace std; int Num; void Display (int&x, int&y) { x = x + y; y = x + y; } void main() { Num = 10; Display(Num, Num); cout << Num; } 20 40 10 40

  36. Display Memory x y Global Num Output Exercise: #include <iostream> using namespace std; int Num; void Display (int x, int&y) { x = x + y; y = x + y; } void main() { Num = 10; Display(Num, Num); cout << Num; } 10 20 10 30 30

  37. Display Memory x y Global Num Output Exercise: #include <iostream> using namespace std; int Num; void Display (int x, int y) { x = x + y; y = x + y; } void main() { Num = 10; Display(Num, Num); cout << Num; } 10 10 20 30 10 10

  38. Passing Parameters by Reference void add(int a, int b, int &c) { c = a + b; } void main() { int x=3, y=5, z; add(x, y, z); cout << z; } • Only in C++ • In C and C++ void add(int a, int b, int *c) { *c = a + b; } void main() { int x=3, y=5, z; add(x, y, &z); cout << z; }

  39. LocalGlobal One Two main #include <iostream> using namespace std; const intPI = 3.14159; floatx, y; voidone(intnum) { int n, m; char c; ……… } voidtwo(float&x) { intz; chary; ……… } void main() { int p, y; ……… } Sub-programs • Variables • Local • Global num, n, m, c PI, x, y x, y, z PI p, y PI, x

  40. Exercise1 • Write a void function “Calculate”that will take two numbersαandβand a character ch, and it will return through a reference formal parameter called “result” the following which depends on the value of ch: chresult ’+’α + β ’–’α – β ’*’α * β ’/’α / β otherwise 0

  41. Exercise 2 • Write a procedure “Swap” that will accept two integer numbers, swap and return their values.

  42. Exercise 3 • Write a procedure “Summation” that will take two integer numbers n and m and return through a reference formal parameter the sum of all the numbers from n until m. If n > m then the sum should be zero. i.e. • If n = 1 and m = 4 then Sum = 1 + 2 + 3 + 4 = 10 • If n = 4 and m = 9 then Sum = 4 + 5 + 6 + 7 + 8 + 9 = 39 • If n = 7 and m = 7 then Sum = 7 • If n = 7 and m = 2 then Sum = 0

  43. Exercise 4 • Write a procedure “Money” that will take a an amount in pounds (real number), and it would return through two integer reference formal parameters the number of pounds and the number of cents. i.e. if amount = 36.78 then pounds = 36 and cents = 78

  44. Exercise 5 • Write a procedure “Euro” that will accept an amount in Cyprus pounds and return the respective amount in EURO. The rate should also be passed to the procedure as a value formal parameter.

  45. Exercise 6 • Given the names of four students and three exam-marks for each one: • Write a procedure to take three marks, calculate and return their average and the highest of the three. • Write the main program to read the four students names and marks and display a list with their names, their average mark, and the highest mark for each student.

  46. 6 8 1 10 3 10 Default Parameters #include <iostream> using namespace std; void print(int n = 1, int m = 10) { cout << n << ’\t’ << m; } void main() { print(6,8); print(); print(3); }

  47. Right-to-Left void print(int n = 10, int m); //ERROR void print(int x = 1, int y = 2, int z = 3); . . . print(7,5); //CORRECT print( , , 6); //ERROR Default Parameters are defined only ONCE #include <iostream> using namespace std; void print(int n, int m = 10); void main() { print(6,8); print(3); } void print(int n, int m) { cout << n << ’\t’ << m; } #include <iostream> using namespace std; void print(int n, int m); void main() { print(6,8); print(3); } void print(int n, int m = 10) { cout << n << ’\t’ << m; } ERROR HERE: print takes two parameters

  48. 6 ? 3 8 2 4.2 Overloading Functions #include <iostream> using namespace std; void display(int n) { cout << n << endl; } void display(char c) { cout << c << endl; } void display(int x, int y) { cout << x << ’\t’ << y << endl; } void display(int n, float m){ cout << n << ’\t’ << m << endl; } void main() { display(6); display(’?’); display(3,8); display(2,(float)4.2); } • A function can be defined more than ones but with different number or/and type of parameters.

  49. More about Functions • Functions can also be called as procedures (the return value will be lost). int max (int a, int b) { cout << a+b << endl; if (a>b) return a; else return b; } int main() { cout << max(5,9) << endl; max(3,1); // Return value is lost } C++ will give a warning (not an error): main() does not return a value

More Related