-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslides-03-01.qmd
264 lines (200 loc) · 5.33 KB
/
slides-03-01.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
---
title: "more on Functions (class slides)"
format: revealjs
slide-number: true
---
# CSC 110 - More on Functions in Python
## Global vs. Local variables (scope)
- Every variable that is created has a particular scope
- The scope of a variable is the region in the code where the variable can be used or modified
## Global vs. Local variables (scope)
- **Local Variables** have local scope -- for example, a variable assigned inside a function can only be used or modified within that function
- **Global Variables** have global scope -- for example, a variable delcared outside a function can be accessed or modified across multiple functions
## Global or Local?
```{python}
#| eval: true
#| echo: true
a = 5
b = 10
def sum():
return a + b
def multiply():
return a * b
def main():
print(sum())
print(multiply())
main()
```
## Global or Local?
```{python}
#| eval: true
#| echo: true
def sum(a, b):
return a + b
def multiply(a, b):
return a * b
def main():
a = 5
b = 10
print(sum(a, b))
print(multiply(a, b))
a = 10
print(multiply(a, b))
main()
```
## Argument vs. Parameter
- Never set variables as global variables, pass values to functions when called
- When a function is defined, the variables you want to pass to the function are called **parameter variables**
- When the function is then called, the values you pass to the function are called **arguments**
## Argument or Parameter?
```{python}
#| eval: false
#| echo: true
# The parameters are a and b
def add(a, b):
return a+b
# The arguments being passed through are 5 and 4
add(5, 4)
```
## Write functions
Write two functions: `sphere_area` and `sphere_volume` that calculates the area and the volume of a sphere:
use `3.1415` for $\pi$ and return the rounded value.
$$
a = 4 \cdot \pi \cdot radius^2
$$
$$
v = {4 / 3} \cdot \pi \cdot radius^3
$$
Name your file as `sphere.py` and submit to gradescope.
Test case:
```{python}
#| echo: true
#| eval: false
r = 0.75
print(sphere_area(r)) # 7.07
print(sphere_volume(r)) # 1.77
```
## Write functions -- solutions 1
```{python}
#| echo: true
#| eval: true
def sphere_area(radius):
"calculates the area of a sphere of given radius"
area = 4 * 3.1415 * radius**2
return round(area, 2)
def sphere_volume(radius):
"calculates the volume of a sphere of given radius"
volume = (4 / 3) * 3.1415 * radius**3
return round(volume, 2)
def main():
r = .75
v = sphere_volume(r)
a = sphere_area(r)
print(v, a)
main()
```
## Write functions
Comparing two formulas:
$$
a = 4 \cdot \pi \cdot radius^2
$$
$$
v = {4 / 3} \cdot \pi \cdot radius^3
$$
We can use area when calculating volume:
$$
v = {1 / 3} \cdot a \cdot radius
$$
Modify your `sphere_volume` function by calling `sphere_area` inside the function.
## Write functions -- solutions 2
```{python}
#| echo: true
#| eval: true
def sphere_area(radius):
"calculates the area of a sphere of given radius"
area = 4 * 3.1415 * radius**2
return round(area, 2)
def sphere_volume(radius):
"calculates the volume of a sphere of given radius"
volume = (1 / 3) * sphere_area(radius) * radius
return round(volume, 2)
def main():
r = .75
v = sphere_volume(r)
a = sphere_area(r)
print(v, a)
main()
```
## Function Comments
- Every function created is required to have a function comment, including main
- Function comments are a multi-line string (as opposed to using `#` for other comments)
## Function Comments
```{python}
#| echo: true
#| eval: true
'''
Xinchen Yu
CSC110
Class Demonstration
This program has two functions: one to calculate the area of a sphere,
the other to calculate the volume of a sphere.
The main() function is called to print to the standard output the
area and volume of a sphere of radius .75
'''
def sphere_area(radius):
'''
This function calculates the area of a sphere of given radius.
Args:
radius: integer representing the radius of the sphere
Returns:
The float representing the area of a sphere of the given radius
'''
area = 4 * 3.1415 * radius**2
return round(area, 2)
def sphere_volume(radius):
'''
This function calculates the volume of a sphere of given radius.
Args:
radius: integer representing the radius of the sphere
Returns:
The float representing the volume of a sphere of the given radius
'''
volume = (1 / 3) * sphere_area(radius) * radius
return round(volume, 2)
def main():
'''
This function prints the area and volume of a sphere of radius .75.
Args:
None
Returns:
None
'''
r = .75
a = sphere_area(r)
v = sphere_volume(r)
print(a, v)
main()
```
## Function to calculate the volume of a cylinder
Write a function that does the following:
1. Its name is `volume`
2. It takes two integer arguments: `radius` and `height`
3. It calculates the volume of a cylinder, based on `radius` and `height`. Volume is area multiplied by height.
4. It returns the float value for calculated volume.
## Function to calculate the volume of a cylinder
```{python}
#| echo: true
#| eval: false
def volume(radius, height):
# calculate the area first
area = 3.1415 * radius ** 2
# multiply area by height
vol = area * height
# return calculated volume
return vol
def main():
print(volume(1, 2)) # 6.283
print(volume(6, 10)) # 1130.94
print(volume(5, 5)) # 392.68750000000006
main()
```