|
2 | 2 | tutorial: "https://www.youtube.com/watch?v=fz4ttmwZWuc"
|
3 | 3 | ---
|
4 | 4 |
|
5 |
| -# `05` Defining vs Calling a function |
| 5 | +# `05` Defining vs Calling a Function |
6 | 6 |
|
7 |
| -Functions will only exists if you or somebody else defines them... it is the only way the language compiler/interpreter knows they exist, therefore it's able to run them when you call them. |
| 7 | +Functions will only exist if you or somebody else defines them; it is the only way the language compiler/interpreter knows they exist, therefore it's able to run them when you call them. |
8 | 8 |
|
9 |
| -To define a function we need to write this basic code formula: |
| 9 | +To define a function, we need to write this basic code formula: |
10 | 10 |
|
11 | 11 | ```python
|
12 |
| -def myFunctionName(parameter, parameter2, ...parameterX): |
13 |
| - # the function code here |
| 12 | +def my_function_name(parameter1, parameter2, ...parameterX): |
| 13 | + # The function code here |
14 | 14 | return something
|
15 | 15 | ```
|
16 | 16 |
|
17 | 17 | The word `def` is a reserved word in Python, this means it is only used to define a function.
|
18 | 18 |
|
19 |
| -**The name** of the function could be anything you like. |
20 |
| -Tip: use a descriptive name (don't be cheap with words, |
21 |
| -use as many as you need) this way you will understand what the function |
22 |
| -does -and returns-. |
23 |
| -Example names: add_two_integers , calculate_taxes , get_random_number, etc. |
| 19 | +**The name** of the function could be anything you like. Tip: Use a descriptive name (don't be cheap with words, use as many as you need); this way, you will understand what the function does -and returns-. |
24 | 20 |
|
25 |
| -**Parameters:** you can define as many parameters as you like or need. |
26 |
| -The amount of parameters will depend on the operations done inside the function, |
27 |
| -I.E: if the function is adding two integers `(3 + 4)` this means the function |
28 |
| -will need two parameters (one for each integer). |
| 21 | +Example names: `add_two_integers`, `calculate_taxes`, `get_random_number`, etc. |
29 | 22 |
|
30 |
| -**Scope:** All the code that the function will contain need to be indented |
31 |
| - one tab to the right, anything on a different indentation |
32 |
| -won't be considered as part of the function, |
33 |
| -this is called **the scope**, and it could be local (inside the function) |
34 |
| -and global (outside of the function). |
| 23 | +**Parameters:** You can define as many parameters as you like or need. The amount of parameters will depend on the operations done inside the function. I.E: If the function is adding two integers `(a + b)` this means the function will need two parameters (one for each integer). |
35 | 24 |
|
36 |
| -**The Return**: not every function needs to return something, but it is recommended that it does. |
37 |
| -Tip: returning `None` is a good default for when you, still, don't know if you need to return something. |
| 25 | +**Scope:** All the code that the function will contain needs to be indented one tab to the right, anything on a different indentation won't be considered as part of the function. This is called **the scope**, and it could be local (inside the function) and global (outside the function). |
| 26 | + |
| 27 | +**The Return**: not every function needs to return something, but it is recommended that it does. Tip: returning `None` is a good default for when you still don't know if you need to return something. |
38 | 28 |
|
39 | 29 | Example of a function:
|
40 | 30 |
|
41 | 31 | ```python
|
42 | 32 | def concatenate_number_to_string(local_number, local_string):
|
43 |
| - local_variable = local_string+""+str(local_number) |
| 33 | + local_variable = local_string + str(local_number) |
44 | 34 | return local_variable
|
45 | 35 | ```
|
46 | 36 |
|
47 | 37 |
|
48 |
| -# 📝 Instructions: |
| 38 | +## 📝 Instructions: |
49 | 39 |
|
50 | 40 | 1. Define a function called `multi`.
|
51 | 41 |
|
52 | 42 | 2. The `multi` function receives two numbers.
|
53 | 43 |
|
54 | 44 | 3. Return the result of the multiplication between them.
|
55 | 45 |
|
56 |
| -## 💡 Hint |
| 46 | +## 💡 Hint: |
57 | 47 |
|
58 |
| -+ Remember to add the `return` line. Every function should return something, in this case it should be the result of the multiplication. |
| 48 | ++ Remember to add the `return` line. Every function should return something, in this case, it should be the result of the multiplication. |
0 commit comments