Functions are blocks of code which realize a particular task.
So we want to have a particular task, the task a done.
To define the function.
We use a keyword def, so we have def, we have the name of the function on the name of the task, and
then we have the arguments that is values which we pass into the function on which may modify the functions
output.
We have the two dots and then we have return.
So we return whatever value.
So yeah, we have arguments and now yeah, we have outputs.
Now let us define a function which gives the sum and product of two integers.
So we have int one and into which are parsed as arguments.
And then here we simply return.
Ain't one times.
Ain't too.
Karma.
Ain't one plus ain't too.
We run this, and now we can call this function.
So we could have.
Some come a product now, some is a keyword.
So we're going to have some one comma product.
Product one which is equals some product of two integers, a two and three.
We print out some one and product one, one, and this we have an arrow or two that this is not defined.
Now, let's look at this.
That was because actually I wouldn't put this.
Plus, we were on this.
Now we have six and five.
We see that, yeah, we have the product and yet we instead have the sum.
And that's because while returning here, we put this as product and these are some so let's reverse
this and we should be having five six.
That tells us that the sum of this two numbers is five and their product is six.
So that's what this function actually does.
We could actually rewrite this by saying the sum sum of numbers equals say in one plus means two would
also have product.
Product of numbers.
Equals and one times ain't too.
Then here I was in the return put out of numbers.
And here we have some of numbers.
Some.
Of numbers.
So we should have the same results.
Now, what if we make a call to this function while our main one of this of you on me, one of this
should have this error message saying that there is one missing positional argument, and that is because
this two arguments must be given when making a call.
So could change changes to see 80.
We have that.
Now let's see how to define the four arguments so we have a default argument.
Yeah.
So 23.
And in this case we could put out to see what we have.
We see that.
Yeah.
We didn't actually put in this second argument, this case.
Like now we've put this is given as the sum of the product, which is four or four.
But in the case where we don't put this so we put just 20 like this, you see that what you actually
have is the sum of 20 and 23 and a put out of 20 and 23.
And that's because this is a default argument.
And with default arguments you must pass in the argument when you're making a call to the function.
Now, if you have the default argument and you have another argument, let's say in three run in this
you see that you already have an error as a non default argument cannot come after a default argument.
So you should have this, you should have the entry coming before.
So you have you in three now run.
This is fine, but running this has a problem because there is this non default argument in three which
hasn't been defined or which hasn't been passed into the function when making the call.
So we have to actually add that although in this case this entry isn't doing any much work, now let's
define another function.
So we have def, we have print time.
Oh, that's a print name.
We have this function print name which takes in a name and age of a person.
So we have this age.
So let's take a default argument.
C 70 that's it.
We have return.
Now if you are returning any value, you could simply put return non.
So with this one in this we have no problem and then you can make a call to this function.
So you could say print print name of.
That's considered a name to be Joseph.
And that's all you mustn't put to each mate.
If you have to put his age, you could select an age.
See?
23.
You see that?
Yeah.
You have this known.
Now, let's modify this as this is meant to be an end.
So run this again.
Yeah, we have none.
Now, we could also return this string which is made up of this too.
So you can have a string made of name plus string of the H.
So what we're doing is we're converting this H into a string.
So with this you would have name or you could have a string before this name could have.
My name is this.
On the plus.
And then you have I am of age this, then you have this plus two.
So actually just concatenated in this different strings.
We have this four string.
You have this next string.
And you have the string.
So we have this end which has been converted into a string.
Now we should note that you could actually convert it into a string.
C e equals six.
You could have print string of E and you have this as a string.
You could also convert this string or you could convert a string say B equals seven and 20 int Yeah,
because a C equals the end of B plus three and you could print out C.
So we see that, yeah, you have ten does because this has been converted into an integer.
Now coming back to this example, printing this out, we have my name is Joseph and I am of a 23.
So let's modify this again.
You could have my name is after this put is space and there we go.
So that is it.
Now suppose we have a variable which adds up your h internally.
So you have this variable other which is equals 40.
So there you have another plus h.
So yeah, you're going to have.
My name is Joseph and I am of age 63.
So yeah, your age has been added automatically.
Let's try doing this without the string.
Find out you have an arrow because you I didn't of a string to an integer.
So yeah, we have this and that is it.
Now let's decide to create a variable out of this function.
This variable is also called other.
So we have our variable, which is 80, and we run this.
We'll see that actually in this function, the arrow we're considering is this other.
So if you have this if you combine this, you actually have this arrow which was defined out of the
function used.
So this is a global variable and this is a local variable, local in the sense that is used only within
this function.
So if you create the function, you try to use this function, I try to use this variable.
You wouldn't get access to this variable, the square unknown function simply.
So we have print.
That are all we have.
My name is this HD, so we could maintain the same output.
Now we're trying to use other.
Let's take away this other and let's run this.
So let's create this other year, which is this local variable.
And we'll try to use out of this.
We.
For now, there is no problem.
But I want to try to change this to print data now.
So make a call to this function.
We have this always seems to be working, but that's not right.
Let's restart our channel.
We start in the channel should be able to get an area.
And that's because the way this Jupyter Notebook works is that once you had declared a variable, either
initial here and put a number to 80, this variable remains even when you remove it like this.
So we start and we'll make us be sure that we don't have that variable in memory again.
So let's run this and we'll see that we actually have an error, which is what is expected, because
this is not defined.
This is defined in this function, but not in this.
So this is a local variable.
Now we can define a global variable 80 and you see that now it works.
So that's how we proceeded with this local and global variables.
Let's now look at functions and wait the number of arguments we could parses variable all like those
functions we've seen so far.
And with the number of arguments and no.
So let's take in this function the laptop characteristics.
And what this function works is it gets the name of the laptop gets as is a memory, it is the memory
and then the ram then simply prints out those characteristics.
So.
Right, yeah.
We could have laptop characteristics.
We have the name, the Mac.
Pro.
We pass the SSD memory so hard to take it.
It is the code for ram it.
So we run that and then we go.
We have the name, but as is the memory, it is a memory.
And then the ram side.
For now, our function has a fixed number of parameters.
Now let's suppose that we want to pass in this.
I want to give this where we have a GPO memory of C for gigabytes.
So we run that.
You see already that this takes four positional arguments that five were given.
So we have this error which is normal.
Now, if we do this, if we say your memory or on that C, that works fine.
Just have to come here and include this in the memory.
So you will have.
Oh, they dedicated.
GP you.
Then we have the GP.
You write?
Yeah.
Keep your memory.
From that.
All those GP.
So.
So that's.
There we go.
So that's what we we get from that.
But it turns out that not all laptops were even possessed.
This that it cannot keep your memory and even some will not possess, as is the memory.
And so instead of having to separately define another laptop characteristic method in which for the
first we could consider all this, for the second we will see, for example, that the case where we
don't have the S is a memory.
We have this.
So yes, it will do.
I defined this without S as D and then so the one will define it without.
As is the.
And you.
To take this vehicle you off with the SS the off.
And then what I want to see would be fine without.
Just the GPO.
So we'll have the SSD and we'll take the step of.
So you see that we could we cannot continue working like this as this not only is efficient but is also
not scalable.
So we'll simply take this off and then we'll work with methods which have variable number of parameters.
So in that case, instead of having all of those parameters low, simply do is would have.
As Tariq added before a sudden parents say arguments.
So let's take this off now.
And then let's print out let's find out what we get from arguments.
So you run that.
You see clearly that this argument right here collects all while we passed.
And so if we don't have the the rum, if we take this off, if we don't have the GP, you take that
off.
You see that this still works.
Like, let's take this print out from now.
So you see that this works as this argument somehow gathers all this information and stores it in this
variable.
So you run that.
That's fine.
And so now, instead of reading all this arcs, all right, instead of print out this virus, print
it out before we just have a follow up with the fall in arts, we're going to print out.
A So let's run that and there we go.
We have the name we have as is the size we have, which is the size, and then we have the round size.
So if we get back to where we have the dip of memory, you see, that comes up automatically if we want
to add some of the parameter like the color so we could have the black on that, everything works fine
if we take all this off some so works fine.
As you may have noticed.
We use a pass in this name or we just pass it in this arguments without specifying exactly what the
signify.
And that is why when we did this print, right.
Yeah.
Even after put in let's say 256 when we did the print, we just have this value has been printed out,
but we don't know exactly what to signify.
And so to solve this, we could simply have the as is the memory.
Yeah.
So we have to define that as has the memory keyword.
Then we go to the it is the memory.
So we have that.
It is the memory.
So 24 and that's fine.
So now let's run this again and you have this error.
Now, the reason why you have this arrow is because once you have this arguments which are passed with
this keywords, then you need to specify the keyword argument with double asterisk before.
So yeah, we have the arguments and then we have the keyword arguments.
Now, before doing anything here, let's, let's take this off and then let's print out what we have
here.
So we'll print out the arguments and then we show up in all the keyword arguments.
You shall see that this will come up with arguments, and then this keywords will have all in this keyword
arguments right here.
So let's run that.
And there we go.
We have the arguments.
That's my pro and then the keyword arguments, as is the memory.
It is the memory.
Notice how this comes in as a dictionary.
And if we just change this, we have a name here.
You see, now this argument's empty while the keyword arguments, it's made up name as is the memory
and the H is the memory.
And still, just as before, we could always add to RAM.
So we have eight for example, on that.
And there we go.
We have the name, we have the as is the memory, which is the memory and then the ram.
So what now we could do is we could simply print this out.
So the name is C arguments zero and then let's have that and then we print out so the back will print
out.
Oh, that's let's actually use a follow up.
So we have for the key in keyword arguments, we're going to print out the key and the keyword arguments
key.
So we're going to bring out a key and then the value.
So let's have that take off around that.
And there we go.
So here we have the name is this as is the memory.
This is the memory that and then the ram that.
But now the a big difference that we don't need to specify or we don't need a fixed number of parameters.
We have a laptop which doesn't even have its memory.
We're on that and it works and so we can read arguments and keyword arguments makes us work more flexibly.
Another thing we could do is we could simply have something like say Param.
So we don't necessarily need to have that.
We will say keyword params or it's a key word params, and that's it.
Now just to keep our answer, actually, so we have key params and then params.
So it's going to work the same way.
So it doesn't it doesn't matter how you name that, but generally we name it arts and then the keyword
act.
So we could also have params and key white params.
So keep our arms.
Yeah.
Params.
The parents see works still fine.