All language subtitles for 011 Returning Values From Functions_en

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranรฎ)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu

Original subtitles

Now in the previous lecture, we learned what functions are, how to define a function and how to call

it.

We also learned that functions can take input like the one that we just created and they can return

input.

But as you can see, the one that we created here, the change Mac function does not return anything

because it doesn't need to.

It only takes interface and a new Mac and it changes the Mac address for the interface.

So in this lecture, let's continue organizing our code.

We're going to create a new function.

And as we do that, we'll also have an example of a function that returns a value.

So first of all, I just want to make this tidier here, and we're going to create our new function

here and we're going to call this new function, get arguments.

Because I want this function to parse the user input and return to me the arguments and the values entered

by the user.

So it's basically, what's this code in here?

Does.

So all we have to do is just copy all of this.

And paste it in my function, but make sure that there is a tab, there is a distance in here before

we paste this so that all of this will be considered as part of the function.

Now I'm going to just delete these blank lines here.

And that's it.

We have our parser, so we have a function called get arguments.

What it does is it creates our parser object.

It adds an option of the interface.

It adds an option of the mark.

And finally it's passing the user input using parser to parse args and storing the values in options

and arguments.

The only problem is options and arguments are only accessible within this function right here.

They're only accessible within this block of code.

So even if we call this function in here at the bottom, the same way that we called our change mark.

You'll see that pie chart is still telling us that options is an unresolved reference.

It's still not going to know what options is because it's only used within the function in here.

Let me show you what I mean.

So I'm going to go down on my program and I'm just going to run the exact same command that we always

run, and you'll see that we're going to get an error.

And it's a good idea to learn how to read these errors and understand what's going wrong, because you'll

definitely get errors in the future.

So you can see that it's telling us that the error is in a file called Mac Changer Dot Pie.

So this is the file that contains my code.

The error is on line 22 and it's listing the line that contains the error and it's given us the error

type, which is a name error.

And the problem is the name options is not defined.

So going up for line 22, we can see that it's even underlined in red here because Python doesn't know

what options is.

We never used it in here.

Each block of code here is separate than the main program.

Even though we're calling get arguments and even though we're setting options and arguments to equal

to the parser dot parse args, this variable is only usable within this block of code.

So in order to capture the value that's in here, what we need to do is I'm going to delete all of this.

I'm actually going to cut it, not delete it, and we're going to say return parser to parse ARGs.

So whenever you get called I want you to execute all the code in here and return parser dot parse args

value to the location where you get called from.

So we're calling all of this on line 21 in here and because we're using this return instruction in here,

get arguments now will return whatever parser dot parse args returns and we can capture it the exact

same way that we capture parser dot parse args by doing options arguments equals get arguments.

Okay.

So all we did is we copied all the code that we used to use to parse the arguments inside the function

called get arguments.

And instead of initializing a variable at the end, we returned the value that the parser that parse

ARGs returns.

Then whenever we call this function, we capture the returned value here using other variables.

So we're using options.

Arguments equals this and then again options is being called here.

Now Python knows what options is equals to and it can run the change mac function.

So here is what's going to happen when we execute the Python program.

It's going to skip all of this.

And the first thing that it's going to do is it's going to run the get argument function.

The get argument function will read all the arguments entered by the user.

It will pass them in here.

And once done with that, it will return the two sets of information, the arguments and the values.

Because we're using the return instruction, whatever that's being returned with, this function is

being captured by options and arguments.

Then we're calling the change mark.

So Python is going to go here.

We're giving it the interface, we're giving it the mark, and then it will execute all of these commands,

which will change my Mac address.

Now let's test this code.

Using the same command.

Let's just set the Mac address to be three three at the end.

Hit enter.

Get the right print message.

And if we do, if config at zero, you'll see that my mac address did actually get changed.

Now, if we look at our code right now, it looks much, much nicer, much more readable.

And it's also reusable because we can reuse any of the functions whenever we want.

So now we have the code for the main program in here, and that's only two lines.

And then all the functionality is encapsulated into two functions.

One of them gets the arguments entered by the user, and the other one changes the MAC address based

on the argument that the user entered.

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.