|
@@ -55,37 +55,44 @@ The first sentence of a function is called the *header* and its structure is as
|
55
|
55
|
|
56
|
56
|
For example,
|
57
|
57
|
|
58
|
|
-`int example(int var1, float var2, char &var3)`
|
|
58
|
+```cpp
|
|
59
|
+int example(int var1, float var2, char &var3)
|
|
60
|
+```
|
59
|
61
|
|
60
|
62
|
would be the header of the function called `example`, which returns an integer value. The function receives as arguments an integer value (and will store a copy in `var1`), a value of type `float` (and will store a copy in `var2`) and the reference to a variable of type `char` that will be stored in the reference variable `var3`. Note that `var3` has a & symbol before the name of the variable. This indicates that `var3` will contain the reference to a character.
|
61
|
63
|
|
62
|
|
-### Calling
|
|
64
|
+### Calling a function
|
63
|
65
|
|
64
|
|
-If we want to store the value of the result of the function called `example` in a variable `result` (that would be of type integer), we call the function by passing arguments as follows:
|
|
66
|
+The following line assigns the value returned by the function `example` to an integer variable `x`:
|
65
|
67
|
|
66
|
|
-`result=example(2, 3.5, unCar);`
|
|
68
|
+```cpp
|
|
69
|
+x = example(2, 3.5, unCar);
|
|
70
|
+```
|
67
|
71
|
|
68
|
|
-Note that as the function is called, you don't include the type of the variables in the arguments as in the definition for the function `example`. The third parameter `&var3` is a reference variable; what is being sent to the third argument when calling the function is a *reference* to the variable `unCar`. Any changes that are made on the variable `var3` will change the contents of the variable `unCar`.
|
|
72
|
+Note that when calling the function we don't include the type of the variables in the arguments as in the function's definition. The third parameter `&var3` is a reference variable. Thus, what is being sent to the third argument when calling the function is a *reference* to the variable `unCar`. Any assignments that are made to the variable `var3` will change the contents of the variable `unCar`.
|
69
|
73
|
|
70
|
|
-You can also use the function's result without having to store it in a variable. For example you could print it:
|
|
74
|
+You can also use the function's result without having to store it in a variable.
|
71
|
75
|
|
72
|
|
-`cout << "The result of the function example is:" << example(2, 3.5, unCar);`
|
|
76
|
+The following line sends the return value of function `example` to the `cout` object, so that it is displayed in the terminal:
|
73
|
77
|
|
74
|
|
-or use it in an arithmetic expression:
|
|
78
|
+```cpp
|
|
79
|
+cout << "The result of the function example is:" << example(2, 3.5, unCar);`
|
|
80
|
+```
|
75
|
81
|
|
76
|
|
-`y=3 + example(2, 3.5, unCar);`
|
|
82
|
+or use it in an arithmetic expression:
|
77
|
83
|
|
|
84
|
+```cpp
|
|
85
|
+y = 3 + example(2, 3.5, unCar);
|
|
86
|
+```
|
78
|
87
|
|
79
|
88
|
|
80
|
89
|
### Overloaded Functions
|
81
|
90
|
|
82
|
|
-Overloaded functions are functions that have the same name, but a different *signature*.
|
83
|
|
-
|
84
|
|
-The signature of a function is composed of the name of the function, and the types of parameters it receives, but does not include the return type.
|
|
91
|
+Overloaded functions are functions that have the same name, but different *signatures*. The signature of a function is composed of the name of the function and the types of parameters it receives. The signature does not include the return type.
|
85
|
92
|
|
86
|
|
-The following function prototypes have the same signature:
|
|
93
|
+The following function prototypes have the **same signature**:
|
87
|
94
|
|
88
|
|
-```
|
|
95
|
+```cpp
|
89
|
96
|
int example(int, int) ;
|
90
|
97
|
void example(int, int) ;
|
91
|
98
|
string example(int, int) ;
|
|
@@ -93,9 +100,9 @@ string example(int, int) ;
|
93
|
100
|
|
94
|
101
|
Note that each has the same name, `example`, and receives the same amount of parameters of the same type `(int, int)`.
|
95
|
102
|
|
96
|
|
-The following function prototypes have different signatures:
|
|
103
|
+The following function prototypes have **different signatures**:
|
97
|
104
|
|
98
|
|
-```
|
|
105
|
+```cpp
|
99
|
106
|
int example(int) ;
|
100
|
107
|
int elpmaxe(int) ;
|
101
|
108
|
```
|
|
@@ -104,7 +111,7 @@ Note that even though the functions have the same amount of parameters with the
|
104
|
111
|
|
105
|
112
|
The following function prototypes are overloaded versions of the function `example`:
|
106
|
113
|
|
107
|
|
-```
|
|
114
|
+```cpp
|
108
|
115
|
int example(int) ;
|
109
|
116
|
void example(char) ;
|
110
|
117
|
int example(int, int) ;
|
|
@@ -165,7 +172,7 @@ Here `var2` is initialized to 5 and `var3` to 10.
|
165
|
172
|
|
166
|
173
|
##DVD Movies and the DVD Data Base
|
167
|
174
|
|
168
|
|
-DVD stands for "digital versatile disk" or "digital video disk", which is an optical disc format for storing digital information invented by Philips, Sony, Toshiba, and Panasonic in 1995. The DVD offers larger storage capacity than compact disks (CD), but has the same dimensions. DVDs can be used to store any kind of digital data, but they are famous for their use in the distribution of movies.
|
|
175
|
+DVD stands for "digital versatile disk" or "digital video disk", which is an optical disc format for storing digital information invented by Philips, Sony, Toshiba, and Panasonic in 1995. The DVD offers larger storage capacity than compact disks (CD) but has the same dimensions. DVDs can be used to store any kind of digital data but they are mainly famous for their use in the distribution of movies.
|
169
|
176
|
|
170
|
177
|
|
171
|
178
|
---
|
|
@@ -195,14 +202,18 @@ DVD stands for "digital versatile disk" or "digital video disk", which is an opt
|
195
|
202
|
|
196
|
203
|
## Laboratory Session:
|
197
|
204
|
|
198
|
|
-In this laboratory session we will be using a data base of DVD movies maintained by http://www.hometheaterinfo.com/dvdlist.htm. This database contains 44MB of information for movies that have been distributed in DVD. Some of the stored information in the database for each DVD is: DVD title, publishing studio, date of publication, type of sound, versions, price, rating, year and genre. The fields of the information for each movie are stored in text with the following format:
|
|
205
|
+In this laboratory session we will be using a database of DVD movies maintained by [http://www.hometheaterinfo.com/dvdlist.htm](http://www.hometheaterinfo.com/dvdlist.htm). This database contains 44MB of information for movies that have been distributed in DVD. Some of the stored information in the database for each DVD is: DVD title, publishing studio, date of publication, type of sound, versions, price, rating, year and genre. The information for each movie is stored in the following format:
|
199
|
206
|
|
200
|
|
-`DVD_Title|Studio|Released|Status|Sound|Versions|Price|Rating|Year|Genre|Aspect|UPC|DVD_ReleaseDate|ID|Timestamp`
|
|
207
|
+```
|
|
208
|
+DVD_Title|Studio|Released|Status|Sound|Versions|Price|Rating|Year|Genre|Aspect|UPC|DVD_ReleaseDate|ID|Timestamp
|
|
209
|
+```
|
201
|
210
|
|
202
|
211
|
For example,
|
203
|
212
|
|
204
|
213
|
```
|
205
|
|
-Airplane! (Paramount/ Blu-ray/ Checkpoint)|Paramount||Discontinued|5.1 DTS-HD|LBX, 16:9, BLU-RAY|21.99|PG|1980|Comedy|1.85:1|097361423524|2012-09-11 00:00:00|230375|2013-01-01 00:00:00
|
|
214
|
+Airplane! (Paramount/ Blu-ray/ Checkpoint)|Paramount||Discontinued|5.1 DTS-HD|
|
|
215
|
+LBX, 16:9, BLU-RAY|21.99|PG|1980|Comedy|1.85:1|097361423524|2012-09-11 00:00:00|
|
|
216
|
+230375|2013-01-01 00:00:00
|
206
|
217
|
```
|
207
|
218
|
|
208
|
219
|
|
|
@@ -244,17 +255,13 @@ In this exercise you will modify the `main` function and some of the pre-defined
|
244
|
255
|
|
245
|
256
|
#### Instructions
|
246
|
257
|
|
247
|
|
-1. In the file `main.cpp`, modify the `main` function so that the program displays the movies that have positions from 80 to 100.
|
248
|
|
-
|
249
|
|
-2. Now modify the `main` function so that the program displays only the movies that have “forrest gump” in the title.
|
250
|
|
-
|
251
|
|
-3. Once again, modify the `main` function so that the program displays only the movie in position 75125 using function composition and the function `showMovie`.
|
252
|
|
-
|
253
|
|
-4. For the movie in part 3 of this exercise, add the necessary code to the `main` function so that the program displays the name and the rating of the movie.
|
254
|
|
-
|
255
|
|
-5. Modify the `getMovieInfo` function in the file `movie.cpp` so that it receives an additional parameter by reference where the name of the movie will be assigned. Remember that you must also modify the function's prototype in `movie.h`.
|
256
|
|
-
|
257
|
|
-6. For the movie in part 3, add the necessary code to the `main` function so that, using `getMovieInfo`, it displays the name, rating, year and the genre of the movie in one line. Hint: note that the function `getMovieInfo` has parameters that are passed by reference.
|
|
258
|
+1. Perform the following implementations in the `main` function (in the `main.cpp` file):
|
|
259
|
+ * Add code so that the program displays the movies that have positions from 80 to 100.
|
|
260
|
+ * Add code so that the program displays only the movies that have “forrest gump” in the title.
|
|
261
|
+ * Add code so that the program displays only the movie in position 75125 using function composition and the function `showMovie`.
|
|
262
|
+ * Add code so that the program displays the name and the rating of the movie in position 75125.
|
|
263
|
+1. Modify the `getMovieInfo` function in the file `movie.cpp` so that it receives an additional parameter by reference where the name of the movie will be assigned. Remember that you must also modify the function's prototype in `movie.h`.
|
|
264
|
+1. Add the necessary code to the `main` function so that, using `getMovieInfo`, it displays the name, rating, year and the genre of the movie in position 75125 in one line. Hint: note that the function `getMovieInfo` has parameters that are passed by reference.
|
258
|
265
|
|
259
|
266
|
### Exercise 3 - Define and Implement Functions
|
260
|
267
|
|