|
@@ -5,11 +5,11 @@
|
5
|
5
|
![rsz_mariposa1.png](images/rsz_mariposa1.png)
|
6
|
6
|
![rsz_mariposa.png](images/rsz_mariposa.png)
|
7
|
7
|
|
8
|
|
-[version 2016.01.29]
|
|
8
|
+[Version 2016- Tatiana]
|
9
|
9
|
|
10
|
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
|
|
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 call and define functions that compute the coordinates of the points of the graphs of some curves. You will also practice the implementation of arithmetic expressions in C++.
|
|
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 experiences, 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 call and define functions that compute the coordinates of the points of the graphs of some curves. You will also practice the implementation of arithmetic expressions in C++.
|
13
|
13
|
|
14
|
14
|
|
15
|
15
|
## Objectives:
|
|
@@ -49,9 +49,9 @@ Before you get to the laboratory you should have:
|
49
|
49
|
|
50
|
50
|
## Functions
|
51
|
51
|
|
52
|
|
-In mathematics, a function $$f$$ is a rule that is used to assign to each element $$x$$ from a set called *domain*, one (and only one) element $$y$$ from a set called *range*. This rule is commonly represented with an equation, $$y=f(x)$$. The variable $$x$$ is the parameter of the function and the variable $$y$$ will contain the result of the function. A function can have more than one parameter, but only one result. For example, a function can have the form $$y=f(x_1,x_2)$$ where there are two parameters, and for each pair $$(a,b)$$ that is used as an argument in the function, the function has only one value of $$y=f(a,b)$$. The domain of the function tells us the type of value that the parameter should have and the range tells us the value that the returned result will have.
|
|
52
|
+In mathematics, a function $$f$$ is a rule that is used to assign to each element $$x$$ from a set called *domain*, one (and only one) element $$y$$ from a set called *range*. This rule is commonly represented with an equation, $$y=f(x)$$. The variable $$x$$ is the parameter of the function and the variable $$y$$ will contain the result of the function. A function can have more than one parameter, but only one result. For example, a function can have the form $$y=f(x_1,x_2)$$ where there are two parameters, and for each pair $$(a,b)$$ that is used as an argument in the function and the function will have only one value of $$y=f(a,b)$$. The domain of the function tells us the type of value that the parameter should have and the range tells us the value that the returned result will have.
|
53
|
53
|
|
54
|
|
-Functions in programming languages are similar. A function has a series of instructions that take the assigned values as parameters and performs a certain task. In C++ and other programming languages, functions return only one result, as it happens in mathematics. The only difference is that a *programming* function could possibly not return any value (in this case the function is declared as `void`). If the function will return a value, we use the instruction `return`. As in math, you need to specify the types of values that the function's parameters and result will have; this is done when declaring the function.
|
|
54
|
+Functions in programming languages are similar. A function has a series of instructions that take the assigned values as parameters and perform a certain task. In C++ and other programming languages, functions return only one result, as it happens in mathematics. The only difference is that a *programming* function could possibly not return any value (in this case the function is declared as `void`). If the function will return a value, we use the instruction `return`. As in math, you need to specify the types of values that the function's parameters and result will have; this is done when declaring the function.
|
55
|
55
|
|
56
|
56
|
### Function header:
|
57
|
57
|
|
|
@@ -69,11 +69,11 @@ would be the header of the function called `example`, which returns an integer v
|
69
|
69
|
### Calling
|
70
|
70
|
|
71
|
71
|
|
72
|
|
-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:
|
|
72
|
+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:
|
73
|
73
|
|
74
|
74
|
`result=example(2, 3.5, unCar);`
|
75
|
75
|
|
76
|
|
-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 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`.
|
|
76
|
+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; this means that 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`.
|
77
|
77
|
|
78
|
78
|
You can also use the function's result without having to store it in a variable. For example you could print it:
|
79
|
79
|
|
|
@@ -121,15 +121,15 @@ int example(char, int) ;
|
121
|
121
|
int example(int, char) ;
|
122
|
122
|
```
|
123
|
123
|
|
124
|
|
-All of the above functions have the same name, `example`, but different parameters. The first and second functions have the same amount of parameters, but their arguments are of different types. The fourth and fifth functions have arguments of type `char` and `int`, but in each case are in different order.
|
|
124
|
+All of the above functions have the same name, `example`, but different parameters. The first and second functions have the same amount of parameters, but their arguments are of different types. The fourth and fifth functions have arguments of type `char` and `int`, but have a different order in each case.
|
125
|
125
|
|
126
|
|
-In that last example, the function `example` is overloaded since there are 5 functions with different signatures but with the same name.
|
|
126
|
+In that last example, the function `example` is overloaded since there are five functions with different signatures but with the same name.
|
127
|
127
|
|
128
|
128
|
|
129
|
129
|
|
130
|
130
|
### Default values
|
131
|
131
|
|
132
|
|
-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.
|
|
132
|
+Values by default can be assigned to the parameters of the functions starting from rightmost parameter. 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.
|
133
|
133
|
|
134
|
134
|
**Examples of function headers and valid function calls:**
|
135
|
135
|
|
|
@@ -193,7 +193,7 @@ $$x=r \cos(t)$$
|
193
|
193
|
|
194
|
194
|
$$y=r \sin(t),$$
|
195
|
195
|
|
196
|
|
-where $$t$$ is a parameter that corresponds to the measure (in radians) of the positive angle with initial side that coincides with the positive part of the $$x$$-axis and terminal side that contains the point $$(x,y)$$, as it is illustrated in Figure 1.
|
|
196
|
+where $$t$$ is a parameter that corresponds to the measure (in radians) of the positive angle with an initial side that coincides with the positive part of the $$x$$-axis and a terminal side that contains the point $$(x,y)$$, as it is illustrated in Figure 1.
|
197
|
197
|
|
198
|
198
|
|
199
|
199
|
---
|
|
@@ -205,7 +205,7 @@ where $$t$$ is a parameter that corresponds to the measure (in radians) of the p
|
205
|
205
|
|
206
|
206
|
---
|
207
|
207
|
|
208
|
|
-To plot a curve that is described by parametric equations, we compute the $$x$$ and $$y$$ values for a set of values of the parameter. For example, Figure 2 shows the values for $$t$$, $$(x,y)$$ for the circle with $$r=2$$.
|
|
208
|
+To plot a curve that is described by parametric equations, we compute the $$x$$ and $$y$$ values for a set of values of the parameter. For example, Figure 2 shows the values for $$t$$, $$(x,y)$$ for the circle with $$r=2$$.
|
209
|
209
|
|
210
|
210
|
|
211
|
211
|
---
|
|
@@ -239,7 +239,7 @@ To plot a curve that is described by parametric equations, we compute the $$x$$
|
239
|
239
|
|
240
|
240
|
## Laboratory session:
|
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
|
244
|
### Exercise 1
|
245
|
245
|
|
|
@@ -247,9 +247,11 @@ In this exercise you will study the difference between pass by value and pass by
|
247
|
247
|
|
248
|
248
|
**Instructions**
|
249
|
249
|
|
250
|
|
-1. Download the folder`Functions-PrettyPlots` from `Bitbucket` using the terminal, moving to the `Documents/eip` directory, and writing the command `git clone http://bitbucket.org/eip-uprrp/functions-prettyplots`.
|
|
250
|
+1. Load the project ‘prettyPlot’ into ‘QtCreator`. There are two ways to do this:
|
|
251
|
+
|
|
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.
|
251
|
254
|
|
252
|
|
-2. Load the project `prettyPlot` in Qt Creator by double clicking on the file `prettyPlot.pro` in the folder `Documents/eip/functions-prettyplots` of your computer.
|
253
|
255
|
|
254
|
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.
|
255
|
257
|
|
|
@@ -356,3 +358,6 @@ Use "Deliverables" in Moodle to hand in the file `main.cpp` that contains the fu
|
356
|
358
|
[2] http://paulbourke.net/geometry/butterfly/
|
357
|
359
|
|
358
|
360
|
[3] http://en.wikipedia.org/wiki/Parametric_equation
|
|
361
|
+
|
|
362
|
+
|
|
363
|
+
|