Download presentation
Presentation is loading. Please wait.
Published byClaud Parks Modified over 8 years ago
1
Programming Principles II Lecture Notes 3.1 Void Functions Andreas Savva
2
2 Sub-programs – Division of Labor The manager of a company Does he pay the bills? Does he pay the bills? Does he answer the phone? Does he answer the phone? Does he clean the office? Does he clean the office? Clean the office Pay the bills Answer the phone
3
3 Functions (Subprograms) Self-contained routines that are identified by a name and have the same structure as main(). 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). They are executed when they are called (calling their names). main() is also a function but a special one. main() is also a function but a special one.
4
4 Building my house I will call: the architect builder the builder the ironmonger builder the builder the plumber the electrician builder the builder the painter the carpenter the designer
5
5 Procedures (void Functions) Functions return a value (see later). Functions return a value (see later). If a function is declared a void it does not return a value, and is called a procedure. If a function is declared a void it does not return a value, and is called a procedure. void is a special data-type. void is a special data-type. void main() { ……… } int main() { ……… return 0; }
6
6 Reasons for using Sub-programs Decrease the size of the program Decrease the size of the program Program becomes more readable Program becomes more readable Decrease of errors Decrease of errors
7
7 Top-Down Design #include using namespace std; // Global declaration section ……… void One() { ……… } float Two(float x) { ……… return ??; } void main() { ……… } Declaration section and function definition Main program Include libraries
8
8 Procedure structure void ( ) {...... }
9
9 Example: #include #include using namespace std; using namespace std; void Builder() { void Builder() { cout << ” * ” << endl; cout << ” * ” << endl; cout << ” * * ” << endl; cout << ” * * ” << endl; cout << ”*******” << endl; cout << ”*******” << endl; cout << ”* *” << endl; cout << ”* *” << endl; cout << ”*******” << endl; cout << ”*******” << endl; cout << endl; cout << endl; } void Gardener() { void Gardener() { cout << ” * ” << endl; cout << ” * ” << endl; cout << ” *** ” << endl; cout << ” *** ” << endl; cout << ” ***** ” << endl; cout << ” ***** ” << endl; cout << ”*******” << endl; cout << ”*******” << endl; cout << ” * ” << endl; cout << ” * ” << endl; cout << endl; cout << endl; } void main() { void main() { Builder(); Builder(); Gardener(); Gardener(); } * * * * * ******* * * ******* * * * * * ******* * * ******* * *** *** ***** ************ * * * * *** *** ***** ************ * * * Executed when we call them
10
10 Example: #include #include using namespace std; using namespace std; void Builder() { void Builder() { cout << ” * ” << endl; cout << ” * ” << endl; cout << ” * * ” << endl; cout << ” * * ” << endl; cout << ”*******” << endl; cout << ”*******” << endl; cout << ”* *” << endl; cout << ”* *” << endl; cout << ”*******” << endl; cout << ”*******” << endl; cout << endl; cout << endl; } void Gardener() { void Gardener() { Builder(); Builder(); cout << ” * ” << endl; cout << ” * ” << endl; cout << ” *** ” << endl; cout << ” *** ” << endl; cout << ” ***** ” << endl; cout << ” ***** ” << endl; cout << ”*******” << endl; cout << ”*******” << endl; cout << ” * ” << endl; cout << ” * ” << endl; cout << endl; cout << endl; } void main() { void main() { Gardener(); Gardener(); } * * * * * ******* * * ******* * *** *** ***** ************ * * * * * * * * ******* * * ******* * *** *** ***** ************ * * *
11
11 Prototypes #include using namespace std; // Global declaration section void one(); float two(float x); ……… int main() { ……… return 0; } void one() { ……… } float two(float x) { ……… return ??; } Prototypes
12
12 Exercise 1 Write the following void functions: Write the following void functions: Line – to display 5 stars in one line, i.e. Line – to display 5 stars in one line, i.e. ***** Ex – to display an X of stars, i.e. Ex – to display an X of stars, i.e. * * * Write the main program to display the following shape: Write the main program to display the following shape:***** * * * * *****
13
13 Exercise 2 Write a void function called “Display” that will prompt the user to enter two integer numbers n and m, and will display all the numbers from n until m. i.e. Write a void function called “Display” that will prompt the user to enter two integer numbers n and m, and will display all the numbers from n until m. i.e. If n = 4, m = 9 it will display: 4 5 6 7 8 9 Also, write the main program to call the procedure “Display”.
14
14 Exercise 3 Write a void function called “Sum” that will prompt the user to enter two integer numbers n and m, and will display the sum of all the numbers from n until m. i.e. Write a void function called “Sum” that will prompt the user to enter two integer numbers n and m, and will display the sum of all the numbers from n until m. i.e. If n = 2, m = 6 it will display: 20 since 2 + 3 + 4 + 5 + 6 = 20
15
Programming Principles II Lecture Notes 3.2 Value Parameters Andreas Savva
16
16 #include using namespace std; void Show (int a, int b, float c) { cout << a << ’ ’ << b << ’ ’ << c; } void main() { Show (5, 2, 6); } Actual parameters Formal parameters Parameters (Arguments) Formal parameters Formal parameters Actual parameters Actual parameters 5 2 6
17
17 Example void pets( int cats) { cout << ”I have ” << cats << ” kittens\n”; }Name Formal parameter Functions are executed when we call them: pets(6);pets(6); pets(4+2*3);pets(4+2*3); I have 6 kittens I have 10 kittens
18
18 Memory Example #include using namespace 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); }Output 5 + 2 = 7 6 + 14 = 20sum x y Add52761420
19
19 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 void function called “Discount”, that will take the price as a value formal parameter. 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 void function called “Discount”, that will take the price as a value formal parameter.
20
20 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 void function “Area”, that will take the base and height as value formal parameters. 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 void function “Area”, that will take the base and height as value formal parameters. Area = (base x height) / 2
21
21 Exercise 3 Write a void function called “Times” that will take an integer number n and a character and it will display the character n times, i.e. Write a void function 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 function “Times”. Also, write the program that will prompt for the number and the character and will call the function “Times”.
22
22 Exercise 4 Write a procedure called “Display” that will take two integer numbers n and m, and will display all the numbers from n until m. i.e. Write a procedure called “Display” that will take two integer numbers n and m, and will display all the numbers from n until 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 function “Display”.
23
23 Exercise 5 Write a void function called “Even” that will take two integer numbers n and m, and will display all the even numbers from n until m. i.e. Write a void function called “Even” that will take two integer numbers n and m, and will display all the even numbers from n until 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 function “Even”.
24
24 Exercise 6 Write a void function called “Line” that will take a character ch and an integer number n and will display ch in line n, i.e. Write a void function called “Line” that will take a character ch and an integer number n and 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 ?
25
25 Exercise 7 Write a void function called “Position” that will take a character ch and two integer numbers n and m and will display ch in row n, and column m, i.e. Write a void function called “Position” that will take a character ch and two integer numbers n and m and will display ch in row n, and column m, i.e. Position(’A’,4,7) will display: 123456789 123456789123456 A
26
26 Exercise 8 Write a void function called “Sum” that will take two integer numbers n and m, and will display the sum of all the numbers from n until m. i.e. Write a void function called “Sum” that will take two integer numbers n and m, and will display the sum of all the numbers from n until m. i.e. Sum(2,6) will display: 20 since 2 + 3 + 4 + 5 + 6 = 20
27
Programming Principles II Lecture Notes 3.3 Reference Parameters Andreas Savva
28
28 #include using namespace std; void First (int a, int b, float c) {... } void main() { int a = 1, b = 3, c = 7;... First (5, c, a); } Actual parameters Formal parameters Parameters (Arguments) Formal Formal Actual Actual
29
29 Formal parameters Value formal parameters Value formal parameters Reference (Variable) formal parameters Reference (Variable) formal parameters void Display (int x, int &y) { x = x + y; y = x + y; } Display(3, Num); ValueFormalparameter Reference formal parameter
30
30 Memory Global x y z Display x y Example: #include 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; } 52Output 7 9 4 9 5 4 79 2 5394
31
31 Memory Global x y z Display x y THE CORRECT WAY – Same example #include 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; } 5Output 7 9 4 9 5 4 7 2 5394 The reference formal parameter is a pointer to the address in memory of the actual parameter.
32
32 Memory Num Global Reference formal parameter: #include 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; }Output 40 102040 Display x y This example will help you to understand.
33
33 Memory Num Global Exercise: #include 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; }Output 30 10 x y Display30 1020
34
34 Memory Num Global Exercise: #include 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; }Output 10 10 x y Display10 30 10 20
35
35 Only in C++ Only in C++ In C and 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; } 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; } Passing Parameters by Reference
36
36 Sub-programs #include using namespace std; const int PI = 3.14159; float x, y; void one(int num) { int n, m; char c; ……… } void two(float &x) { int z; char y; ……… } void main() { int p, y; ……… } Variables Variables Local Local Global Global One Two main Local Global num, n, m, cPI, x, y x, y, zPI p, yPI, x
37
Global Variables 37 #include using namespace std; int x = 0; void Display () { x = x + 1; cout << x << endl; } int main() { for (int i=0; i<5; i++) Display(); return 0; } 1234512345
38
Local Variables 38 #include using namespace std; void Display () { int x = 0; x = x + 1; cout << x << endl; } int main() { for (int i=0; i<5; i++) Display(); return 0; } 1111111111
39
Static Variables 39 #include using namespace std; void Display () { static int x = 0; x = x + 1; cout << x << endl; } int main() { for (int i=0; i<5; i++) Display(); return 0; } 1234512345 This line is executed only the first time and the variable stays in memory
40
Prototypes 40 #include using namespace std; &; void Display (int x, int &y); int main() { int Num = 10; Display(Num, Num); cout << Num; return 0; } & void Display (int x, int &y) { x = x + y; y = x + y; } #include using namespace std; &; void Display (int, int&); int main() { int Num = 10; Display(Num, Num); cout << Num; return 0; } & void Display (int x, int &y) { x = x + y; y = x + y; }
41
The return keyword The reserved word return when executed, it causes the flow of control to immediately exit the function. The reserved word return when executed, it causes the flow of control to immediately exit the function. 41 void divide( int a, int b) { if (b==0) cout << ”Division by zero”; else cout << b << ” goes to ” << a << ” ” << a / b << ”times”; } void divide( int a, int b) { if (b==0) { cout << ”Division by zero”; return; } cout << b << ” goes to ” << a << ” ” << a / b << ”times”; } same
42
42 Exercise 1 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: 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 ’+’α + β ’–’α – β ’*’α * β ’/’α / β otherwise0
43
43 Exercise 2 Write a void function “Swap” that will accept two integer numbers, swap and return their values. Write a void function “Swap” that will accept two integer numbers, swap and return their values.
44
44 Exercise 3 Write a void function “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. Write a void function “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 = 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 = 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 = 7 then Sum = 7 If n = 7 and m = 2 then Sum = 0 If n = 7 and m = 2 then Sum = 0
45
45 Exercise 4 Write a void function “Money” that will take a an amount in pounds (real number), and it would return through two integer reference formal parameter the number of pounds and the number of cents. Write a void function “Money” that will take a an amount in pounds (real number), and it would return through two integer reference formal parameter the number of pounds and the number of cents.i.e. if amount = 36.78 then pounds = 36 and cents = 78
46
46 Exercise 5 Write a void function “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. Write a void function “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.
47
47 Exercise 6 Given the names of four students and three exam-marks for each one: Given the names of four students and three exam-marks for each one: 1. Write a void function to take three marks, calculate and return their average and the highest of the three. 2. 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.
48
48 Arrays as parameters Arrays are pointers Arrays are pointers Passed by reference Passed by reference void init(int Y[10]){ for (int i=0; i<10; i++) Y[i] = i + 1; } void change(int X[]){ X[3] = 99; *(X+5) = 45; } void print(int *p){ for (int i=0; i<10; i++) cout << p[i] << endl; } void main(){ int A[10]; init(A); change(A); print(A); } 1 2 3 99 5 45 7 8 9 10 A 0123456789A123456789100123456789A12399545789100123456789
49
49 2D-Arrays as parameters void init(int Y[10][2]){ for (int i=0; i<10; i++){ Y[i][0] = i + 10; Y[i][1] = i + 20; } void print(int A[][2]){ for (int i=0; i<10; i++) cout << A[i][0] << ’-’ << A[i][1] << endl; } void change(int *p){ p[0] = 0; *(p+1) = 1; } void main(){ int A[10][2]; init(A); change(A[4]); change(A[8]); print(A); } 10-20 11-21 12-22 13-23 0-1 15-25 16-26 17-27 0-1 19-29 A 0 1 0123456789 1011121314151617181920212223242526272829 1010
50
50 Arrays A12345678910 0123456789 B 0 11121314151617181920 141424344454647484950 0123456789 int *p = A; p++; cout << *p << endl; p = &A[5]; cout << *p << endl; p += 3; cout << *p << endl; p = *B; cout << *(p+5) << endl; p = *(B+5); cout << *p << endl; p = B[7]; cout << *(p+2) << endl; p = &B[8][1]; cout << *p << endl; p++; cout << p[0] << endl;p 2 6 9 43 16 19 49 20
51
51 Strings char s[] = ”I come from Cyprus”; char *p = &s[10]; cout << *p << endl; cout << p[2] << endl; cout << p << endl; char *q = new char[5]; for (int i=2; i<6; i++) q[i-2] = p[i]; q[4] = ’\0’; cout << q << endl; m C m Cyprus Cypr
52
52 Multidimensional Arrays as parameters void print3D(int A[][6][3], int n) { for (int i=0; i<n; i++) { for (int j=0; j<6; j++) { for (int k=0; k<3; k++) cout << A[i][j][k] << ’\t’; cout << endl; } cout << endl << endl; } void print2D(int A[][3], int n) { for (int i=0; i<n; i++) { for (int j=0; j<6; j++) cout << A[i][j][k] << ’\t’; cout << endl; } void print1D(int A[], int n) { for (int i=0; i<n; i++) cout << A[i][j][k] << ’\t’; } void main() { int A[10][6][3];... print3D(A,10); print2D(A[3],6); print1D(A[7][4],3); }
53
53 Memory Classes as Value Parameters class Fraction { public: int numerator; int denominator; }; void change(Fraction f) { f.numerator = 1; f.denominator = 5; } void main() { Fraction y = {6,12}; change(y); cout << y.numerator << ’/’ << y.denominator; }main y 12 6 f 12 6 change 1 5 6/12
54
54 Memory Classes as Reference Parameters class Fraction { public: int numerator; int denominator; }; void change(Fraction &f) { f.numerator = 1; f.denominator = 5; } void main() { Fraction y = {6,12}; change(y); cout << y.numerator << ’/’ << y.denominator; }main y 12 6 1 5 1/5 change f
55
55 Stack Memory main y 6 Classes with Pointers as Parameters class Fraction { public: int numerator; int *denominator; }; void change(Fraction f) { f.numerator = 1; *f.denominator = 5; } void main() { Fraction y = {6}; y.denominator = new int(12); change(y); cout << y.numerator << ’/’ << *y.denominator; } 6/5 ?? 12 f 6 change 5 1
56
56 Default Parameters #include using namespace std; void print(int n = 1, int m = 10) { cout << n << ’\t’ << m << endl; } void main() { print(6,8); print(); print(3); } 6 8 1 10 3 10
57
57 Default Parameters are defined only ONCE #include using namespace std; void print(int, int = 10); void main() { print(6,8); print(3); } void print(int n, int m) { cout << n << ’\t’ << m; } #include using namespace std; void print(int, int); void main() { print(6,8); print(3); } void print(int n, int m = 10) { cout << n << ’\t’ << m; } ERROR HERE: print takes two parameters 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
58
58 Overloading Functions #include 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); } 6 ? 3 8 2 4.2 A function can be defined more than ones but with different number or/and type of parameters. A function can be defined more than ones but with different number or/and type of parameters.
59
59 The void parameter void display(void) { cout << ”Welcome to C++” << endl; } void display() { cout << ”Welcome to C++” << endl; } same
60
60 More about Functions Functions can also be called as procedures (the return value will be lost). Functions can also be called as procedures (the return value will be lost). int max (int a, int b) { cout << a+b << endl; cout << a+b << endl; if (a>b) return a; if (a>b) return a; else return b; else return b;} int main() { cout << max(5,9) << endl; cout << max(5,9) << endl; max(3,1); // Return value is lost max(3,1); // Return value is lost} C++ will give a warning (not an error): main() does not return a value
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.