© Distribution of this video is restricted by its owner
Transcript ×
Auto highlight
Font-size
00:05 the topic for the week is And this video will walk you through

00:12 basics of functions in python. So basic idea of a function is it

00:23 a black box that takes some input produces some output. The significance of

00:30 box is that you don't necessarily need know you know exactly what's inside the

00:37 . If it's a square root it calculates a square root for

00:41 You yourself may or may not know to do it, but it will

00:45 it for you and you can use for things even without knowing the details

00:52 . So functions are designed to be within a program or across programs.

00:59 in this course you've seen many functions import print max would be finding the

01:07 of a few numbers, the length a string, some of a series

01:12 cosin and many other Trina metric Okay, so that's the basic idea

01:17 a function. Um in terms of a function looks like, it's basically

01:25 bunch of python statements and it has names and programmers can create new functions

01:32 and a function has to be created it has to be called for it

01:37 do anything useful. So, here as simple an example and we can

01:43 of Um a function starts with a statement and then the name of the

01:50 is prentice and it takes a parameter . And inside the function you have

01:56 statement. Print Num eight times But this itself will do nothing.

02:02 when someone calls this function called print it will do something which in this

02:08 would be print a bunch of So there's the concept of a function

02:13 which is here and the concept of a function that's here. One more

02:23 with functions. Um Look at the , as you can tell, the

02:31 is simply calculating the area of a . Um that is the length and

02:39 is the width, the computation for area and it returns the area.

02:45 the point we're making, is that function does something and then it returns

02:53 net result of what it does. in this particular case here is how

03:00 function is being used. You have variable that imports the length from the

03:06 . You have a variable that inputs width from the user. Then over

03:12 it's calling the area calculation function to out what the area is. So

03:21 it finds out what the area this has the area, the areas returned

03:28 the function. And it's uh you know, in the next

03:33 we're printing the area. So basically getting the length and the width somehow

03:40 calling the function and looking at what function is returning in this case,

03:45 the area without knowing what's inside We get the area and then we

03:50 bring it to virtues or here, basically printing it. So keep

03:57 Even if you're not a little confused , you know exactly what's going

04:02 The next few minutes. We'll just over specific pieces of what we have

04:10 in in a very sort of intuitive so far. Um One more uh

04:19 that is good to know. There the concept of a fruitful and a

04:25 function. So a function that returns is fruitful. The function that does

04:33 return anything is fruitless. So here , here's an example of a fruitful

04:38 and a fruitless function. Um You need to memorize the spelling or

04:44 Just the main point really is that functions may return something. They don't

04:49 to return anything. There's both kinds functions. So now that we have

04:57 least an intuitive notion of what a does and how it is used.

05:04 try to get familiar with the exact of the components of a function.

05:12 a function has a name. Every has to have a name here is

05:19 here is a real function and here something that we're using to illustrate the

05:25 of the function. So the function is calculate area. That's the name

05:30 the function. Then it has a of parameters. The function can have

05:36 number of parameters. And over here for this area function. The parameters

05:42 length and width. It has two . Uh It doesn't even have to

05:47 parameters. It can have zero or parameters. Then there is the body

05:53 the function in this case the body just area equals length, multiplied by

06:00 in general, a body can be . It's arbitrary python code that could

06:06 1000 or 1000 lines. Finally, can have a return statement. So

06:14 it has a return statement and it a returned value, then that is

06:20 value that's returned. So in this example, the area is being

06:25 However, as we noted before, function does not have to return

06:31 It can just do something or it calculate something and return it. But

06:35 that there's only one return value. one value is returned. So just

06:42 at it for a second. If not sure what the components of the

06:46 function are. We're carrying this over the previous slide. That's again the

06:56 of the function and will not a things now that you know what?

07:05 a name, there's parameter, there's body and return value. We want

07:10 clear up a few things. A can have multiple parameters. It can

07:14 zero to any number of parameters theoretically function body can have any chord.

07:22 can have any length that can have , loop sprints other functions. Anything

07:30 is only one return value or none all. And uh functions can be

07:37 in expressions. So previously we use we the example we had was some

07:43 equals function value but you can directly the value returned by calculator function inside

07:51 expression as below. So we're covering same things we discussed just to make

07:58 that the basics are all clear. let's think for a minute, why

08:10 we even have functions? So functions not necessary in the sense that you

08:18 write any python program without functions. could not do it a python program

08:26 assignment statements. There's a lot of you must have but but a function

08:33 in theory and in principle you can anything without the function. However,

08:40 realize that that may be true in , in practice. That is that

