Functions

11 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$99.99
List Price:  $139.99
You save:  $40
€93.07
List Price:  €130.30
You save:  €37.23
£79.62
List Price:  £111.47
You save:  £31.85
CA$136.60
List Price:  CA$191.24
You save:  CA$54.64
A$151.87
List Price:  A$212.63
You save:  A$60.75
S$135.20
List Price:  S$189.29
You save:  S$54.08
HK$781.33
List Price:  HK$1,093.90
You save:  HK$312.56
CHF 90.72
List Price:  CHF 127.01
You save:  CHF 36.29
NOK kr1,092.46
List Price:  NOK kr1,529.50
You save:  NOK kr437.03
DKK kr694.20
List Price:  DKK kr971.90
You save:  DKK kr277.70
NZ$167.15
List Price:  NZ$234.02
You save:  NZ$66.86
د.إ367.25
List Price:  د.إ514.16
You save:  د.إ146.91
৳10,946.16
List Price:  ৳15,325.07
You save:  ৳4,378.90
₹8,342.71
List Price:  ₹11,680.13
You save:  ₹3,337.42
RM473.95
List Price:  RM663.55
You save:  RM189.60
₦123,487.65
List Price:  ₦172,887.65
You save:  ₦49,400
₨27,847.21
List Price:  ₨38,987.21
You save:  ₨11,140
฿3,679.53
List Price:  ฿5,151.49
You save:  ฿1,471.96
₺3,232.18
List Price:  ₺4,525.18
You save:  ₺1,293
B$511.10
List Price:  B$715.57
You save:  B$204.46
R1,851.52
List Price:  R2,592.21
You save:  R740.68
Лв181.98
List Price:  Лв254.78
You save:  Лв72.80
₩135,881.87
List Price:  ₩190,240.05
You save:  ₩54,358.18
₪371.60
List Price:  ₪520.25
You save:  ₪148.65
₱5,713.97
List Price:  ₱7,999.79
You save:  ₱2,285.82
¥15,308.41
List Price:  ¥21,432.39
You save:  ¥6,123.98
MX$1,693.57
List Price:  MX$2,371.07
You save:  MX$677.49
QR364.01
List Price:  QR509.63
You save:  QR145.62
P1,365.33
List Price:  P1,911.52
You save:  P546.18
KSh13,468.65
List Price:  KSh18,856.65
You save:  KSh5,388
E£4,794.02
List Price:  E£6,711.82
You save:  E£1,917.80
ብር5,699.43
List Price:  ብር7,979.43
You save:  ብር2,280
Kz83,741.62
List Price:  Kz117,241.62
You save:  Kz33,500
CLP$94,755.52
List Price:  CLP$132,661.52
You save:  CLP$37,906
CN¥723.62
List Price:  CN¥1,013.10
You save:  CN¥289.48
RD$5,785.38
List Price:  RD$8,099.76
You save:  RD$2,314.38
DA13,457.99
List Price:  DA18,841.73
You save:  DA5,383.73
FJ$225.30
List Price:  FJ$315.43
You save:  FJ$90.13
Q775.21
List Price:  Q1,085.33
You save:  Q310.11
GY$20,876.73
List Price:  GY$29,228.26
You save:  GY$8,351.52
ISK kr13,989.60
List Price:  ISK kr19,586
You save:  ISK kr5,596.40
DH1,008.69
List Price:  DH1,412.20
You save:  DH403.51
L1,771.31
List Price:  L2,479.90
You save:  L708.59
ден5,730.13
List Price:  ден8,022.42
You save:  ден2,292.28
MOP$803.11
List Price:  MOP$1,124.38
You save:  MOP$321.27
N$1,860.85
List Price:  N$2,605.27
You save:  N$744.41
C$3,673.63
List Price:  C$5,143.23
You save:  C$1,469.60
रु13,311.40
List Price:  रु18,636.50
You save:  रु5,325.09
S/375.46
List Price:  S/525.66
You save:  S/150.20
K383.63
List Price:  K537.10
You save:  K153.47
SAR375.03
List Price:  SAR525.05
You save:  SAR150.02
ZK2,685.42
List Price:  ZK3,759.69
You save:  ZK1,074.27
L463.14
List Price:  L648.42
You save:  L185.27
Kč2,328.17
List Price:  Kč3,259.54
You save:  Kč931.36
Ft36,248.95
List Price:  Ft50,749.98
You save:  Ft14,501.03
SEK kr1,083.98
List Price:  SEK kr1,517.62
You save:  SEK kr433.63
ARS$87,816.53
List Price:  ARS$122,946.66
You save:  ARS$35,130.12
Bs689.12
List Price:  Bs964.79
You save:  Bs275.67
COP$389,940.87
List Price:  COP$545,932.82
You save:  COP$155,991.94
₡50,893.45
List Price:  ₡71,252.86
You save:  ₡20,359.41
L2,475.75
List Price:  L3,466.15
You save:  L990.40
₲747,391.81
List Price:  ₲1,046,378.43
You save:  ₲298,986.62
$U3,833.15
List Price:  $U5,366.56
You save:  $U1,533.41
zł402.67
List Price:  zł563.75
You save:  zł161.08
Already have an account? Log In

Transcript

