1 00:00:00,000 --> 00:00:01,400 [no audio] 2 00:00:01,400 --> 00:00:03,800 Friends, here we are going to see 3 00:00:03,800 --> 00:00:06,700 the simple usage of your functions that 4 00:00:06,700 --> 00:00:09,600 is converting given code in terms of functions 5 00:00:09,600 --> 00:00:11,400 without changing the output. 6 00:00:12,500 --> 00:00:14,300 Actually, I have a simple Python Script. 7 00:00:14,300 --> 00:00:16,800 This is the Python Script, and this is the output for that. 8 00:00:17,600 --> 00:00:20,500 Now, I want to implement this code in terms of functions. 9 00:00:21,400 --> 00:00:25,600 Friends, we know that. Generally we use the functions whenever 10 00:00:25,600 --> 00:00:30,900 there is a reusability, right? Not only for that, assume that 11 00:00:30,900 --> 00:00:33,700 you have some thousands of lines in your code. 12 00:00:34,800 --> 00:00:40,100 Instead of writing line-by-line thousand lines in your script, 13 00:00:41,200 --> 00:00:44,900 it's better to divide your entire program into some parts 14 00:00:45,100 --> 00:00:46,400 based on the logic. 15 00:00:46,600 --> 00:00:51,700 I mean, if you observe our code we have six lines of code, right? 16 00:00:51,700 --> 00:00:55,200 Assume that first two lines are for simply like welcome 17 00:00:55,200 --> 00:01:00,400 message, right, and second two lines represent suppose, 18 00:01:01,000 --> 00:01:06,000 this is whatever the concepts you know, known concepts, right? 19 00:01:06,000 --> 00:01:13,400 Just assume that, known concepts. And last two lines are your learning concepts. 20 00:01:13,400 --> 00:01:15,500 [no audio] 21 00:01:15,500 --> 00:01:18,900 Now if you observe my entire script, I can divide into three parts. 22 00:01:19,200 --> 00:01:21,100 One part is welcome message, 23 00:01:22,500 --> 00:01:29,200 second part is known concepts, third part is learning concepts. 24 00:01:29,700 --> 00:01:33,100 So if you divide your code in terms of three parts, then 25 00:01:34,100 --> 00:01:36,900 readability is more, right? 26 00:01:36,900 --> 00:01:41,600 Now, functions are not only for reusability purpose, 27 00:01:41,700 --> 00:01:43,800 it is also for readability purpose. 28 00:01:43,800 --> 00:01:45,400 [no audio] 29 00:01:45,400 --> 00:01:48,800 Right. Just assume that you have thousands of lines of code 30 00:01:48,900 --> 00:01:52,400 and you implemented today. And after one year you're coming 31 00:01:52,400 --> 00:01:56,600 back and you're going through that logic. Directly you can't understand 32 00:01:56,600 --> 00:02:00,400 what is the logic you implemented. Instead of that 33 00:02:00,400 --> 00:02:04,000 if you divide your code in terms of some separate parts, 34 00:02:05,200 --> 00:02:07,800 then by seeing each and every part you can easily understand 35 00:02:07,800 --> 00:02:12,000 this part is so-and-so purpose, this part is so-and-so purpose. 36 00:02:13,400 --> 00:02:18,100 Right. So that's why if you try to implement your code in 37 00:02:18,100 --> 00:02:21,900 terms of functions, then there is more readability. 38 00:02:22,400 --> 00:02:23,400 Now for that 39 00:02:23,700 --> 00:02:26,200 what I am doing is, anyway it is a very simple example, 40 00:02:26,400 --> 00:02:29,400 but just to get you idea on functions 41 00:02:29,400 --> 00:02:32,200 I am trying to convert this code in terms of functions. 42 00:02:33,500 --> 00:02:36,500 So before going to convert, guys already we know, if you want 43 00:02:36,500 --> 00:02:42,700 to write a function, right, you need to define functions, right? 44 00:02:42,700 --> 00:02:45,000 So you know how to define a function as well. 45 00:02:45,000 --> 00:02:48,700 You have to use 'def' keyword, then some name, right, some 46 00:02:48,700 --> 00:02:52,700 name, anything, right, some 'xyz', then '():', 47 00:02:52,800 --> 00:02:55,600 then you have to write some number of lines based on your 48 00:02:55,600 --> 00:03:01,000 requirement, and finally you have to write 'return None', right? 49 00:03:01,000 --> 00:03:03,800 We'll discuss about 'return', but this is the standard syntax 50 00:03:03,800 --> 00:03:05,500 to write your function as of now. 51 00:03:07,100 --> 00:03:11,700 Now what I am doing is, I am trying to divide my logic, six 52 00:03:11,700 --> 00:03:13,400 lines of logic into three parts. 53 00:03:14,200 --> 00:03:18,100 So in first part I am taking these two lines, and I am implementing 54 00:03:18,100 --> 00:03:28,100 in this way 'def welcome_msg'. That's it. 55 00:03:28,100 --> 00:03:32,800 Then inside of this function, I am going to write these two lines. 56 00:03:32,800 --> 00:03:37,000 [no audio] 57 00:03:37,000 --> 00:03:40,000 Right. Finally, I have to write 'return None'. 58 00:03:40,800 --> 00:03:44,100 Then I am going to define one more function that is, suppose 59 00:03:44,200 --> 00:03:45,800 'def known_concepts'. 60 00:03:46,400 --> 00:03:47,400 That's it. 61 00:03:48,300 --> 00:03:50,700 Then whatever the lines you have in you your known concept, 62 00:03:50,700 --> 00:03:54,400 suppose these two lines for my known concepts logic, 63 00:03:55,600 --> 00:03:57,000 just assumption, nothing is there. 64 00:03:57,000 --> 00:04:01,500 How you can convert your code into functions. 65 00:04:02,100 --> 00:04:06,300 So for that just I am giving idea. 'return None'. And we have 66 00:04:06,500 --> 00:04:09,000 two more lines. For these two more lines 67 00:04:09,000 --> 00:04:12,200 what I am doing is, I am going to divide again that as suppose 68 00:04:12,900 --> 00:04:14,700 new concept or learning concepts. 69 00:04:14,800 --> 00:04:16,500 Let me take suppose 'new_concepts()'. 70 00:04:16,500 --> 00:04:18,500 [no audio] 71 00:04:18,800 --> 00:04:19,899 That's it. 72 00:04:19,899 --> 00:04:23,300 [no audio] 73 00:04:23,300 --> 00:04:27,300 Right. And finally just write 'return None'. 74 00:04:27,600 --> 00:04:31,800 So now whatever the logic you have, that logic now I am implementing 75 00:04:31,800 --> 00:04:36,000 in terms of functions and I'm running this, no output. 76 00:04:36,000 --> 00:04:39,000 We know that if we define simply function you won't get 77 00:04:39,000 --> 00:04:43,900 any output. To get the output, whatever the output you need 78 00:04:44,300 --> 00:04:48,400 that particular function you need to call, right. See, suppose 79 00:04:48,400 --> 00:04:53,400 I want to get 'known_concepts' logic output, then I will call 80 00:04:53,400 --> 00:04:55,600 in this way, function name, parentheses. That's it. 81 00:04:55,600 --> 00:04:57,500 Let me run it and see the output. You are getting. 82 00:04:58,800 --> 00:05:01,400 But actually I need to get first these two lines, 83 00:05:01,700 --> 00:05:04,500 then these two, then these two, and the same order you just 84 00:05:04,500 --> 00:05:08,400 call your functions. Very first you just call your function, 85 00:05:09,200 --> 00:05:11,800 that function as 'welcome_msg'. 86 00:05:12,400 --> 00:05:15,200 Then last one 'new_concepts'. That's it. 87 00:05:15,200 --> 00:05:16,500 Let me run it and see the output. 88 00:05:16,500 --> 00:05:20,700 Yes, you are getting. Now what we did, whatever the logic you 89 00:05:20,700 --> 00:05:26,000 have, but actually this logic consists of three parts 90 00:05:26,000 --> 00:05:29,600 Just I'm assuming. First two are something like 'welcome_msg', 91 00:05:29,600 --> 00:05:32,400 second two are something like 'known concepts' logic, 92 00:05:32,400 --> 00:05:36,100 the last two are for 'new_concepts' logic. Based on that just I have 93 00:05:36,100 --> 00:05:38,200 given some names and I'm calling them. 94 00:05:38,200 --> 00:05:40,500 [no audio] 95 00:05:40,500 --> 00:05:44,100 Right. So guys if we change this order, this functions called, the 96 00:05:44,100 --> 00:05:47,700 way how you're calling know, this order, you will get based 97 00:05:47,700 --> 00:05:49,200 on that order only you will get the output. 98 00:05:50,600 --> 00:05:53,000 Right. And one more thing guys, see 99 00:05:53,400 --> 00:05:58,600 if I remove these three functions names to call your defined function, 100 00:05:58,600 --> 00:06:01,200 you're not getting output by running that code. The reason 101 00:06:01,200 --> 00:06:04,700 is, whenever if you run your code Python will run from top 102 00:06:04,700 --> 00:06:07,600 to down. While running from top to down whenever there is 103 00:06:07,600 --> 00:06:10,700 a 'def' key word, at that time your Python 104 00:06:10,700 --> 00:06:14,200 won't execute that logic or block of code which 105 00:06:14,200 --> 00:06:15,500 is there under your function. 106 00:06:15,600 --> 00:06:18,600 Simply your Python will remember the name of your function. 107 00:06:19,800 --> 00:06:22,800 Same way if you observe all places you have simply 'def', 108 00:06:22,800 --> 00:06:25,600 there is no straight forward line or main line 109 00:06:25,600 --> 00:06:27,400 that's why you're not getting any output. 110 00:06:28,200 --> 00:06:30,500 But if I write in this way, 111 00:06:30,500 --> 00:06:32,800 [no audio] 112 00:06:32,800 --> 00:06:35,000 now only this part is defining 113 00:06:35,000 --> 00:06:38,900 your function. Now directly you're calling, so your line of, I mean 114 00:06:38,900 --> 00:06:41,400 your execution part will start from thirteenth line. 115 00:06:42,400 --> 00:06:46,100 So from this your Python will go here, and will execute here, 116 00:06:46,100 --> 00:06:47,000 and will come back. 117 00:06:47,400 --> 00:06:49,800 So after that line in case if you have one more function 118 00:06:49,800 --> 00:06:53,900 or same function maybe, in case if you want to print welcome 119 00:06:53,900 --> 00:06:56,000 message two times, yes, no problem 120 00:06:56,000 --> 00:06:59,000 you can print it. Now see the result. Two times you are getting. 121 00:06:59,600 --> 00:07:00,900 But I want only one time. 122 00:07:00,900 --> 00:07:01,900 Yeah, no problem. 123 00:07:01,900 --> 00:07:07,100 [no audio] 124 00:07:07,100 --> 00:07:08,500 That's it. Now see the result. 125 00:07:10,100 --> 00:07:14,500 Okay, so guys this is just I am trying to give some overall 126 00:07:14,500 --> 00:07:18,200 idea. In case already if you have some logic in your hand how 127 00:07:18,200 --> 00:07:20,400 to convert that into in terms of functions. 128 00:07:21,900 --> 00:07:25,900 Right. So while going forward we will discuss in detail about your 129 00:07:25,900 --> 00:07:27,500 different types of functions. 130 00:07:28,600 --> 00:07:31,100 Anyway, we are going to discuss about user-defined functions. 131 00:07:31,300 --> 00:07:32,200 Built-in functions, already 132 00:07:32,200 --> 00:07:37,200 we discussed something like 'list', 'integer', 'length', right, 'id', 'char', 133 00:07:37,300 --> 00:07:39,400 something like that, default functions 134 00:07:39,400 --> 00:07:41,000 which are defined with your Python. 135 00:07:41,000 --> 00:07:45,300 Now, we are trying to define our own defined function. User-defined 136 00:07:45,300 --> 00:07:49,100 function we are defining, and we are calling them, right? 137 00:07:49,700 --> 00:07:52,900 Okay guys, we will see in detail while going forward. 138 00:07:53,200 --> 00:07:55,000 Okay, thank you for watching this video. 139 00:07:55,000 --> 00:07:57,208 [no audio]