[no audio]
Friends, here we are going to see
the simple usage of your functions that
is converting given code in terms of functions
without changing the output.
Actually, I have a simple Python Script.
This is the Python Script, and this is the output for that.
Now, I want to implement this code in terms of functions.
Friends, we know that. Generally we use the functions whenever
there is a reusability, right? Not only for that, assume that
you have some thousands of lines in your code.
Instead of writing line-by-line thousand lines in your script,
it's better to divide your entire program into some parts
based on the logic.
I mean, if you observe our code we have six lines of code, right?
Assume that first two lines are for simply like welcome
message, right, and second two lines represent suppose,
this is whatever the concepts you know, known concepts, right?
Just assume that, known concepts. And last two lines are your learning concepts.
[no audio]
Now if you observe my entire script, I can divide into three parts.
One part is welcome message,
second part is known concepts, third part is learning concepts.
So if you divide your code in terms of three parts, then
readability is more, right?
Now, functions are not only for reusability purpose,
it is also for readability purpose.
[no audio]
Right. Just assume that you have thousands of lines of code
and you implemented today. And after one year you're coming
back and you're going through that logic. Directly you can't understand
what is the logic you implemented. Instead of that
if you divide your code in terms of some separate parts,
then by seeing each and every part you can easily understand
this part is so-and-so purpose, this part is so-and-so purpose.
Right. So that's why if you try to implement your code in
terms of functions, then there is more readability.
Now for that
what I am doing is, anyway it is a very simple example,
but just to get you idea on functions
I am trying to convert this code in terms of functions.
So before going to convert, guys already we know, if you want
to write a function, right, you need to define functions, right?
So you know how to define a function as well.
You have to use 'def' keyword, then some name, right, some
name, anything, right, some 'xyz', then '():',
then you have to write some number of lines based on your
requirement, and finally you have to write 'return None', right?
We'll discuss about 'return', but this is the standard syntax
to write your function as of now.
Now what I am doing is, I am trying to divide my logic, six
lines of logic into three parts.
So in first part I am taking these two lines, and I am implementing
in this way 'def welcome_msg'. That's it.
Then inside of this function, I am going to write these two lines.
[no audio]
Right. Finally, I have to write 'return None'.
Then I am going to define one more function that is, suppose
'def known_concepts'.
That's it.
Then whatever the lines you have in you your known concept,
suppose these two lines for my known concepts logic,
just assumption, nothing is there.
How you can convert your code into functions.
So for that just I am giving idea. 'return None'. And we have
two more lines. For these two more lines
what I am doing is, I am going to divide again that as suppose
new concept or learning concepts.
Let me take suppose 'new_concepts()'.
[no audio]
That's it.
[no audio]
Right. And finally just write 'return None'.
So now whatever the logic you have, that logic now I am implementing
in terms of functions and I'm running this, no output.
We know that if we define simply function you won't get
any output. To get the output, whatever the output you need
that particular function you need to call, right. See, suppose
I want to get 'known_concepts' logic output, then I will call
in this way, function name, parentheses. That's it.
Let me run it and see the output. You are getting.
But actually I need to get first these two lines,
then these two, then these two, and the same order you just
call your functions. Very first you just call your function,
that function as 'welcome_msg'.
Then last one 'new_concepts'. That's it.
Let me run it and see the output.
Yes, you are getting. Now what we did, whatever the logic you
have, but actually this logic consists of three parts
Just I'm assuming. First two are something like 'welcome_msg',
second two are something like 'known concepts' logic,
the last two are for 'new_concepts' logic. Based on that just I have
given some names and I'm calling them.
[no audio]
Right. So guys if we change this order, this functions called, the
way how you're calling know, this order, you will get based
on that order only you will get the output.
Right. And one more thing guys, see
if I remove these three functions names to call your defined function,
you're not getting output by running that code. The reason
is, whenever if you run your code Python will run from top
to down. While running from top to down whenever there is
a 'def' key word, at that time your Python
won't execute that logic or block of code which
is there under your function.
Simply your Python will remember the name of your function.
Same way if you observe all places you have simply 'def',
there is no straight forward line or main line
that's why you're not getting any output.
But if I write in this way,
[no audio]
now only this part is defining
your function. Now directly you're calling, so your line of, I mean
your execution part will start from thirteenth line.
So from this your Python will go here, and will execute here,
and will come back.
So after that line in case if you have one more function
or same function maybe, in case if you want to print welcome
message two times, yes, no problem
you can print it. Now see the result. Two times you are getting.
But I want only one time.
Yeah, no problem.
[no audio]
That's it. Now see the result.
Okay, so guys this is just I am trying to give some overall
idea. In case already if you have some logic in your hand how
to convert that into in terms of functions.
Right. So while going forward we will discuss in detail about your
different types of functions.
Anyway, we are going to discuss about user-defined functions.
Built-in functions, already
we discussed something like 'list', 'integer', 'length', right, 'id', 'char',
something like that, default functions
which are defined with your Python.
Now, we are trying to define our own defined function. User-defined
function we are defining, and we are calling them, right?
Okay guys, we will see in detail while going forward.
Okay, thank you for watching this video.
[no audio]