08:45 be kind of silly. So the we use functions, there's a number

08:51 good reasons. The probably the most one is modular development or abstraction.

08:59 you're working on a big complex application then each, it has many

09:07 and the newer components built on the at the lower level build are used

09:15 higher level components. Or your problem broken up into sub problems and different

09:22 or different groups might be working on problem. Sub problems. So each

09:27 those sub problems can be captured by function. And that way when somebody

09:33 working on the whole program, they have to think of, you

09:37 every possible thing that is a component that problem at the same time.

09:44 as a very simple example, let's you're doing some complex calculation, there

09:52 something you don't need to try to it and it requires square root.

09:59 , you know how to calculate square , but it might be, you

10:02 , another 10 2030 lines of And when you are doing this

10:09 there is no reason for you to the whole program on how to compute

10:17 square root every time you need the root of a number or an

10:23 So it makes so much more sense you write one function that calculates square

10:29 and call it whenever you need Right? So when, when you're

10:34 on the square root function, you have to think of anything else,

10:37 just need to focus on. How you calculate square roots? And when

10:40 working on a higher level things, something complex with a square root,

10:47 you can just focus on what you're and not have to um worry specifically

10:54 a square root is calculated. So is the basic con of an

10:58 When you have big complex problem as solve every day, now you don't

11:04 on or you don't get large looking how immense the problem is you focus

11:10 one component at a time at a and those components could be loosely aligned

11:15 the functions so that's almost essential to anything useful. Um other reasons when

11:25 have functions, you can not only technically what the function is doing,

11:32 can also describe in english what the is doing And so that improves readability

11:41 of course compactness because if you were your 20 lines how to calculate a

11:47 root every time you needed a square , you would have, you know

11:52 of lines of code instead of maybe or 20. So it helps with

11:58 , helps with keeping the program size control and fine. Finally, something

12:05 we have visited several times in this this course which is incremental development,

12:12 always release stated and insisted that when develop we write a few lines of

12:20 and then write a few more and more and more and develop in a

12:25 which is which is the most efficient of developing a program. And functions

12:32 in handy because you can then break things into functions and say let me

12:38 this function to work. And once gotten back to work and the function

12:42 be two lines or 10 lines, something relatively short and then I can

12:48 to the next. So instead of having to decide, I'm going to

12:54 two lines, then two more lines maybe I need four lines, you

12:58 break it up into functions right, functions, you know, test

13:02 make sure they're running well and then the next function that possibly uses it

13:08 help you with developing in an incremental so that when you do run into

13:16 , they're relatively easy to fix more . So we've talked about parameters quite

13:26 bit uh in this area of the parameters were lengthened width. You

13:34 come across this terminology, formal parameter actual parameters. And it's extremely important

13:41 understand what the difference is. So formal parameter is defined when the function

13:50 defined? Remember now this is the of the function. And over here

13:58 how the function is being used. ? So in the definition, you

14:04 these names, length and width. are the formal parameters. Those are

14:09 declaring, give me any value of and width. And I'm going to

14:15 function is going to calculate the area you. And lengthen with the formal

14:21 . Now over here, when you try to use the function to calculate

14:28 , you have to give an actual for for length and width. Otherwise

14:33 meaningless to uh to compute the So over here it makes sense not

14:41 have any value, but when you're it, you have to have some

14:45 to be able to actually give, a solution. So there when you

14:53 a function again, you have matching and those are your actual parameters.

15:00 can be other variables. In which , whatever value that variable has at

15:07 point will be what goes in as actual parameter or it could even just

15:14 a number or a string or Okay, so formal parameters at the

15:20 declaration and actual parameters when you're using function. And over here quite often

15:29 will hear the term argument. So and parameters are the same thing.

15:36 it's just different people use different words describe the same concept. So don't

15:41 confused. One or the other is same and and sometimes depending on what

15:49 or what environment you're in, one is more commonly used than the

15:54 So even I and the teaching assistants sometimes call it argument, but it's

16:01 same thing for our purposes. At now we're going to cover most of

16:12 rest of the lecture on something called , scope of parameters and variables.

16:19 this is actually very important. And one of the most confusing things in

16:26 course, if I may say So if you pay attention the next

16:31 minutes, hopefully you get a good and getting the hang of what the

16:35 is all about. So getting to . Uh We've already seen that variables

16:44 defined by assigning to them. You X equals one. Suddenly you have

16:48 variable called X. So now when talked about the about this concept of

16:55 , we did not have functions. let's uh talk about variables in the

17:03 of functions. So a variable can inside the body of a function or

17:10 can be outside any variable that's inside body of a function is called a

