Hello welcome to this tutorial on functions so a function is a block of code that performs a specific
task.
Python allows us to define functions according to our need known as user defined function and a python
function may or may not have a name.
And in this tutorial we will talk about functions which have a name
and here does these syntax for defining a function and a function counters of two.
But the head and the body function head the dot with Dickie Bird D E F which is the short form for definition
and this keyword is followed by the function name and the function name can be any valid identifier
and after the function name we have a list of arguments
inside but and this is separated by comma and we use these arguments to parse required a data to a function
and a function can take any number of arguments odd none at all.
If a function doesn't take any argument then the parent misses is left empty the function header and
the body separated by a call in here.
After a debate and this is in the next line we have a block of statements or the function body the function
body contains statements which defined what the function does.
Remember all these statements in the function body must be equally indented.
Otherwise we will get a syntax error as you can see all of these statements are equally indented Python
uses indentation of statements to determine when blogs start and end
the definition.
The function definition ends there.
The indentation ends.
So as you can see with this statement The indentation has ended.
So does the function definition the print statement he would
is define out of the function.
So here's an example off of user defined function.
So I have used the key word D E F to define the function great.
And this particular function does not take any argument.
The parenthesis is left empty.
Next in the function body I have a print function which prints the string hello.
This is the function definition but a function definition does nothing by its an if we want to use the
function.
We must call it in order to execute the code inside the function definition.
We need to call a function and the correct term is a function call.
Here it is how you call a function your function name is great.
And then you have deep it and this is and since the function greed does not take me any argument in
the function call as well you do not pass any arguments to the function remember the function call must
appear after the function is defined.
You define your function first and then make a function called and you will encounter a name error exception
you have your function call your ID and when a function is call the program control jumps to that function
definition and executes this statement inside the function body.
Once you make a function call the control jumps to this point in the program and after executing the
body of the function the program control jumps back to the part of the program which going to function.
So after all of these statements here and executed in the function body the control jumps back to this
point in the program and it assumes execution at that point.
Let us now execute the simple function in the Jupiter notebook.
This is of a function definition as you can see here.
The statement in the function body has been indented now in order to end the function definition the
indentation should also end the next statement.
Another example is a function call to the function greet call the function read using this statement
here let execute the cell and decode inside the function greed has been executed.
And here is another example.
We have a function which takes two arguments a and b and within the function body we are adding the
arguments a and b and assigning the result to a variable C and finally we are printing the value of
debatable C and in the function call.
I have parse the argument seven and eight execute this code in the Jupiter notebook function definition
for the function.
And we are passing the argument seven and eight to the function using a function called blitz.
Execute this piece of code the function has added the argument seven and eight men.
But in today's result no a function can also have an optional that don't statement.
Up to this bind we have been discussing or creating functions which do not return any values and when
a written statement is encountered inside a function the function terminate and the value of the expression
followed by the the return keyword is sent back to the part of the program that call the function and
the return statement can appear anywhere in the body of the function and the functions which returns
why use are known as why you the daunting functions
functions which do not return any values are also known as wide functions.
So let's discuss an example.
I have defined two functions the function and here takes in two arguments a and b and both of these
arguments and returned the value nixed in my second function.
Also known as + this function also takes two arguments A and B and D two argument but in this particular
function we are printing the result.
So what does it differentiate as we have discussed.
This function is called O value the turning function and the second function is known as a wide function.
We do not have the written statement in this function so let us define both of these functions in the
Jupiter notebook and see the differences between value returning function and avoid function D function
and which has the return statement.
So let execute this piece of code.
So the function has added to arguments and adorned the sum of the two arguments.
Now let's also see an example of a wide function we want to define the function.
And so this is the body of the function.
We will now make a function call bypassing the argument forward and fight to this function.
Let's execute this piece of code.
The output of this function is 9 and the value added Dawn by the function and is also 9 in order to
understand the difference between these two functions.
Letters assign this function call in this scene to a variable see in this one line we are assigning
the function card to a variable C meaning the value returned by this particular function is assigned
to this variability C the output in this cell.
Let's add another statement so we will use a print function to print the value of the variable C so
let's execute this the value of the variability is nine.
That means the result of this particular function has been assigned to this variable C and finally began
C devalue here.
Now let us check these type of the variable C
the variable C here is an integer.
This function good has returned an integer data type.
Now let's go to this function which contains a print function in the function body here as well.
We will assign a variable to the function called
and execute this say the code in this cell.
This function has been executed with this function call.
And then the function body has a print function.
The charge these arguments a and b as you can see the output here.
Now let us check the type of the variable D which has been assigned to a function call.
So let's check the data type of variable.
The add function here which does not contain a return statement has returned the data type.
None none the print function does not assign value to a variable and it doesn't return a value from
a function called.
So that is the reason the data type of this variable is of none type.
Now let us consider another example here.
We have defined a function test which has a written statement in the function body.
There is no expression after the return keyword.
So the expression followed by the return keyword is omitted.
In this case the value none is returned by this function.
So let's execute this cell.
This particular function does not return anything.
Let's assign this function call to a variable and then check the type of debatable x.
So this is also the darned the special value none.
Even if you have other done statement within the function body but if the expression is omitted the
return value would be none.
So consider another example here we have a function at 80 which returns the sum of the argument a and
b.
Now I want to use the desired of the value returned by this function.
In short I want to multiply the result of the return value by minus one.
So in order to do that I can assign this particular function call to a variable C I want to multiply
the result by minus one.
So the result is multiplied by minus 1 and then assigned to this variable C and then finally we will
print debatable C here so let's execute this piece of code.
The function D has added these two numbers A and B and then multiplied by minus 1.
Assign this value to debatable C and finally printed this variable we can use the values written by
a function in a program.
Using this example.
See this example here we have defined a function and and it returns this expression.
And in the function call here we have two arguments.
One is this value.
And the other is the function at which is adding the numbers 2 and 3.
A function can also parsed as a parameter in this particular function call D and function within the
parents.
This is executed first and the at dawn by D.
Function is parsed as an argument to the function call.
And here let let's execute this piece of code.
So this is about the return statement.