Locked lesson.
About this lesson
Understanding Program Flow in Python, and how functions can change that flow.
Exercise files
Download this lesson’s related exercise files.
Program Flow.docx59.2 KB Program Flow - Solution.docx
59.2 KB
Quick reference
Program Flow
Where you place your function code is important!
When to use
Use these concepts whenever you create functions.
Instructions
It's important that your function code gets read into memory by Python BEFORE you try to call the function.
Pay attention to functions inside of other functions.
Make sure the other function has been read into memory before you call it in another function.
Hints & tips
- Put all your functions at the top of your program!
- 00:04 We talked about functions in the last couple of videos.
- 00:06 I want to continue on and talk about something else that deals with functions,
- 00:10 specifically program flow.
- 00:12 I'm just going to create really quick
- 00:17 our namer Function one more time and
- 00:22 let's return Hello as name.
- 00:27 Okay, so if we print out namer John, save this and
- 00:32 run just to make sure it worked.
- 00:35 We get Hello John, so far, so good.
- 00:39 I mentioned earlier that functions don't get run unless you specifically call them,
- 00:44 and you call them by just writing out their name,
- 00:47 and then passing in the parameter.
- 00:48 Python starts at the beginning of every file, and
- 00:52 it starts on line one and reads down, and it goes line by line.
- 00:56 So first, it imports this module, then it clears the screen.
- 00:59 And then it sees this comment, doesn't do anything, it ignores this line.
- 01:03 And then it sees this function and it reads it into memory, but
- 01:06 it doesn't do anything.
- 01:07 We already understand this.
- 01:09 So I want to ask, if we were to copy this and
- 01:12 delete it and put it up here, what do you think would happen?
- 01:15 Well, let's find out.
- 01:16 If we save this and run it, no, we get an error.
- 01:19 What's happening is Python starting at the top like I said, does this line,
- 01:24 this line, and this line, this line comes to here, it says, okay, call namer.
- 01:28 And then Python goes, whoa, whoa, wait a second, what is namer?
- 01:32 It has no idea, because it's still on line five,
- 01:34 it hasn't gone down the line seven yet, and learned what namer is.
- 01:38 It hasn't read this into memory yet, and so we get an error.
- 01:43 So it's very very important with functions, and it's sort of the convention
- 01:47 to put all your functions at the very top of your program.
- 01:51 If you have 100 functions, put them all at the very top.
- 01:53 If you have five functions, put them all at the very top.
- 01:56 That makes sure that you don't accidentally call a function before
- 01:59 it's been read into memory.
- 02:01 So that's the program flow, so you've gotta be sure and keep an eye on that.
- 02:05 And it's not just because of this.
- 02:07 I mean, okay,
- 02:08 this is one simple function and here we know we're going to call it right here.
- 02:12 We can sort of be on the lookout and
- 02:14 make sure we put this call beneath this function.
- 02:19 But it's not always that simple because inside of a function,
- 02:21 you can call another function.
- 02:23 You can kind of lose track of where all your functions are.
- 02:25 So that's why it's a good idea just to keep them all at the top of your file.
- 02:29 So I recommend you do that.
- 02:30 So let's create a function, another function, let's call it color.
- 02:34 No parameters, and we just want to print out.
- 02:37 I like BLUE capitalized, Don't forget your colon, right?
- 02:42 So let's change this around, let's this make this really easy.
- 02:45 Let's just print this and let's just call color.
- 02:49 And if we save this and run it.
- 02:53 And then since we changed this to not need a return, we can just call our namer.
- 02:58 Let's run this guy, so we get hello John in blue.
- 03:02 Everything works fine but now if we take our function call, and
- 03:07 for instance, we stick it here right after namer.
- 03:10 In this case, namer is the function we're calling or passing in our parameters so
- 03:15 this should work, right?
- 03:16 Because this function is read into memory before we called it.
- 03:21 If we run it, we're going to get an error because inside of this function,
- 03:25 we're calling another function and that function is down here.
- 03:28 And again, Python hasn't read that in the memory.
- 03:31 The program flow hasn't reached that far yet.
- 03:33 So it starts at the beginning.
- 03:35 Does line one, line two, reads this into memory, doesn't really know what color is,
- 03:39 but that's okay, because we haven't called it yet.
- 03:42 But then right here, we call a namer and executes this and
- 03:46 then it tries to execute this, and it says, what is color?
- 03:49 I have no idea because the program itself hasn't gone pass line nine yet.
- 03:53 Just another reason why you want to keep all of your functions
- 03:58 at the top of your program before you call any of them.
- 04:02 It's just sort of best practices to do that.
- 04:04 That's program flow.
- 04:05 Very interesting and important thing to understand just a concept you need to
- 04:08 understand pretty simple just keep all your functions at the top of the file and
- 04:11 you're good to go.
- 04:12 So in the next video, we're going to look at random numbers.
Lesson notes are only available for subscribers.