You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As we know, we can use functions to give a name to sequences of our code, to make it more expressive. We can also use functions to allow us to reuse code. In this lab we will get practice in using functions for these purposes.
4
+
## Introduction
5
5
6
-
### Objectives
6
+
As we know, we can use functions to name sequences of our code, thus making our code more expressive. We can also use functions to allow us to reuse our code. In this lab we will practice using functions for these purposes.
7
7
8
-
* Practice declaring and returning values from function
9
-
* Practice accessing variables outside of a function's scope, from inside of a function
8
+
## Objectives
10
9
11
-
### Writing our first functions
10
+
* Practice declaring and returning values from functions
11
+
* Practice accessing variables that are outside of a function's scope, from inside of a function
12
12
13
-
Imagine we work in a diner. We have a list of `orders` which we assign below. Write a function called `number_of_orders` that returns the current number of orders.
13
+
## Writing our first functions
14
+
15
+
Imagine we are working on our list of travel destinations -- which is really turning out to be a full time job. We have our list of `travel_destinations` which we assign below. Write a function called `number_of_destinations` that returns the number of destinations we have on our list.
Now write another function called `next_up` that returns the first order (the order with the lowest index), in the `orders` list.
29
+
Now write another function called `next_up` that returns our first destination (the destination with the lowest index), in the `travel_destinations` list.
Ok, now write a function called `healthy_order` that returns the string `'spinach salad'`.
49
+
Ok, now write a function called `favorite_destination` that returns the string `'madagascar'`.
45
50
46
51
47
52
```python
48
-
defhealthy_order():
53
+
deffavorite_destination():
49
54
pass
50
55
```
51
56
52
57
53
58
```python
54
-
healthy_order() # 'spinach salad'
59
+
favorite_destination() # 'madagascar'
55
60
```
56
61
57
-
Now let's declare an array called `orders`. Change the function `healthy_order` so that it continues to return the string `'spinach salad'`, but also adds the string `'spinach salad'` to the end of the list of orders.
62
+
Again, let's declare an array called `travel_destinations`. Change the function `favorite_destination` so that it continues to return the string `'madagascar'`, but also adds the string `'madagascar'` to the end of the list of destinations.
Now let's write another function iterates through the list of `orders` and capitalizes the first letter of each word in the order. It should return a list of capitalized orders.
71
+
Now let's write another function which iterates through the list of `destinations` and capitalizes the first letter of each word. It should return a list of capitalized destinations.
Now if someone adds, an upcapitalized order, we can simply call our function again to return a list of capitalized orders.
85
+
Great! Now if someone adds a country that is lowercased to our list of destinations, we can simply call our function again to capitalize each of the destinations in the list.
Great job. Through this lab you were able to get practice both writing and returning values from functions. You also practiced accessing variables not local to the function, but in the global scope.
97
+
Great job! In this lab we were able to get practice both writing and returning values from functions. We also practiced accessing variables not local to the function but in the global scope.
0 commit comments