Hello, in this video, we are going to cover functions. And we've actually been using functions quite a bit. In Python we've just been using the built in one to create around for the benefit of creating function a allows us to encapsulate code. And if we need to reuse our code several times in different occasions, we just call a function name, also referred to as methods. And it just runs that code. So one, it reduces the amount of code you need to write to, if you need to update the code, the actual functionality, let's say you're only updating as a 10th location review where you where you have used it to date in the original function.

And it just propagates throughout your application. So the syntax of a function is like this def of just short for define that for your function name. So the function name, I'm just gonna call it awesome function that is an awesome function and you just open In close bracket, you can specify parameters as well, we'll cover that very, very soon. And now you just put a colon at the end, make sure it's indented, because that's how Python knows that all the code belongs to this function. And now, you just do what you want to do with the function. So if I were to, say, do this, if I just did print, so if I just printed something really, really simple, if I just printed Hello, world, and that's if I printed something else.

And no. Then at the end, he just needs to have a return statement. This just returns a value to the point where the method was called. You don't need to have a value away return so you can leave this as you know Default button. As you can see, once the Python ID knows that you've got a return, clicking enter will resent it off in the terminal sorted. So if I were to say awesome function, this is how you call it you do awesome function, open close bracket, specify any option parameters, as we don't have any, we're not specifying any.

So if I run it, see what we get there. So Hello, my name is Bob, and I cannot fix it. And if I were to duplicate this several times, it will run it several times. We'll show you that in a second. A number of things that in our family can add is just essentially, a function doc string. And this is just like you can think of it as like some sort of description of the function.

Folks say this. Awesome. So I'm gonna do that. And if we run this, you should get this printed out three times. So we have simplified six lines of coke, they will be in a two times, let me free times. So the six in total to free long my thinking but you know, still technically five lines, then you gotta know this as well.

Obviously, the more complex you can, the more time you use it, the more beneficial it becomes nothing you call a function. So now let's have a look at passing a value in so if I'm creating a function for the function to and I want to call this number, you can specify multiple values and just separated by a comma. So point number one Number two and in here, the docstring I'll say Add to add the number together. Obviously this is very simple operation but you get the picture and let's say or you could either store it in a variable and return that but do returns in month one plus two missing something What am I missing and I feel like I'm Why is why the indentation not working the way I want it to return you Second up, this indentation wasn't working, but it might just be this ID for example.

So if we go back we are going to print out or some function to justify two values five and six, and see what we get. We get 11 because we've added them together and returned it returns you back to the point at which it is called, which is here. This is venting, permanently a lemon there, that's the best way you can think of it. So there's you know, a bunch of other things you can do with functions as well. And when passing in values or parameters, you can pass them by reference or by values or parameter arguments in the Python language are passed by reference. So you just need if you change you know word parameter refers to within a function, the change also reflects back in the calling function.

So that's just something to bear in mind. So if I were to do a change function, and I'll just call it number one. And again, this does not conflict with this because this has local scope. This can only be used within this variable. If I have a variable here, it's only a value of five. And if I were to print out for one, then here is if I did change docstrings optional.

Now if I did vol number one, number one equals eight and adjusted return, so we're actually returning the value of Pinto All one again. Let's see what we get. So 11 and five and five. So what have What has happened? So let's take a look at this. So, we had a value of five assigned to it, and then we pass it in.

So, the argument was passed in like so. And it worked. Fine, I think I may have said you passed in by reference passing by value, if you pass in something like a dictionary, for example, or some sort of other sequence that will get passed in by venues and modify that will, you know, have modification that affect the previous original variable. So as an extra task, check that out. And I think that again, that was just a mistake on my part in between the reference and the value. So like I said, you can have parameters also Known as arguments, and we have one right here, you can have as many as you want either separated by a comma.

It is really that simple. You can also have default arguments. So, if for example we did so let's say if I had vol one in here, and if I do equals eight for example, that means if you do not assign a value to do not pass in, you will just pass in eight. So what we're going to do is with the value, so, all 1.2 that's all we're gonna do. And now we're going to wind up default, og and we're going to pass in a value of nine We're gonna do the same, but this time not catching our path and forget what we get. We get a 1016.

So why do we get that? So with the first one passing the value of nine, so, var one got overridden with nine, nine times to the 18th. But the second one, we can actually pass in a parameter. So we had a default parameter rate eight times to 16. Hence, we got printed out like that as an extra task, I want you to have a look at what happens if you don't have default arguments. So it's just like var one, for example.

But you don't pass in a parameter, you will get some sort of error. And the other thing to bear in mind is if you are creating multiple arguments, like for example, four default arguments must be at the end. That's something to bear in mind. Cuz otherwise the compiler won't know the first prime d that for which you know prompter default charges. So again doesn't bear in mind the return statement is really useful and valid, but you always need it, but you might not always specifically use it to return some sort of value. Nothing I want to cover is global and local variables and a global variable such as the global equals one for example, can be used in all of these methods as a global variable and as a next step way to try using var global and you can use it as client is something you could do before mathematical operation, but variable there are creating zoid like, like number one, for example, is only limited to the local scope.

That again, something to bear in mind where is created here, whenever I create number two, here from the value of nine that has pure, local scopes that means upon you the inside of the method here, here, or in a nova method, make sure you understand the difference between local and global scope variables. So that's it for this video. There's quite a bit more to it with functions. Again, they're just extra nuances. I'll provide a link with these videos, we can check all of that if we feel comfortable with everything we covered in here. You're all good to go.

Thank you for watching, and I'll see you in the next awesome functional video.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.