|
|
|
|
5
|
![rsz_mariposa1.png](images/rsz_mariposa1.png)
|
5
|
![rsz_mariposa1.png](images/rsz_mariposa1.png)
|
6
|
![rsz_mariposa.png](images/rsz_mariposa.png)
|
6
|
![rsz_mariposa.png](images/rsz_mariposa.png)
|
7
|
|
7
|
|
8
|
-[Version 2016- Tatiana]
|
|
|
|
|
8
|
+[Version 2016- Tatiana - Ive]
|
9
|
|
9
|
|
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.
|
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.
|
11
|
|
11
|
|
|
|
|
|
241
|
|
241
|
|
242
|
In the introduction to the topic of functions you saw that in mathematics and in some programming languages, a function cannot return more than one result. In this laboratory experience's exercises, you will practice how to use reference variables to obtain various results from a function.
|
242
|
In the introduction to the topic of functions you saw that in mathematics and in some programming languages, a function cannot return more than one result. In this laboratory experience's exercises, you will practice how to use reference variables to obtain various results from a function.
|
243
|
|
243
|
|
244
|
-### Exercise 1
|
|
|
|
|
244
|
+### Exercise 1: Difference between pass by value and pass by reference
|
245
|
|
245
|
|
246
|
-In this exercise you will study the difference between pass by value and pass by reference.
|
|
|
247
|
|
246
|
|
248
|
**Instructions**
|
247
|
**Instructions**
|
249
|
|
248
|
|
250
|
1. Load the project ‘prettyPlot’ into ‘QtCreator`. There are two ways to do this:
|
249
|
1. Load the project ‘prettyPlot’ into ‘QtCreator`. There are two ways to do this:
|
|
|
250
|
+ * Using the virtual machine: Double click the file `prettyPlot.pro` located in the folder `home/eip/labs/functions-prettyplots` of your virtual machine.
|
|
|
251
|
+ *Downloading the project’s folder from `Bitbucket`: Use a terminal and write the command `git clone http://bitbucket.org/eip-uprrp/functions-prettyplots` to download the folder `functions-prettyplots` from `Bitbucket`. Double click the file `prettyPlot.pro` located in the folder that you downloaded to your computer.
|
251
|
|
252
|
|
252
|
- *Using the virtual machine: Double click the file `prettyPlot.pro` located in the folder `home/eip/labs/functions-prettyplots` of your virtual machine.
|
|
|
253
|
- *Downloading the project’s folder from `Bitbucket`: Use a terminal and write the command `git clone http://bitbucket.org/eip-uprrp/functions-prettyplots` to download the folder `functions-prettyplots` from `Bitbucket`. Double click the file `prettyPlot.pro` located in the folder that you downloaded to your computer.
|
|
|
254
|
-
|
|
|
255
|
-
|
|
|
256
|
-3. Configure the project and execute the program by clicking on the green arrow in the menu on the left side of the Qt Creator screen. The program should show a window similar to the one in Figure 3.
|
|
|
|
|
253
|
+2. Configure the project and execute the program by clicking on the green arrow in the menu on the left side of the Qt Creator screen. The program should show a window similar to the one in Figure 3.
|
257
|
|
254
|
|
258
|
---
|
255
|
---
|
259
|
|
256
|
|
|
|
|
|
263
|
|
260
|
|
264
|
---
|
261
|
---
|
265
|
|
262
|
|
266
|
-4. Open the file `main.cpp` (in Sources). Study the `illustration` function and how to call it from the `main` function. Note that the variables `argValue` and `argRef`are initialized to 0 and that the function call for `illustration` makes a pass by value of `argValue` and a pass by reference of `argRef`. Also note that the corresponding parameters in `illustration` are assigned a value of 1.
|
|
|
|
|
263
|
+3. Open the file `main.cpp` (in Sources). Study the `illustration` function and how to call it from the `main` function. Note that the variables `argValue` and `argRef`are initialized to 0 and that the function call for `illustration` makes a pass by value of `argValue` and a pass by reference of `argRef`. Also note that the corresponding parameters in `illustration` are assigned a value of 1.
|
267
|
|
264
|
|
268
|
void illustration(int paramValue, int ¶mRef) {
|
265
|
void illustration(int paramValue, int ¶mRef) {
|
269
|
paramValue = 1;
|
266
|
paramValue = 1;
|
|
|
|
|
272
|
<< "The content of paramRef is: " << paramRef << endl;
|
269
|
<< "The content of paramRef is: " << paramRef << endl;
|
273
|
}
|
270
|
}
|
274
|
|
271
|
|
275
|
-5. Execute the program and observe what is displayed in the window `Application Output`. Notice the difference between the content of the variables `argValue` and `argRef` despite the fact that both had the same initial value, and that `paramValue` and `paramRef` were assigned the same value. Explain why the content of `argValor` does not change, while the content of `argRef` changes from 0 to 1.
|
|
|
|
|
272
|
+4. Execute the program and observe what is displayed in the window `Application Output`. Notice the difference between the content of the variables `argValue` and `argRef` despite the fact that both had the same initial value, and that `paramValue` and `paramRef` were assigned the same value. Explain why the content of `argValor` does not change, while the content of `argRef` changes from 0 to 1.
|
276
|
|
273
|
|
277
|
-### Exercise 2
|
|
|
|
|
274
|
+### Exercise 2: Creation of an overloaded function
|
278
|
|
275
|
|
279
|
-In this exercise you will practice the creation of an overloaded function.
|
|
|
280
|
|
276
|
|
281
|
**Instructions**
|
277
|
**Instructions**
|
282
|
|
278
|
|
|
|
|
|
314
|
|
310
|
|
315
|
2. Now you will create an overloaded function `circle` that receives as arguments the value of the angle $$t$$, the reference to the variables $$x$$ and $$y$$, and the value for the radius of the circle. Call the overloaded function `circle` that you just implemented from `main()` to calculate the values of the coordinates $$x$$ and $$y$$ for the circle with radius 15 and draw its graph. Graph the circle within the `wCircle` object. To do this, you must call the method functions `AddPointToGraph(x,y)`, `Plot` and `show` from `main()`. Remember that these should be preceded by `wCircle`, for example, `wCircle.show()`.
|
311
|
2. Now you will create an overloaded function `circle` that receives as arguments the value of the angle $$t$$, the reference to the variables $$x$$ and $$y$$, and the value for the radius of the circle. Call the overloaded function `circle` that you just implemented from `main()` to calculate the values of the coordinates $$x$$ and $$y$$ for the circle with radius 15 and draw its graph. Graph the circle within the `wCircle` object. To do this, you must call the method functions `AddPointToGraph(x,y)`, `Plot` and `show` from `main()`. Remember that these should be preceded by `wCircle`, for example, `wCircle.show()`.
|
316
|
|
312
|
|
317
|
-### Exercise 3
|
|
|
318
|
-
|
|
|
319
|
-In this exercise you will implement another function to calculate the coordinates of the points in the graph of a curve.
|
|
|
|
|
313
|
+### Exercise 3: Implement function to calculate the coordinates of the points in the graph of a curve.
|
320
|
|
314
|
|
321
|
**Instructions**
|
315
|
**Instructions**
|
322
|
|
316
|
|