Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
Now.
So far we built a good enough program.
It can take user input and based on it, it changes the Mac address of the interface that the user specifies
to the MAC address that they choose.
What I don't like about it right now, though, is it looks a little bit messy and I think it can really
use some refactoring.
Therefore, I think this is a great opportunity to introduce you to functions.
Now functions are used everywhere in programming and we've actually used them in previous lectures,
even without you thinking.
So for example, when we used to use the input or zero input, that was a function that displays on
screen whatever we put in here and returns, whatever the user inputs.
And then we use to capture that and store it in a variable.
So what we mean by a function is it's a set of instructions that carry out a certain task.
Like we've seen before, it can take an input.
So for example, again here, the input or the RO input takes a string as input and displays it on screen
and it can return a value.
And again, we see that this returns the value of whatever the person enters using their keyboard.
Now the reason why we use functions is because, first of all, they make our code look pretty and we'll
see that now once we use it.
It also makes our code reusable.
So we've seen before that we use the input function twice in our code to ask for the interface and for
the Mac address.
Without that function, we would have had to write the code for the input function twice.
It also makes our code or makes programming more abstract because when we use the input, for example,
we don't even know how it works.
We don't know the code used in this function.
All we know is I need to give it a string.
The string that I give will be displayed on screen and then the function will work and ask the user
to enter something.
Whenever the user enters something, the function will give it back to me.
I don't know the code behind the input function itself.
Whoever wrote it, wrote it very well and now all of us are using it.
So you need to use the same idea when you program.
So whenever you have a block of code in your program, for example, right here we have a block of code
that changed the MAC address.
Now we know that this code works very well, and if we bundle this inside the function, then we can
reuse this function any number of times, even in the future.
If we write bigger programs and need to be able to change the MAC address from within that program,
then we can just call this function instead of having to type its code.
Again, not only that, but we can also bundle this in a separate file and then we can just import this
the same way that we import sub process right here and call a mac changer function so we won't even
have to worry about the code written in this function.
Maybe in a year or two year or five year time from now, you want to be able to use a code that changes
the Mac address.
You won't have to rewrite it.
You won't even have to remember how to change the Mac address.
All you have to do is just call the function that you created before.
So let's try and bundle all of this inside the function and use it from our code.
Now, first of all, you can define a function like so.
You just have to type death.
Followed by the function name, and it's a good idea to use meaningful function names.
So we'll call this function change mark.
Okay.
Now, followed by this, you need to put two brackets.
And between these two brackets, we're going to specify the input that this function takes.
Again, going back to the input function, this function used to take a string as an input and then
do stuff with it and display it on screen as we know.
So when we're defining our function here, we need to specify what inputs it takes.
So we know in order to change the Mac address, we need an interface and we need a mac address.
So we're going to call these interface.
And new mark again.
The names here are completely up to you.
You can use any name that you find suitable.
You can use X and Y if you want it, as long as they are valid variable names.
Once you define the function name and the inputs that it takes, all you have to do is just put call
on here at the end of the line and then hit enter.
Now you'll see automatically.
PY charm has indented this and put a tab for our cursor.
This is to show that everything that we're going to write now is going to be part of the change Mac
function and it's going to be part of this block of code.
Now, the goal of this function to change the Mac address and we already know the instructions to change
the MAC address, it's these instructions right here.
So I'm just going to cut them, control X and paste them here.
What's very important to note is all of the code that is part of this function need to be indented,
the same indentation in here.
So all of the code that started here, this is part of the normal Python program or script.
Everything that's indented in here is part of the change mac function.
This is how Python separates blocks of code based on indentation.
That's it.
Now our function is done.
We have a function called change, Mark.
It takes an interface and a new mark.
And whenever we call this function, it will execute all the instructions in here.
So if we don't call this, nothing is going to happen.
Let me show you what I mean.
If I go down on my Python program and run the same command that we used to run before, and let's change
that to two, two at the end.
You can see that we didn't get a print statement.
And if I do, if config 88 zero, you'll see that the MAC address did not change.
This is because we define the function here, but we never used it in here.
So it's kind of similar to when we import a module but never use it.
So now what's happening is whenever you run, Python is going to read this, read this, read that this
is a function but not execute any of that.
And then it will go back in here and start executing from the parser.
It will read this stuff that the person enters.
It will pass them.
It will get the interface and all of that.
But it will never call change Mac.
So all we have to do in here is call change Mac.
Now, as you can see, pie chart automatically knows what I need to give to this based on the definition
in here.
So it's telling me that I need to give it an interface and we know that the value for the interface
is stored in options dot interface.
So I'm actually going to use this directly and instead of using the interface variable in here.
So I'm just going to do options that interface.
And for the Mac we're going to use options dot new Mac and I'm going to delete these two lines because
we don't need them.
They just store the value of options dot interface and interface and options dot new Mac any new Mac.
But now I'm using it directly.
I'm using options dot interface and options dot new Mac in the change mac function.
So now when Python reaches this line, it's going to read, okay, this is a call for change.
Mac, I'm going to use the interface as the first input here and use New Mac as the second input in
here.
Call this function.
Execute all of this code which will change the MAC address for me.
Let's test it out.
Going down.
Run the same exact command that we did before.
But it didn't work right now.
It printed the statement for me, which means that it actually did get into this block of code.
And then if we do, if config 88 zero, you'll see that the MAC address did change to the MAC address
that I want.
Now, as you can see, the code already looks neater.
And now every time I need to change the Mac address, I won't have to use these four lines anymore.
All I have to do is just call change Mac anywhere I want in my code.
Give it the interface, give it the new Mac and it will change it for me.
Not only that, but in the future you'll learn how to put this in a special file and be able to call
it from any Python program that you want.
This will allow you to change the Mac address within any program that you write without having to worry
about the code used in this function.
Something similar to the way that we use input, print row input and so on.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.