17:15 variable. So then there are parameters you are linked with their in a

17:23 definition. Those are also local So there is a concept of local

17:29 associated with the function. Each function have its own set of local

17:35 And the point is that the local cannot be used outside there are only

17:41 , they're only relevant, only legal the function. And then you can

17:49 have variables that are defined outside of function in the main program. So

17:57 variables that are not inside of any . Those are global variables. So

18:06 two kinds of variables local that are within a function. Global that are

18:13 all over the program. So now looking what if there is a function

18:21 X. In the in the so not a function. There's a variable

18:29 X. Inside the function and there's variable called X. That is

18:35 Outside the function. Which one are talking about? The rule is that

18:39 you're inside the function first you look the local variable, is there any

18:45 variable by that name? If there , then that's the variable you're talking

18:50 ? If there isn't, Then there one outside the function a global

18:54 then that's the the variable you're talking . So hopefully this all these things

19:01 more clear in the next 10 minutes so we're just gonna focus on these

19:09 . So this is a visual illustration this program visually there are global variables

19:20 and Y. And within the read there are some local variables L.

19:26 P. And the green function. a local variable P. And

19:32 So the first thing to note is this L. And P. The

19:37 are only meaningful in red. They no meaning or no value outside of

19:43 red region, which is where the is defined. The greens are only

19:49 within the green region while this global . And Y. They're defined outside

19:56 the function and they can be accessed within outside the function or inside of

20:02 of the functions. So um if noticed there's a funk, there's a

20:09 P that is defined and function one there's a variable P defined in function

20:17 that is perfectly acceptable and perfectly You can have the same name in

20:23 functions and functions. And obviously if are referring to the valuable P over

20:31 in this area then you are referring one that is defined as high

20:37 If you're referring to it in the area then you're referring to the one

20:41 says hello. And if you're outside the green and the red areas like

20:47 P has no value, it has meaning because it's a local variable that's

20:53 meaningful inside a function. Okay, let's make this a little bit more

21:03 . So I've taken away the assignments P but I've added a new statement

21:12 . And the problem is that there already a function called Q. A

21:19 called Q. In one of the . And I've added a variable definition

21:24 Q. Outside of the function. that will be a global. So

21:30 there is a global called Q. variable called Q. And a local

21:36 called Q. So just just look this for a second. And the

21:40 is where do you get sex? you try to use the variable

21:46 Where all do you get sex? where do you get four? So

21:52 may want to stare at it for second. And then I think in

21:58 of what we've discussed already, inside green function Q will have a value

22:05 because the python rule is that uh you first look at locals, there

22:12 a local variable Q. If you print queue is gonna print for because

22:18 that's what it is. So all here we have Q. Is

22:24 Okay. And now outside of uh in the in the outside of

22:34 these functions there is no access to local variable. So all of these

22:40 we're gonna have Q Equal six. now let's focus in here.

22:48 this is a function. So first have to look at the local

22:52 Local variables. So the the python is gonna say is there a local

23:01 by the name? Q. And isn't, there is a local variable

23:08 Q. But it's not here, in the other function which is not

23:13 . So inside this red function, local variable is there is no local

23:21 called Q. Therefore the value of here will be six. So everywhere

23:28 value of Qs six, because it's global except in the function where it

23:33 defined there, it will be So two with that, that is

23:38 basics of scoping the two functions of two variables can coexist even though the

23:44 is the same, Yes, we're to cover a few more examples and

23:51 at the end of it, this this gets pretty clear. So just

23:58 at this program for a second. the same program. And uh you

24:10 want to pause the video and say gonna happen, what's gonna get printed

24:16 ? What's gonna get printed here to your thinking. But let's say you

24:20 that. So let's proceed with This is a function and it's straightforward

24:26 parameters lengthen with it returns the Okay. And over here it's being

24:33 with parameters foreign. It so this this statement here is fairly straightforward.

24:43 call the function whatever value it you put it in the variable my

24:48 and you moved on, you went the next line and you printed my

24:52 . So this should print 32. , now the question, the main

25:01 of this slide is what happens with second print statement. So the second

25:08 statement is trying to print the value the variable area. Now, what

25:16 area? Where was it defined? variable area was defined inside this function

25:24 it was returned from within the So this is the function and this

25:33 rest of it is just to the . The point is that the name

25:39 does not exist outside the function because was defined in the function. It

25:44 not been defined outside of the There is no such thing as uh

25:52 area in the we're we're trying to it so you will get an error

26:01 I do not know what area It doesn't exist around here. It

26:06 exist inside the function but we don't access to that because function variables defined

