Friends here we are going to
discuss about how to define a function, and
how to use or call or execute defined function.
Right. Let me explain this with examples.
So let me create a simple script to define and to use your
function. So 'define_function.py'.
[no audio]
See that. If you want to define a function, you have to use
keyword called 'def'.
With that keyword you need to define your function, and anyway guys before
going to work with functions, why we need a function? Whenever
if you have repeated logic in your code, instead of
repeating your logic in different places
we are going to write that logic in one place, and we are
giving some name.
[no audio]
So giving name is nothing but defining a function.
Right. So I am defining simply 'display' function. That's it.
Assume that
I'm repeating two lines of code in different places. Now instead
of writing two lines of code in different places,
what I am doing is, those two lines I am writing here.
So, "This is first line", just assumption.
"This is second line".
So these two lines I am repeating suppose in ten places, instead
of repeating in ten places
I am writing two lines here, and I am giving the name for
that. So this is the way how to define a function. You have
to define your function with 'def' keyword. 'def', your function
name, then parentheses, after that colon.
Guys, actually you are defining for these two lines
you are giving one name called 'display'.
That means these two lines are there under 'display'.
So whenever if you want to write some lines under your function,
you should provide some indentation, or some space for all
your lines which are there under your function.
[no audio]
If you define any function without any line there is no use.
[no audio]
Right. See that.
I am defining in this way.
Your Python will give an error. What is that error?
"SyntaxError: unexpected EOF while parsing".
You don't have anything,
that's why it is giving an error.
[no audio]
You should have at least one line inside of your function.
Now, I have two lines.
No problem. Yes, it's fine.
[no audio]
So I defined a function, and then I am running my Python script.
You're not getting any output.
[no audio]
So whenever if we define a function, it won't execute by
itself. If you want to run your Python function you should
call somewhere.
So let's say I am calling that function from sixth line, 'display'.
Now let me run it. Now,
you're going to get your two lines of output.
So guys whenever your Python is running your code, how it
is going to run? From top to down.
So whenever it is coming from top to down, if Python identifies
a keyword called 'def', then at that time your Python won't
execute this logic, simply it will remember the name of your
function name.
Then it will come out from your function.
Then do you have any line here?
No problem, you don't have any line here.
Now, we have a line here,
but this line is 'display'. Already Python knows that 'display' is
somewhere a function.
That's why whenever 'display' is there, because already Python
knows that where 'display' is there, now your Python flow of
control, execution part will come to here, and now will execute
these two lines. After completion of these two lines, again
it will come to the same line, it will come automatically to sixth line itself.
Then it will try to go to seventh line if you have any line here.
As of now we don't have any seventh line.
Right. So now what I am doing is, I am going to call it once again.
[no audio]
See that. So what you are doing?
[no audio]
You're calling first your function,
then you are coming back to here only, then you're going to
seventh line. Seventh line, again you're calling same function.
Then you come back to here. Then you'll go to eighth line. As
of now you don't have any eighth line. If you have it
will, it may be any line, eighth line.
Let's say I am going to print, "Now, we called
display function two times".
[no audio]
Now once again, I'm going to call
it. Yes, you can call it. No problem.
[no audio]
Right. So if I run my code, first thing, very first line is a 'def'.
That's why Python won't execute this 'display' function.
Simply it will remember the name where your 'display' is there, then
it'll come out from your function, then is there anything
here? No. Here 'display'
is there. Now your Python will go to here, and it'll execute
that, and will come back to same line. Then
anyway, you don't have anything to execute in the same line.
Then it'll, Python will go to seventh line.
Now again 'display'. 'display' is a function. Now your python will
come here, and will execute these two lines.
Then it'll come back to same line, seventh line. As of now
once again you don't have anything to do on seventh line.
Then it will go to eighth line. Eighth is a 'print' statement.
It'll print that line.
Then it'll will go to ninth line. Ninth is again a function.
Now, you will come to here, you will execute these two lines, and
it'll come back to here, after that
we don't have any lines, then your Python script will stop.
Now, let me run it and see the output. That's it.
[no audio]
So guys, this is a simple way to define your function, and
is the way how to call or execute your function.
Make sure that your Python function won't execute by itself.
If you want to execute or use your function, you should somewhere
call that function in this way.
So whenever if you are calling your function, your function
name with parenthesis you have to mention. While defining
your function with the 'def', function name, parentheses, ':'.
Under function name with some indentation, with some space
you have to mention some number of lines.
That's it.
Right. Now what I am doing here is, forget about this logic.
See, I am going to define somewhere your function, simply
'display()'.
'print("
Welcome to functions concept")'.
'print("Simple way to define your function")'.
Right. Let me run it.
No output. No error. No output. So you just defined a function.
So if you want to execute that function, you should call somewhere.
Yes, I'm calling. Now,
it is executing. But instead of calling your function from ninth line
I am going to call it from very first line.
Then see the result what you are getting. You are getting
an error called, name 'display' is not defined.
Why you are getting this error means, see your Python will execute
your code line by line.
It will try to execute first line, as of now 'display'.
[no audio]
As of now your Python don't know what is the 'display'. In case
if it encounters 'def' keyword first, then your Python understands
that 'display' is a function because of this helping keyword.
[no audio]
But you defined somewhere, but before definition defining your
function you are calling, this is not the correct way to define your function.
So always you should define your function first, and after
defining your function you have to call your function.
So before calling your function, somewhere you should define
your function in your sequence.
[no audio]
Fine, and it is a better practice to write at the end
'return None' as of now. As of now, 'return None'.
See, let me run it.
Is there any problem? No problem.
It is working fine. Even though if you include 'return None',
it's working fine.
So actually it is a good practice to write at the end of
your function 'return None'.
So what is the use of that, we'll discuss while going forward.
Right. Fine. And guys, one more thing, while defining your
function name you have to follow some rules.
[no audio]
Rules to define function name. So already we know that guys.
If you remember, what are the rules to define variables.
[no audio]
Variable name should have only 'a' to 'z', or capital 'A' to 'Z', then
number 0 to 9, and then underscore. Only these three group
of characters
you should have in a variable name. Now even function also same thing.
There is no difference. The same rules which we have to define
a variable, all rules are applicable to define your function.
[no audio]
That's it. So capital letters function name and small letters function
names are different, case sensitive, we know that. So just try
to recollect the rules to define variables,
same variable name rules are applicable for your function as well.
Right. You should not start with any number fir your variable. Same here,
also functions should not start with
[no audio]
number, but we can start with
[no audio]
underscore, no problem. Underscore or letters, characters,
and don't include any space.
[no audio]
That's it. So these are the rules. Exactly the variable rules are applicable
even while defining your function name as well.
That's it.
and finally,
[no audio]
so your function must be defined.
[no audio]
before calling it.
See, I am defining at very first your function.
I am calling at sixth line,
then there is no problem.
But if I call our function at very first line, and if I define
this function somewhere in 10th or 11th somewhere below
that then it will create a problem.
It will give an error. That is not the correct
way. First define your function, then somewhere you can
call that. Right.
So guys, once again this is a calling function, and you are calling
this. This is a called function. Called function you're calling
from here. So of course both are same but because of this you
are able to execute these lines.
So this is calling, this is called function.
You need to remember these names as well. And you can call
your function any number of times in your code. Right. Fine.
So finally I want to give this definition
once again. A function is a block of code for some specific
operation, and functions are reusable.
You can reuse your function code any number of times in your
logic. There is no restriction for that.
We can use that any number of times. And one more thing,
it won't execute itself.
It will execute only when it is called in this way. You're
calling from here. This function we called.
So if you don't have this thing, it won't execute, right?
See, let me save it and run it. You don't have any output.
But if I do, if I call that in this way then only it is going
to execute these two lines.
[no audio]
That's it. So guys now tell me why we need functions? Why functions are
required? Why functions are used in our code? First thing
is code reusability purpose.
So we started our introduction of functions with
this concept only. Whenever if you are repeating your logic
in different places, so instead of repeating your logic,
we are writing your logic in one place and we are calling
that in different places.
Then second one, is improve modularity. Means,
[no audio]
see even though if you don't have any requirement with your
functions in your code, it's better to write your code in
terms of functions.
Why? Why means, just assume that you are writing some thousands of lines.
[no audio]
Thousand lines of code you're writing.
So if you don't have functions concept, what you will
do? Line-by-line you will go and write.
Assume that these first four lines are for some specific
purpose, and next hundred lines are for some specific purpose.
[no audio]
Now instead of writing straightforward, if I use some name for this,
[no audio]
right, if I give some name for your different lines, what
is the purpose of that logic? In future easily
I can understand
what is the logic you've written, or the big code
I can split into some parts and each part
I can write as a separate function, and finally I will call
all the functions in one place.
So that the readability will increase by seeing your code,
for your code.
Right. So from now onwards even though if it is having only one
line of code try to write in terms of functions so that you
will be good with functions after sometime.
So from next class onwards, we will try to write each and
every code in terms of functions only, so that you will be
good with functions. Functions are very easy.
You don't need to worry much about your functions.
[no audio]
Right. Then, types of functions. So guys, basically there are two
types of functions. One is built-in functions. Built-in functions
means by default
they are with your Python.
[no audio]
Then user-defined functions, means we are defining those functions.
Just now we defined, right?
We defined one function called 'display'.
[no audio]
So if there is a function, then only you can use it, right?
Let me remove this function.
Right. What I am doing is I am running this.
[no audio]
You're going to get an error called, 'display' is not defined.
Because nowhere we define our function, right. Now,
it is giving perfect output. Same way suppose assume that
I'm writing some 'print(len("hi"))'.
[no audio]
You're getting 2. Actually, 'len()' is a function.
Do you have that in our code? No, but still it is working.
See if I remove this part, 'display' is not working.
Right. If I remove this part 'display' is not working.
[no audio]
But 'len()', nowhere we define 'len()' function, but still
we are able to use that.
So that means this is the default function with your Python,
built-in functions with your Python.
So for built-in functions, you don't need to worry, directly you
can use it. Not only length, if you remember we have suppose,
if I write 'x = 40', then 'print(id(x))', 'id' is also
one built-in function with your Python. It is going to give
you the address of your variable. Not only that, let me take
[no audio]
'x = '
[no audio]
(5,6),
[no audio]
right. Then 'print(len(x))'.
Now tuple length, you're going to get. 'len()' is already
defined. Not only this we have so many functions
we've already seen. Let's say. 'print(min(x))'.
What is the result? 5. 'min()' is a function here. 'print(max(x))'.
Maximum value among your values in 'x', 6. So these all
are some of the default functions with your Python.
[no audio]
Not only that, 'int(x)', right.
I mean if you want to do some type conversion what you are
doing? 'int', 'list', conversion, right. Suppose if I have a
'5', that is a string actually. Now,
what I am doing is, I am trying to convert that as a integer.
So 'int(x)', 'int()' is a function here, but that is
a built-in function or default function with your Python.
[no audio]
Right. So you have two types of functions - one is user defined
functions, and one more is built-in functions.
So very rarely we used built-in functions.
But sometimes you have very importance with your built-in
functions, with some functions, some built-in functions,
but most of the cases you are going to define your user defined
functions. Right.
So now your concentration is how to define and use and work
with your user-defined functions.
[no audio]
Right. So as of now we have seen simple way to define your function,
right. In next video
we will see in detail with your functions, how to define, and
how to call with some arguments. See 'display' in parentheses
I'm not writing anything, but in 'len()', I am writing something.
So why should we write this?
So to understand this function you need to work with your
user defined functions by passing some value here so that
you can understand effectively why 'len()' requires some input.
Right.
Okay guys, so finally you need to remember this, how to define
a function and how to call a function. Right.
Okay guys, thank you for watching this video.
[no audio]