1 00:00:00,000 --> 00:00:03,869 Great, you can now create functions, call them and 2 00:00:03,899 --> 00:00:07,169 add parameters to make them more dynamic. And now 3 00:00:07,169 --> 00:00:10,589 what you can also do is to make a function return 4 00:00:10,649 --> 00:00:14,039 a value. Okay, so this functions, say_hello is 5 00:00:14,039 --> 00:00:16,679 just a function that does an action, it doesn't 6 00:00:16,679 --> 00:00:18,899 return anything. Now I'm going to create a new 7 00:00:18,899 --> 00:00:21,449 function. So as you can see, when you just press 8 00:00:21,449 --> 00:00:24,539 enter here, I still have the indentation. So what 9 00:00:24,539 --> 00:00:26,339 I'm going to do, I'm going to remove that 10 00:00:26,339 --> 00:00:29,969 indentation, and do def again. So let's say I'm 11 00:00:29,969 --> 00:00:35,039 going to create a function double_number, and say 12 00:00:35,129 --> 00:00:38,759 a just number. So you can create as many 13 00:00:38,759 --> 00:00:41,579 functions as you want, you just need to put them 14 00:00:41,609 --> 00:00:45,599 one after the other. Okay, so now I'm going to 15 00:00:45,599 --> 00:00:50,369 create a function with one parameter, and the name 16 00:00:50,369 --> 00:00:53,249 of the function is double_number. And I simply want 17 00:00:53,279 --> 00:00:56,939 to return the double for this number. So I'm 18 00:00:56,939 --> 00:00:59,129 still going to compute the result, which is the 19 00:00:59,129 --> 00:01:02,489 double of that number. So I'm going to create a 20 00:01:02,549 --> 00:01:08,369 variable result is equal to number times two. And 21 00:01:08,369 --> 00:01:10,319 then what I'm going to do, I'm going to use the 22 00:01:10,349 --> 00:01:15,689 return statement, return result. So what this 23 00:01:15,689 --> 00:01:18,329 function will do when it's called? So first it is 24 00:01:18,329 --> 00:01:22,019 going to receive a parameter number, it's going to 25 00:01:22,019 --> 00:01:24,779 compute the double of this number and return the 26 00:01:24,779 --> 00:01:27,209 result. So when you call the function, you can get 27 00:01:27,209 --> 00:01:30,899 the double of the number you sent. Just before we 28 00:01:30,899 --> 00:01:33,359 continue, one thing I'm going to do here is that 29 00:01:33,479 --> 00:01:36,059 so we create a variable and we just return that 30 00:01:36,059 --> 00:01:39,029 value. So basically, we are not going to use that 31 00:01:39,179 --> 00:01:41,459 we only return that variable. So what we can do 32 00:01:41,459 --> 00:01:44,909 instead of result is equal to number times two and 33 00:01:44,909 --> 00:01:49,859 return result, we can just return number times 34 00:01:49,859 --> 00:01:53,369 two and remove that. So this is going to return 35 00:01:53,399 --> 00:01:57,449 directly the correct value. And now well, I can 36 00:01:57,449 --> 00:02:01,529 call this function, so I'm just going to put a 37 00:02:01,529 --> 00:02:04,319 comment here. Okay, so we don't call those so this 38 00:02:04,319 --> 00:02:06,119 is not a problem, you could call this and call 39 00:02:06,119 --> 00:02:08,519 this, that's not a problem. Here, I just want to 40 00:02:08,519 --> 00:02:11,219 call that function and not this one. So we just 41 00:02:11,219 --> 00:02:15,119 have less lines, okay, in the output. So what I 42 00:02:15,119 --> 00:02:18,269 can do, I can do double number, I can use the auto 43 00:02:18,269 --> 00:02:22,949 completion, with let's say, three, okay. And I'm 44 00:02:22,949 --> 00:02:26,309 going, of course, to store that. So let's say 45 00:02:26,969 --> 00:02:31,229 number is equal to double_number(3), and then 46 00:02:31,229 --> 00:02:35,789 print number. So when you create a function that 47 00:02:35,819 --> 00:02:38,879 returns something with the return statement, you 48 00:02:38,879 --> 00:02:41,009 can, when you call the function, you can actually 49 00:02:41,099 --> 00:02:44,939 assign the value, you get here to a variable. And 50 00:02:44,939 --> 00:02:46,949 then you can print of course, the variable, or you 51 00:02:46,949 --> 00:02:49,619 can do whatever you want with the variable. So I 52 00:02:49,619 --> 00:02:53,999 just ran the program, and you can see here we have six, so 53 00:02:54,119 --> 00:02:57,809 the double of three is six, so the number variable 54 00:02:57,809 --> 00:03:00,989 here contains the value six. Alright, so you can 55 00:03:00,989 --> 00:03:04,469 see two types of functions here. So those who are 56 00:03:04,469 --> 00:03:06,749 just going to do an action and don't return 57 00:03:06,779 --> 00:03:10,469 anything, and those who compute or maybe get a 58 00:03:10,469 --> 00:03:13,139 value from some way and then return that value. 59 00:03:13,379 --> 00:03:16,229 Now that you can return any kind of value you 60 00:03:16,229 --> 00:03:19,349 want, here, I return an integer number, but you 61 00:03:19,349 --> 00:03:23,879 can return a float, a Boolean, string, list of 62 00:03:23,939 --> 00:03:27,209 whatever you want, well just return what you want 63 00:03:27,209 --> 00:03:29,879 from a function. But just make sure that when you 64 00:03:29,879 --> 00:03:32,489 call the function, you actually know what kind of 65 00:03:32,519 --> 00:03:35,549 data type you're going to get. Okay, and one thing 66 00:03:35,549 --> 00:03:38,939 you can also do is you can call a function inside 67 00:03:38,969 --> 00:03:41,669 another function. And those you can really make 68 00:03:41,669 --> 00:03:44,969 functions as building blocks that allow you to 69 00:03:44,969 --> 00:03:47,819 create even more building blocks. Let's see that 70 00:03:47,819 --> 00:03:54,659 with an example. def print_double_number. So 71 00:03:54,689 --> 00:03:59,639 we've number and I'm going to do results is equal 72 00:03:59,639 --> 00:04:04,379 to double_number of the number we get here, 73 00:04:05,039 --> 00:04:11,039 and then print, result or maybe make it better. 74 00:04:11,069 --> 00:04:18,238 So Double of plus, the number cast it as a 75 00:04:18,238 --> 00:04:26,009 string plus is and plus the result also casted 76 00:04:26,099 --> 00:04:30,449 as a string. So when you call this function, what 77 00:04:30,449 --> 00:04:34,403 it's going to do is, going to call the double_number function. 78 00:04:34,403 --> 00:04:35,639 Okay, so from the function you 79 00:04:35,639 --> 00:04:38,279 call another function, and you can see this 80 00:04:38,279 --> 00:04:41,249 parameter I got here, I'm going to pass it also to 81 00:04:41,249 --> 00:04:43,919 the double_number function. And then I can print 82 00:04:43,919 --> 00:04:46,919 some stuff with the parameter and the result I 83 00:04:46,919 --> 00:04:53,560 got. And I'm going to call this print_double_number. 84 00:04:53,560 --> 00:04:54,688 I'm going to remove that. 85 00:04:55,890 --> 00:04:58,950 I'm going to remove that and I'm simply going to 86 00:04:58,980 --> 00:05:02,820 run. And you can see double of three is six. 87 00:05:03,000 --> 00:05:06,750 Okay. It's all happening in that function. So here 88 00:05:06,750 --> 00:05:09,570 you have a function that just does an action and 89 00:05:09,570 --> 00:05:12,630 that's it. Here a function that returns a number. 90 00:05:12,810 --> 00:05:15,690 And here a function that does an action but inside 91 00:05:15,870 --> 00:05:18,780 you actually call another function and you can 92 00:05:18,780 --> 00:05:20,970 create another function that is going to call that 93 00:05:20,970 --> 00:05:23,300 one, etc, etc.