|
| 1 | +''' |
| 2 | +Welcome to another python 3 basics video, in this video we will carryon with |
| 3 | +functions. In the last video you were shown a very basic function, without |
| 4 | +any parameters |
| 5 | +
|
| 6 | +In this video, lets include a parameter, and give this function more... |
| 7 | +functionality. |
| 8 | +''' |
| 9 | + |
| 10 | + |
| 11 | +# changed name to simple math, to better describe our intentions |
| 12 | +''' |
| 13 | +Now we've specified 2 parameters for our function, calling them num1 |
| 14 | +and num2, for number 1 and number 2. |
| 15 | +
|
| 16 | +Now, we carry on writing our function, where we can specify what we |
| 17 | +desire to do with num1 and num2. |
| 18 | +
|
| 19 | +in our case, we want to do simple addition. |
| 20 | +''' |
| 21 | +def simple_addition(num1,num2): |
| 22 | + answer = num1 + num2 |
| 23 | + print('num1 is', num1) |
| 24 | + # so here the answer variable will be filled with whatever |
| 25 | + # num 1 plus num 2 is. |
| 26 | + print(answer) |
| 27 | + # then at the end, we want to print out the answer to the client |
| 28 | + |
| 29 | + |
| 30 | +''' |
| 31 | +so now we run this, and when we want to do some simple_addition... |
| 32 | +''' |
| 33 | + |
| 34 | +simple_addition(5,3) |
| 35 | +# here we will do 5 + 3, for an answer of 8 |
| 36 | + |
| 37 | +''' |
| 38 | +There is no limit to the amount of variables you can have. The only thing |
| 39 | +you will want to look out for at this point is the order of the variables, |
| 40 | +
|
| 41 | +as well as the quantity. |
| 42 | +
|
| 43 | +You can protect yourself from order by doing the following in your calling: |
| 44 | +''' |
| 45 | + |
| 46 | +simple_addition(num1=3,num2=5) |
| 47 | +# or more clearly # |
| 48 | +simple_addition(num2=3,num1=5) |
| 49 | + |
| 50 | +# in this case, if you are clear in your specification, it does not matter |
| 51 | +# the order. Most people, however, do not write out the variables like that, |
| 52 | +# they just maintain the order. |
| 53 | + |
| 54 | + |
| 55 | +#finally, it is important to use the proper quantity of variables. |
| 56 | + |
| 57 | +# will not work, too many vars |
| 58 | +simple_addition(3,5,6) |
| 59 | + |
| 60 | +# will not work, too few vars |
| 61 | +simple_addition(3) |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | +''' |
| 67 | +That's it for this video, in the next video I willbe covering default variable |
| 68 | +assignments. |
| 69 | +
|
| 70 | +''' |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | + |
0 commit comments