We're starting with the very important concept that is a Biton functions how to write functions in our
python programming.
Whenever we write the long code, then also we try to divide that code, segregate that code into multiple
different models and functions.
And whenever functions are there, then it will make us easier to understand that program.
And obviously the reusability of the code can be enhanced by writing one related one function containing
related statements which are executed as a whole whenever such an operation will be asked for.
So let us go for the further discussion on this function of in Python.
So function is a group of predicate statements that perform a specific task in our program function
implementation breaks up a program into smaller and modular chunks and function helps us to make our
program more modular, organized, easy to debug, reusability will be enhanced and so on.
It is really features helps us to avoid repetition of our programming code, syntax of a passive function,
how to write one function in our python so def will be written.
That is a different function, so defined function also we can define so def will be there.
So this is a function name.
These are the list of parameters you may or may not pass, obviously, so list of parameters added.
Don't forget to give the school on because if you get this call, that means you are starting with the
function body and the function body will have that indent in that case.
And there is a string which has been enclosed within three skorts the double there.
So there is a GOC string doxxing if you want to insert the doctrine that is that documentation string,
that should be the first statement within the function body.
And then we can write one or more than one statement and then return value return and then return value
would be there.
And that is optional.
A function may or may not return a value.
So there is a way to write the function in about Biton.
Types of functions, basically functions can be divided into the following two types, the first one
is the building functions, the functions that are built into the python as an example, ebb's, which
will give you the absolute value of the input parameter and then character with having to see each other.
So there is a flow, there is a conversion to the float.
That is, the print will be using this print, whether using this print multiple times in our python
code to print some string on the screen, on the console.
So these are the inbuilt functions.
There is a built in functions we need not to write that is code for all these functions there.
But I can readily use them in our code as when required.
So next one is our use, the different functions and these functions.
We are responsible to write the body and the function defined by the users themselves and to do certain
specific tasks.
All of us remember the function name should be very much, very much important because it is a different
function from the function name.
Who should get the purpose of the function.
If you give the function name like function one function to function three, they don't carry an instance
in future.
So next time, if you if I ask you what is the purpose of function, one, you are supposed to look
at the code, but the function name should be pretty much logical, very much meaningful, so that from
the function name event you can get the purpose of the function.
I let us go for one practical demonstration to show you how to write these functions in our Python programming
with some simple coding.
So here is the demonstration in.
We are going to discuss functions and in case of Python functions, the function names should be defined
and here will be using the keyword.
There is a defined that is a diff and the function name within the opening and closing prospect.
We can write the respective parameters if I want to pass some input parameters there.
This is known as it was a different function because we are writing their respective function, but
also function should be should have this opening and closing bracket that will indicate that it is a
function here as here we are writing the function body.
So it will be known as the user defined function.
But if you don't write the function body, that means in case of print, you can easily find that this
print is a function because it is having this opening our first record and the closing possibilities
and it is taking a string as input parameter.
But we are not writing the body of the screen.
So that is why it is called library function or inbuilt function.
After writing this def and then function, then opening back at closing bracket, then we shall put
one colon here.
Then this is part of the function body will have the indentation, which is the four blank spaces are
there.
So all the statements which will be maintaining this for blank spaces beforehand will be treated as
a part of the function body.
OK, so in this way that is no big and there is no culebras open.
Culebras close, there is no block begin and block end marker.
Only this indentation will indicate that these statements are within this function bodies.
So this is this one is a function which is having no return statement.
You can find that there is no written statement we have written, but in return we are having one return
statement and it is having this string to be returned, return common returns, back the control to
the column, and mainly used to return a value to the column of the function.
So whenever we are calling this one, then this print statement will get executed because this is the
only one statement within this one function body.
But in case of Halo two, when we'll be calling this, it will return one string so the content will
come back to the caller when this return statement will be executed and then print will print that this
particular print function of print method will print the string, which will be returned here.
So let us go for the execution.
You can find this is the outcome of hello one hello, one function calling.
And this outcome of this print has to function so a function can have the first step in which will be
in the indentation will be known as the document string or string if it is enclosed within our triple
single course or triple double code.
So I can write this one as and this one.
So this is.
This is a.
Multi-line text, so I've written this one within tripple single quotes.
Now this is known as the dark string, and this docketing is a first step in this function body, which
is having that in the indentation.
OK, so how to access this one so we can do this one in this way?
So in this way, I can access this tape and string or documentary, it can easily find this is in my
line text instead of enclosing it within a single triple single cause, I also can use them as double
quotes.
Then also, it will have the same output as I've you.
OK, next, we are going for the other program part here.
One thing has to be remembered here.
This one is that it was a different function because we are writing the function body.
This Halbertal is that it was a different function because we are writing this function body.
But this print is a function or the library function because we're not writing the function body of
print, but we're using it, we're calling it according to our purpose.
So that's why this print is known as the library function here.
Here to define multiple functions, so first function is my addition, which takes X and Y as input
document.
So here the key word def there is a defined function name and then input arguments included within opening
and closing must be there.
And it is the body of the function which is printing this statement that is performing the addition,
Operation and Return X, plus similarly written the function body for subtraction, performing the subtraction
operation return X minus Y, my multiplication which is taking X and Y as input parameters, performing
the multiplication operation Return X started while my division it is taking this X and Y as input arguments.
So print performing that division, Operation Return X by white.
So in this way we have defined my additions, my subtraction, my multiplication, my division methods
now are going to define another method that is known as my menu.
All these methods that user defined, user defined functions.
So under this my menu are having this amendment will be printed.
Therefore options are going to get printed.
Then it blocks for the for the option number.
Then it will accept the option number he had.
Input is a function library function which returns a string.
So before assigning it to the variable, I'm doing the typecasting using another inbuilt function library
function.
That is a..
So now it will return the value of S.H. So my menu returns the value of S.H. Let us go for the final
function that is about UNDEF calculation so that it is calling my menu.
So my menu will print this, my menu and the four options and it will accept for the choice and then
the value will be returned to value which will be returned, will be assigning, will be assigned to
this.
S.H. It does not mean that here the variable is C.H. So here also we are supposed to have the variables
and you can also make it as choice.
So that is not an issue because this choice is is local within this menu and this is local within this
calculation.
So they may be having the same name, maybe having the different name, but in both cases they will
have their separate memories with allocation during execution of our code.
So here we have we agreed to number one.
And number two, it was in the same way.
Please enter the first number.
Please enter the second number, input function returns string.
So it has been converted to integer using IMT here.
So if the value will be returned by this, if it is equal to one, then the result is equal to you.
See, I'm calling this my addition function.
And number two, I'm passing two parameters.
This number and number will get assigned onto X and Y.
You can find it and then it to return the sum of it after printing this line.
So that result will be assigned to this result so that some will be assigned to this result.
Four tries to return the respective difference that will be assigned to result in case of choice three,
the respective product will be returned and that will be assigned to the variable result in case of
choice for the respective quotient will be returned and that will be assigned to the variable result
here.
So print result is able to result.
So I'm going to print that one.
So if I call this respective function, that is calculation.
You're going to go for the calculation.
You can find that it is at first it is calling this myman I.
My menu is displaying the options and it is asking for the option number.
If I give the option number one, then it is asking for the first number.
Let it be one hundred and the second number, let it be two hundred.
So as a result of that, as I selected option number one, so additions will be done between 100 and
200.
So as a result we are going to get 300 here if I execute the code once again.
So here I'm going for the option number two, I'm going for the multiplication between 10 and 20 you
can easily find.
So I'm getting the I'm getting the subtraction.
I've selected.
Option number two.
It is for the subtraction.
So good getting here minus ten as the outcome.
So let me show it once again.
So here I'm going for option number two.
That is a subtraction.
So I'm just giving the value one hundred and giving the value forty.
You can find that performing the subtraction operation.
So because this particular line has got printed, because I wrote that one in our minds, a production
method and then it is returning X Y and that will be assigned to the result and and later.
So result is equal to the result.
Variables value will get printed.
So result is equal to sixty is getting printed because we have given inputs one hundred and forty.
So the difference will be sixty here.
So let us go for the third option.
So option number three, so now it is multiplication.
So if I give the value one hundred and twenty, so the result is coming as two thousand performing the
multiplication of.
So result is a goal to 2000 here.
So let us go for the last one.
So if I give the option number four, I'm supposed to perform the division operation.
So the question will be returned.
I'm giving this one hundred and twenty five.
You can find that it is giving me the result.
As for Bunjil, so in this way, you can easily find that we have written some code here for the better
explanation and for your better understanding.
So a function can return, a return value may not return a value.
A function should should be starting with this difficult which is defined then the function name, then
opening bracket.
Closing bracket will include the input parameters, then colon is master and all the all the statements
which will be written with, with intent.
So they will be treated within the function body.
You can find out how we are writing this code and how the function is getting called here.
So I think the conception is getting clear to you that how this function can work.
So I'm giving you one assignment.
We know that division by zero is illegal.
So that's why whenever you are performing this division operation before performing the division, obviously
I should check the value of Y.
If the value of Y is negligible, then obviously I should handle the situation carefully.
So please modify this code and so that you can also check with the division by zero is illegal, is
happening or not, and accordingly modified this code and write and write the program yourself.
Thanks for watching this video.