26:13 the function or not visible outside. , so likely get an error.

26:22 make sure you understand this, we'll on to another example here. The

26:32 is on parameters. The function is same. It's being used also in

26:39 similar way but we're focusing on another of scoping, we're focusing on this

26:47 name parameter called length. So in function length is a formal parameter.

26:54 being used inside uh to calculate Then area is being returned. Remember

27:02 said that parameters are like local variables the function. So when we call

27:11 call the function, we call it , we give four is the actual

27:19 length is a formal parameter. We it with four and eight. And

27:25 the area is calculated and then this print 32. There's no nothing,

27:35 tricky here. The thing you want think about is what will this

27:40 print length. So the point is length, once again does not exist

27:54 of this function. No. Where it defined outside of this function?

27:59 it doesn't exist, you cannot print . Okay, so this will return

28:04 error. I think of it over . You can't see what's in

28:11 If you can't see what's in then you're like what? Like.

28:14 , so you will get an The point of the two. These

28:19 examples is that the function parameters and variables are not visible outside. If

28:26 try to use them, try to them or you try to use them

28:30 part of an expression. It will work because they are meaningful only inside

28:35 functions and only inside the function in they are defined more examples. Once

28:49 , the best way to learn from is to pause the video and look

28:54 it yourself and see if you can out what's going on. So in

28:59 case, same function. We make definition of area here. So the

29:07 thing here is that there is a of area inside the function and we

29:14 have a definition of area outside the . After that we go ahead and

29:21 the area with parameters fall in Nothing special. So we print the

29:27 is my area. Nothing special. shirt print now the interesting thing is

29:37 happens here when we try to print . So there is an area inside

29:45 function and recently it was assigned the 32 and there is a variable called

29:51 outside. So once again the the is that when you're trying to look

30:01 what area is all about, all stuff is not visible That's inside the

30:08 and you are outside the function. if you just look at this part

30:13 area was 5000. So it stays just because a function used the variable

30:20 that name for something and returned, is not relevant. You will print

30:26 here too, which is just because what was defined here. This function

30:31 can't affect the value of global variable that. To the point being that

30:44 there's a global and local variable, local is affected inside the function.

30:50 when you did made these assignments that to the local, not to the

30:55 , that was outside another example. , take a look for a

31:11 So this is a different kind of um it's kind of still about scoping

31:19 point here is that you have this calculate area. Not that the function

31:26 changed now, it's not the same as before. This function is not

31:32 any argument, it's empty inside the . It has length multiplied by wit

31:40 outside Lanqing is has lengthened with have definition and at that point the Calgary

31:48 function is called. So the question , what will this function do?

31:55 has not been called with any It is being asked to multiply these

32:04 lengthen width and those variables are there they were defined outside the function.

32:14 , so your instinct might be that know, no, I can't see

32:19 variables are not inside the function but this program will work fine and you

32:27 calculate the area and this should print . Okay. The point to remember

32:38 you can see global variables outside the but you cannot see from outside

32:45 So in here in the function if is no variable called length and width

32:52 go and see is there any variable And once you are looking outside you

32:58 that there are these variables and you those variables to calculate the area.

33:03 , so reiterating that you can see variables that are defined outside the function

33:10 inside but not the other way around outside, you cannot see um the

33:18 variables inside the function. So this are this is global and there is

33:24 local variable and global variables are visible functions and outside functions. So the

33:35 works however, don't do it. is not how you write functions.

33:41 is a really poor way of writing where you have some variables and then

33:48 assume that the function will get to and do some calculation the right way

33:54 what we saw in previous pages where arguments length and which are passed on

34:00 the function, not just function knowing name it will have outside and using

34:07 in particular. Now you cannot use function with any other variables with any

34:13 names because it's trying to directly access values. So that's the one point

34:17 , it's good to know what works what doesn't, but it's highly not

34:23 . And the other thing here, we will not discuss fully is that

34:30 cannot change the value of these global . Global variable is something that they

34:35 outside the function. You cannot change values inside at least for now,

34:42 mean there are some exceptions but we worry about it right now. Right

34:47 assume that a global variable that is outside of a function can only be

34:54 outside of the function, it can accessed inside a function inside a

35:00 you can print its value and you use the value to do things just

35:05 you did here, but you cannot its value. So that brings us

35:14 this example. So this is a of the previous example and and you

35:26 probably guess that something is gonna go because uh lengthen with our global variables

35:35 inside the function we're trying to modify . Okay, so this is a

35:44 tricky note that if these statements were there, there's no problem. We're

