|
|
|
|
1
|
-
|
|
|
2
|
# Using functions in C++ - DVD Info
|
1
|
# Using functions in C++ - DVD Info
|
3
|
|
2
|
|
4
|
|
3
|
|
|
|
|
|
9
|
|
8
|
|
10
|
A good way to organize and structure computer programs is dividing them into smaller parts using functions. Each function carries out a specific task of the problem that we are solving.
|
9
|
A good way to organize and structure computer programs is dividing them into smaller parts using functions. Each function carries out a specific task of the problem that we are solving.
|
11
|
|
10
|
|
12
|
-You've seen that all programs written in C++ must contain the `main` function where the program begins. You've probably already used functions such as `pow`, `sin`, `cos`, or `sqrt` from the `cmath` library. Since in almost all of the upcoming lab activities you will continue using pre-defined functions, you need to understand how to work with them. In future exercises you will learn how to design and validate functions. In this laboratory experience you will search and display information contained in a DVD data base to practice declaring simple functions and invoking pre-defined functions.
|
|
|
|
|
11
|
+You've seen that all programs written in C++ must contain the `main` function where the program begins. You've probably already used functions such as `pow`, `sin`, `cos`, or `sqrt` from the `cmath` library. Since in almost all of the upcoming lab activities you will continue using pre-defined functions, you need to understand how to work with them. In future exercises you will learn how to design and validate functions. In this laboratory experience you will search and display information contained in a DVD data base to practice declaring simple functions and calling pre-defined functions.
|
13
|
|
12
|
|
14
|
|
13
|
|
15
|
##Objectives:
|
14
|
##Objectives:
|
16
|
|
15
|
|
17
|
1. Identify the parts of a function: return type, name, list of parameters, and body.
|
16
|
1. Identify the parts of a function: return type, name, list of parameters, and body.
|
18
|
-2. Invoke pre-defined functions by passing arguments by value ("pass by value"), and by reference ("pass by reference").
|
|
|
|
|
17
|
+2. Call pre-defined functions by passing arguments by value ("pass by value"), and by reference ("pass by reference").
|
19
|
3. Implement a simple function that utilizes parameters by reference.
|
18
|
3. Implement a simple function that utilizes parameters by reference.
|
20
|
|
19
|
|
21
|
|
20
|
|
|
|
|
|
27
|
|
26
|
|
28
|
a. the basic elements of a function definition in C++
|
27
|
a. the basic elements of a function definition in C++
|
29
|
|
28
|
|
30
|
- b. how to invoke functions in C++
|
|
|
|
|
29
|
+ b. how to call functions in C++
|
31
|
|
30
|
|
32
|
c. the difference between parameters that are passed by value and by reference
|
31
|
c. the difference between parameters that are passed by value and by reference
|
33
|
|
32
|
|
|
|
|
|
59
|
|
58
|
|
60
|
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.
|
59
|
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
|
|
60
|
|
62
|
-###Invoking
|
|
|
|
|
61
|
+###Calling
|
63
|
|
62
|
|
64
|
-If we want to store the value of the `example` function's result in a variable `result` (that would be of type integer), we invoke the function by passing arguments as follows:
|
|
|
|
|
63
|
+If we want to store the value of the `example` function's result in a variable `result` (that would be of type integer), we call the function by passing arguments as follows:
|
65
|
|
64
|
|
66
|
`result=example(2, 3.5, unCar);`
|
65
|
`result=example(2, 3.5, unCar);`
|
67
|
|
66
|
|
68
|
-Note that as the function is invoked, 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 invoking 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`.
|
|
|
|
|
67
|
+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`.
|
69
|
|
68
|
|
70
|
You can also use the function's result without having to store it in a variable. For example you could print it:
|
69
|
You can also use the function's result without having to store it in a variable. For example you could print it:
|
71
|
|
70
|
|
|
|
|
|
117
|
In that last example, the function `example` is overloaded since there are 5 functions with different signatures but with the same name.
|
116
|
In that last example, the function `example` is overloaded since there are 5 functions with different signatures but with the same name.
|
118
|
|
117
|
|
119
|
|
118
|
|
120
|
-###Values by default
|
|
|
|
|
119
|
+### Default values
|
121
|
|
120
|
|
122
|
Values by default can be assigned to the parameters of the functions starting from the first parameter to the right. It is not necessary to initialize all of the parameters, but the ones that are initialized should be consecutive: parameters in between two parameters cannot be left uninitialized. This allows calling the function without having to send values in the positions that correspond to the initialized parameters.
|
121
|
Values by default can be assigned to the parameters of the functions starting from the first parameter to the right. It is not necessary to initialize all of the parameters, but the ones that are initialized should be consecutive: parameters in between two parameters cannot be left uninitialized. This allows calling the function without having to send values in the positions that correspond to the initialized parameters.
|
123
|
|
122
|
|
124
|
-**Examples of function headers and valid invocations:**
|
|
|
|
|
123
|
+**Examples of function headers and valid function calls:**
|
125
|
|
124
|
|
126
|
1. **Headers:** `int example(int var1, float var2, int var3 = 10)` Here `var3` is initialized to 10.
|
125
|
1. **Headers:** `int example(int var1, float var2, int var3 = 10)` Here `var3` is initialized to 10.
|
127
|
|
126
|
|
128
|
- **Invocations:**
|
|
|
|
|
127
|
+ **Calls:**
|
129
|
|
128
|
|
130
|
a. `example(5, 3.3, 12)` This function call assigns the value 5 to `var1`, the value 3.3 to `var2`, and the value of 12 to `var3`.
|
129
|
a. `example(5, 3.3, 12)` This function call assigns the value 5 to `var1`, the value 3.3 to `var2`, and the value of 12 to `var3`.
|
131
|
|
130
|
|
|
|
|
|
134
|
2. **Header:** `int example(int var1, float var2=5.0, int var3 = 10)`
|
133
|
2. **Header:** `int example(int var1, float var2=5.0, int var3 = 10)`
|
135
|
Here `var2` is initialized to 5 and `var3` to 10.
|
134
|
Here `var2` is initialized to 5 and `var3` to 10.
|
136
|
|
135
|
|
137
|
- **Invocations:**
|
|
|
|
|
136
|
+ **Calls:**
|
138
|
|
137
|
|
139
|
a. `example(5, 3.3, 12)` This function call assigns the value 5 to `var1`, the value 3.3 to `var2`, and the value 12 to `var3`.
|
138
|
a. `example(5, 3.3, 12)` This function call assigns the value 5 to `var1`, the value 3.3 to `var2`, and the value 12 to `var3`.
|
140
|
|
139
|
|
|
|
|
|
143
|
c. `example(5)` In this function call only the first parameter is given a value, and the last two parameters will be assigned values by default. That is, `var1` will be 5, `var2` will be 5.0, and `var3` will be 10.
|
142
|
c. `example(5)` In this function call only the first parameter is given a value, and the last two parameters will be assigned values by default. That is, `var1` will be 5, `var2` will be 5.0, and `var3` will be 10.
|
144
|
|
143
|
|
145
|
|
144
|
|
146
|
-**Example of a valid function header with invalid invocations:**
|
|
|
|
|
145
|
+**Example of a valid function header with invalid function calls:**
|
147
|
|
146
|
|
148
|
1. **Header:** `int example(int var1, float var2=5.0, int var3 = 10)`
|
147
|
1. **Header:** `int example(int var1, float var2=5.0, int var3 = 10)`
|
149
|
|
148
|
|
|
|
|
|
151
|
|
150
|
|
152
|
a. `example(5, , 10)` This function call is **invalid** because it leaves an empty space in the middle argument.
|
151
|
a. `example(5, , 10)` This function call is **invalid** because it leaves an empty space in the middle argument.
|
153
|
|
152
|
|
154
|
- b. `example()` This function call is **invalid** because `var1` was not assigned a default value. A valid invocation to the function `example` needs at least one argument (the first).
|
|
|
|
|
153
|
+ b. `example()` This function call is **invalid** because `var1` was not assigned a default value. A valid call to the function `example` needs at least one argument (the first).
|
155
|
|
154
|
|
156
|
**Examples of invalid function headers:**
|
155
|
**Examples of invalid function headers:**
|
157
|
|
156
|
|
|
|
|
|
173
|
---
|
172
|
---
|
174
|
|
173
|
|
175
|
|
174
|
|
176
|
-!INCLUDE "../../eip-diagnostic/DVD/en/diag-dvd-01.html"
|
|
|
|
|
175
|
+!INCLUDE "../../eip-diagnostic/DVD/en/diag-dvd-01.html"
|
|
|
176
|
+<br>
|
|
|
177
|
+
|
|
|
178
|
+!INCLUDE "../../eip-diagnostic/DVD/en/diag-dvd-03.html"
|
|
|
179
|
+<br>
|
177
|
|
180
|
|
178
|
-!INCLUDE "../../eip-diagnostic/DVD/en/diag-dvd-03.html"
|
|
|
|
|
181
|
+!INCLUDE "../../eip-diagnostic/DVD/en/diag-dvd-11.html"
|
|
|
182
|
+<br>
|
179
|
|
183
|
|
180
|
-!INCLUDE "../../eip-diagnostic/DVD/en/diag-dvd-11.html"
|
|
|
|
|
184
|
+!INCLUDE "../../eip-diagnostic/DVD/en/diag-dvd-12.html"
|
|
|
185
|
+<br>
|
|
|
186
|
+
|
|
|
187
|
+<br>
|
181
|
|
188
|
|
182
|
-!INCLUDE "../../eip-diagnostic/DVD/en/diag-dvd-12.html"
|
|
|
183
|
---
|
189
|
---
|
184
|
|
190
|
|
185
|
---
|
191
|
---
|
|
|
|
|
201
|
|
207
|
|
202
|
###Exercise 1
|
208
|
###Exercise 1
|
203
|
|
209
|
|
204
|
-The first step in this lab experience is to familiarize yourself with the functions that are already defined in the code. Your tasks require that you imitate what the functions do, so it is important that you understand how to invoke, declare and define the functions.
|
|
|
|
|
210
|
+The first step in this lab experience is to familiarize yourself with the functions that are already defined in the code. Your tasks require that you imitate what the functions do, so it is important that you understand how to call, declare and define the functions.
|
205
|
|
211
|
|
206
|
**Instructions**
|
212
|
**Instructions**
|
207
|
|
213
|
|
208
|
1. Open the project `DVDInfo` in Qt by double clicking the file `DVDInfo.pro` in the folder `Documents/eip/Functions-DVDInfo` on your computer. You may also access `http://bitbucket.org/eip-uprrp/functions-dvdinfo` to download the folder `Functions-DVDInfo` to your computer.
|
214
|
1. Open the project `DVDInfo` in Qt by double clicking the file `DVDInfo.pro` in the folder `Documents/eip/Functions-DVDInfo` on your computer. You may also access `http://bitbucket.org/eip-uprrp/functions-dvdinfo` to download the folder `Functions-DVDInfo` to your computer.
|
209
|
|
215
|
|
210
|
-2. Configure the project. The file `main.cpp` has the function invocations that you will use in the next exercises. The declarations and definitions of the functions that will be invoked can be found in the files `movie.h` and `movie.cpp`.
|
|
|
|
|
216
|
+2. Configure the project. The file `main.cpp` has the function calls that you will use in the next exercises. The declarations and definitions of the functions that will be called can be found in the files `movie.h` and `movie.cpp`.
|
211
|
|
217
|
|
212
|
3. Double click on the file `movie.h` that contains this project's function prototypes. Go to `movie.h` and identify which functions are overloaded and describe why.
|
218
|
3. Double click on the file `movie.h` that contains this project's function prototypes. Go to `movie.h` and identify which functions are overloaded and describe why.
|
213
|
|
219
|
|
|
|
|
|
224
|
|
230
|
|
225
|
---
|
231
|
---
|
226
|
|
232
|
|
227
|
-!INCLUDE "../../eip-diagnostic/DVD/en/diag-dvd-06.html"
|
|
|
228
|
-
|
|
|
|
|
233
|
+!INCLUDE "../../eip-diagnostic/DVD/en/diag-dvd-06.html"
|
|
|
234
|
+<br>
|
|
|
235
|
+<br>
|
229
|
|
236
|
|
230
|
###Exercise 2
|
237
|
###Exercise 2
|
231
|
|
238
|
|
|
|
|
|
255
|
|
262
|
|
256
|
2. Implement a function `getMovieStudio` that receives a string with the information of a movie and returns the name of the film's **studio**. Remember to add the function's prototype in the file `movie.h`. Invoke the function `getMovieStudio` in `main()` to display the name and studio of the movie in the position 75125 and demonstrate its functionality.
|
263
|
2. Implement a function `getMovieStudio` that receives a string with the information of a movie and returns the name of the film's **studio**. Remember to add the function's prototype in the file `movie.h`. Invoke the function `getMovieStudio` in `main()` to display the name and studio of the movie in the position 75125 and demonstrate its functionality.
|
257
|
|
264
|
|
258
|
-3. Implement an overloaded function `getMovieInfo` that returns the name of the studio as well as the name, rating, year and genre. Invoke the function `getMovieInfo` in `main()` to display the name, studio, rating, year and genre of the movie in the position 75125 and demonstrate its functionality.
|
|
|
|
|
265
|
+3. Implement an overloaded function `getMovieInfo` that returns the name of the studio as well as the name, rating, year and genre. Call the function `getMovieInfo` in `main()` to display the name, studio, rating, year and genre of the movie in the position 75125 and demonstrate its functionality.
|
259
|
|
266
|
|
260
|
-4. Implement a function `showMovieInLine` that **displays** the information the information displayed by `showMovie`, but in a single line. The function should have a parameter to receive a string of information of the movie. Invoke the function `showMovieInLine` in `main()` to display the information for the movie in position 75125 to demonstrate its functionality.
|
|
|
|
|
267
|
+4. Implement a function `showMovieInLine` that **displays** the information the information displayed by `showMovie`, but in a single line. The function should have a parameter to receive a string of information of the movie. Call the function `showMovieInLine` in `main()` to display the information for the movie in position 75125 to demonstrate its functionality.
|
261
|
|
268
|
|
262
|
-5. Implement a function `showMoviesInLine` that **displays** the same information displayed by `showMovies` (all of the movies within a range of positions) but in a single line per movie. For example, a function call would be: `showMoviesInLine(file, 148995, 149000);`. Invoke the function `showMoviesInLine` in `main()` to display the information and demonstrate its functionality.
|
|
|
|
|
269
|
+5. Implement a function `showMoviesInLine` that **displays** the same information displayed by `showMovies` (all of the movies within a range of positions) but in a single line per movie. For example, a function call would be: `showMoviesInLine(file, 148995, 149000);`. Call the function `showMoviesInLine` in `main()` to display the information and demonstrate its functionality.
|
263
|
|
270
|
|
264
|
---
|
271
|
---
|
265
|
|
272
|
|