Hello and welcome back.
In today's lesson we are going to look at subroutines which are also known as functions.
So go ahead and download the file 0 6 subroutines functions it and put it in your fascinating project
folder and then zip it
and it is easy for now you have a new folder 0 6 subroutines functions so many and you receive the four
of us here.
One is the add two names are some file and another file had two nuns three functions.
That means this file is run he's written not using any functions but this code.
This program is written refactoring.
So let's take a look at the first one first.
So this program prompts I use it to enter two numbers and then then prints the result.
So let's run it click on run compile
and let's go through this coded after compiling it has this file.
Yes he has
time out at least the directory and you should have this far.
So let's run it now
a promise.
Enter first number.
So we enter three presenter.
You can enter any number.
Then it prompts you to enter the second number.
Let's enter five.
Again you can enter any number you like PRESENTER Any prints result eight.
So it took the number three he took the string three and convert it into number.
And then a two then the the string five and converted it into a number and added it to number to produce
eight.
So let's see how it works.
So we have a few strings here.
The first two string into num one is two probably four the the first number.
The second string enter number two is a promise to enter the second number and the third string message
is to output a result and some is in teacher 0 number.
It is used to store the result of adding the two numbers together.
And it is initially set to zero so here you have one two three four five.
We have five numbers.
Didi missing data double giving this data bytes so to go with here are numbers.
So you're declaring five numbers.
So the first two number is for storing the input handle used by the API function and vice red is to
keep track of how many invites have been read by the Windows API can refile.
And then number one is the star the number that has been converted from the string so when it enters
a string he will be converted to number and start here.
Same thing with the two input string is the string that is being read for example three he'd be starting
his input string first before it is converted to a number and start in number 1
and in also our starts the second number as a string before it is converted into a number and start
in number two.
You program starts you ask.
Easy to enter a number by printing that from and then on one.
So here in the number of print Enter first number
then over here you've written a number issues enters.
So these are type 3 which is a string and the string is three start in this terrible input string thereafter
you use a string to all you have a function to convert the string into a number.
And this 10 and a B starting he has to register in order here we shift and he s really set value in
a variable call them or.
So number one we store the number three then it is a user would then be prompted and enter a number
so these types five finally this tiny string.
And again he would be converted into a number.
So file become with a number and start in E X NBER this number into another variable call.
And the two.
So number two in our stores.
Five.
And number one stars.
Three how we are going to add two numbers.
B 0.
He asks because we are going to use the s to sum up the numbers and we had number one two idiots and
stark idiots.
So he number one starts the very tree and he s is zero initially so triple zero is tree and trees there
is a tree start in he s so it s not becomes tree then we take the number two is contains no Fi and added
2 Three he reaches edicts so 5 plus three is it and the result it is starting back into ADX then we
soon copy some idea into another will cause some so it is an dies and it is not being saved into a sum
so somehow becomes 8 and now finally we print the result how by putting some in to the stack and push
up a message and then copying it.
So these two are the two parameters to print f.
So our whole message contains this string and a format specify.
So percentage these is used to print out numbers and he will be replaced with some.
So we get the results result hit.
So as you can see this program is quite quite long because we are repeating a few things over and over.
For example here we repeat repeating this here read the number converting to string.
So it has a better way to do this.
We don't want to keep repeating things over and over.
So what we can do is we can take this and convert it into a function subroutine and improved version
is over here.
So in this second equation how police on the left hand is in the right.
We have taken we have taken this out and put it into a function.
So this is a
if motion is from here the function get num starts here and function going on here and we give it a
label.
So it is the name of the function or the subroutine and you can see now this part here may be read from
the console is found over here and this by here we convert string to number is fun.
Or here and then we have something new here called written.
So the function runs from here and then you finishes executing a return you return to the caller now
Cody shorter instead of writing this.
Now we just have a call the new function here.
So one line here replaces all these lines here.
So we call get num he would jump to their function executed and then when he hits the written instruction
he would go back to the next instruction after the call which is move.
So this function when he finishes a restored the result.
In each case that means he has to contain the number the last functioning the insight needs and get
numb is to convert the string into a number and that number is tiny.
So many returned from this file Hinkle we will now start that he checks into the variable number one
just like what we do in our weekend program.
After that you probably use a two in there a second number and then you will call again.
Canon once the function returns he will start the value in a second variable.
Number two.
Now notice that once you finish with the second call to the function and is starting the result of the
second number in number two you are now ready to go to the next part of the instruction which you know
in Django where all this function.
So we do that by using a gem in jump up with Riza who jumped all the way past this function.
Come down here and over here you add the two numbers together so you will first initialize the X to
0 and then you add the number one mediates at number two tedious and then move the S into the variable
sum and finally print the result.
So now code is shorter.
We don't have to call these few lines twice so our code each other as you'd see in the original code
you find these two lines here.
These two functions here and using being repeated again down here but oh here we replace those with
one com and here is another call.
So these two costs make it short.
So another code is neater a shorter OK.
This is not a new thing here.
Notice.
Instead of fashion I added these two lines.
EVP and movie here speed EVP and here I also added this one I probably maybe now when you're writing
your own functions you should always do this.
This is called The Function Prolog and this is called The Function epilogue.
The reason why we do this is because we want to preserve the register EVP why do we need to preserve
EBV because GBP is used as a pointer to the stack frame.
What is a stack frame as time frame is uh part of a stack.
We started the main program and any of the CHA programs we are Cha functions which are running twice
under when you first start this program.
This whole program start from the start entry point so the entry point you will be pushed to the stack
by the operating system and the entry point will have its own base address which you start in EVP.
So when the main program calls in and a function you will then move to execute the function.
But when you go to execute in the subroutine or function the central theme function might call up EVP
but maybe using it inside is.
So to maintain the original values in EVP the CHA function will always push the original EVP value to
the stack to preserve its original value and then we'll pop it back to restore the order value when
it finishes.
So that is why we need to do this.
And what is the reason for this line.
So this line is optional.
Now why we use this is because we want to use ESB in site here so ESB can be used in say a child function
or subroutine when you want to create local variables.
But in this case here you are not creating any local variables.
So for now you can use a notice since you are not creating any local variables but it is good practice
to have a habit of writing this as well.
So whenever you write your own user defined function they should add these two lines plus just.
Now if you recall in the early lessons on unpacking we also saw that we need to keep a lookout for EVP
push EVP and probably be.
So now you understand why we need to write this when we are creating our own user defined function.
So if you forgot about the unpacking go and revise the attacking chapter since they we discuss why we
need to keep track of pushing VPN properly so that's it for this program let's take a look at the second
program.
The second program is a new program which we studied before.
Compiling and now we have a look see.
So let's run it.
Let me clear the screen first and then run the loop and then no.
And then as we enter 5 he prints one up to the number that we enter.
So we did this before in the previous lesson.
So I'm not going to go through it again.
So remember this look.
Now this look is quite long because the implement the print f inside here
so this makes it longer to make the court more neater and more efficient.
We can factor this up and put it in a separate function.
So let's take a look at the function version of this program looks a function.
Here you see
now we have uh call call call to print Current um print Konami search function we should be fine here.
So notice now he said of these uh four lines.
He replaces the one single line one single line and the effect out and this into a separate function.
And notice here again we have this function Prolog and the function epilogue and the return keyword.
So whenever these look runs when he comes to call he will call this function will and print a number
and then you return.
And if he shows so when he returns he always returns to the line after the call.
So this is how the written was right let's run this nice comparison runs to make sure it works.
So now let's try the version where it functions and the number is a 6.
And it works.
So okay so now you know how to create your own functions and subroutines to try to convert any program
to one issue land before in to another version using functions.
So that's all for this lesson.
Thank you very much.
House here the next one.