35:52 to the previous example where we were to calculate the length multiplied by West

35:59 we've added these statements so that does up things. The way, you

36:08 , if a variable is local or is where it is defined.

36:14 so here you look at this and not looking at, you're not looking

36:20 anything else, You're just looking at part, You're just looking at the

36:26 lengthen wit are being defined inside the . Okay, So which means they're

36:34 variables. And if the local variables have a problem, The problem is

36:40 you're trying to multiply length with with they have any value. So these

36:47 local variables uh and independent of these global variables, therefore, length

36:56 since lengthen with do not have any uh any value when you're trying to

37:04 them and get the area, this not meaningful. This should cause some

37:08 of an error. Standard error. you have a local variable, you

37:13 not defined its value and you're trying use it. Okay, So that's

37:18 point of this somewhere there, you'll an error saying this looks a lot

37:23 a local and you're using a local in the wrong way. Remember again

37:30 local that once you have an assignment a function you have a local variable

37:36 matter that it is already assigned and elsewhere. As long as there's a

37:45 of a variable inside a function, doesn't matter what's there in any other

37:50 or outside the function, it's a variable and it is treated like a

37:54 variable. Okay, once again, cannot be assigned inside the function and

38:03 are exceptions that we will we will about possibly later in the course,

38:10 not important but to assume for now the global cannot be modified cannot be

38:16 inside a function. So we looked all kind of strange examples with the

38:28 same name of variables as a as a global as a local and

38:34 on. The point of that was to make clear how the scoping in

38:43 works in real world. Do not that. Do not put the same

38:49 inside the function and outside the function use, you know, it's always

38:54 to use different names for different Even if you're having, you

38:59 you're talking about length and width Inside function you're talking about lengthen with outside

39:05 function, you still want to call different, different things. And that

39:10 avoid a lot of in a lot confusion for example, you could call

39:16 my length and my word and then them uh you know, like use

39:24 and then then you have no problem a conflict because it's the same.

39:45 , the last slide here is an one. It is the flow of

39:51 inside a python program so far, the python programs we have seen and

39:58 have seen a kind of mini er for the for loop, so you

40:03 , if this is the program you from top to bottom except that if

40:10 a loop then you may go around few times and then you keep going

40:15 that's what every program we have seen to functions and with functions it's a

40:22 bit different. Remember the definition of function is not something that does anything

40:30 you call them. Okay, so see what happens here when python interpreter

40:36 from top to bottom trying to say to do with this particular program.

40:41 program has an area function and a function, then a bunch of court

40:47 to use them. So it goes here says, oh, this is

40:53 function definition. Have nothing to do it until someone calls the function.

40:59 it goes, I'll just put some then it goes down here again,

41:05 function definition, nothing to do. get to it if anybody ever called

41:10 function at that point you reach So this will be the first thing

41:16 will actually get executed. That the interpreter or python will do anything.

41:23 will assign the value for two variable land next it will assign the value

41:30 to the other variable migrant. Then looks over here going forward with execution

41:38 says hey they're calling a function called area. Where is that function?

41:44 it goes and finds out the function over here. Execution goes all the

41:50 here um and then it will you know, this will be assigned

41:57 this will be assigned in it will it will go it will go calculate

42:01 come to the return statement. Now about it. What happens next now

42:07 done with the function call. Do go to the next statement after the

42:15 ? No. You go back to the function was called. Okay.

42:22 you're done with this thing. You to return this value of area to

42:27 variable with which the function was That's here. So you'll go here

42:34 this will get this value and now done executing up to here.

42:41 The next step you will um go the next statement which is another function

42:49 this time. It's calling the function cal perry which is to calculate the

42:54 . Now what do we do? have to go find this function?

42:58 that's also up there. So from the execution will come here to calculate

43:03 parameter and once uh we're over there will calculate and return the parameters.

43:12 as before the after being done with function, it will go back to

43:18 this was being called from at this . My perry will get the value

43:26 of the perimeter returned by the cal function. My area already has the

43:34 of area that is returned by the function and it will go to the

43:39 statement, print and print those two . So the main point is you

43:46 the execution from top to bottom. that you skip over functions and you

43:53 back to the function um function chord the function is called. And as

44:01 as you're done with the function execution go back to the point where the

44:07 call was made and continue. And is also something where you can benefit

44:19 the the the tools provided to you tracking execution. So there's the book

44:30 your options. So you can as saw you can uh follow the execution

44:37 the court, you can use this that will be helpful. You can

44:40 see where the execution is going as as your program proceeds step by

44:49 that's all we have for function today functions today. Thank

-
+