Transcript for:
Understanding Functions in Modular Programming

foreign okay welcome to um I think this is the eighth uh c173 webinar um and what I want to cover today is where we left off last time on the last webinar uh we started talking about unit 4 here um functions and I think we stopped about Midway in unit 4.2 so that's kind of where I want to pick up our our conversation right up to this point we've well let me go back a second um up to this point we've covered uh two and three uh where we learned the fundamental constructs of programming uh via VIA flow charts and pseudocode and um they were all sequential in nature meaning all of the pseudo code all of the flow charts had a beginning point we went through a certain uh set of logic and then the program either terminated or maybe even looped over itself but it was all one big main program or one big uh one big function and so now in unit 4 what we're going to learn about is how do we take those fundamental programming constructs and start to organize that code into a more uh modular way okay and one of the ways we can do that is utilizing something called functions okay and so that's that's what unit four is all about and in the last webinar uh we went into the uh the basics about how to create a function and what that meant uh when you transfer control from let's say the main program to another function or even from a function a function can call another function right so we can get we can actually go as many levels deep as as we want with these uh functions but now in 4.2 what we're going to learn about is something called a return value um and where this comes in handy and I think I alluded to this in the last webinar is that these functions are useful not only to modularize code but they also follow the IPO Paradigm right input process oh hey we gotta we gotta attend the hey Paul uh input this uh this meeting is being recorded by the way input process and output and the return value is Handy for the uh for the output portion of the of the IPO uh Paradigm so 4.2 is about well how can we get these functions to actually return a value or output a value to the previous caller okay maybe the previous caller was the main program maybe the previous caller was another function but the whole idea is once that function is done we need to be able to take a result and hand it back to our parent function or or program okay so in order to do that um what they want you to know is we need to define a return variable or a return a return variable that contains a return value inside of the return variable and in a lot of these participation activities that are found throughout your book notice that this is one of the reasons flow charts kind of fall short is that the only way we can really see what the parameters are to a function or a main program or even the output values are to a function or main program is uh those variables or parameters need to be listed on the right hand side beside the flow chart so they're kind of uh it's kind of a disjoint set of data when we utilize these these flow charts and we'll see later on that if we use actual pseudo code instead of flowcharts it's much easier to see what the parameter variables are and it's much easier to see what the return variables are because they are actually within the pseudocode itself it's not kind of a separate chart off to the side of the main flow chart all right with that said I think we went last webinar we went through 4.2.2 here and I believe we also went through 4.2.3 and then we ran out of time so I want to kind of continue on here right past 4.2.3 and um right so we know about functions we know how to create a function uh from 4.1 we know how to read the flow charts uh that exist uh well for instance we can even review here so here is a main program let's talk about participation activity 4.2.4 and we'll see we know that this is the main program because it has a tab at the top called Main and every program starts with main that is how the computer knows where to start executing your code okay as it always looks for main so in this case this flowchart here represents main but notice that within this flowchart we have a statement here put height feet inches to centimeters so there's there's a custom user function called out here in a flowchart called height feet inches to centimeters okay and this is not a standard system function so this has to be defined somewhere or otherwise the compiler is going to yell and give you what's called a undefined error so luckily up here at the top we see that they call this out oh here's the height feet inches to centimeter Tab and this is what that particular function does okay so what they did was they broke up this program here's the main program but then the main program calls this new height feet inches the centimeter function and that is defined over here so so all functions all programs have a start and they have an end right start and they have an end um the other thing to note with this is that not only do they have a start and end but to the right hand side they always tell you what variables are associated with that particular function or program right so in this case for Maine we have a user foot user inch variable and also it lets us know remember anytime we declare variables when we're programming not anytime but for most languages you have to declare the data type you have to tell the computer what kind of value is being stored in there and that's what this integer uh this integer word is off to the side of the variables user foot and user inch is an integer data type and if we go to height feet inches to centimeter function here we're going to see a lot of variables and it's super important that we we understand what these variables are okay because when we get to functions there's something called scope okay and parameter variables are basically only seen by the function that they are defined with so this height foot and height inch parameter our input variables to this height foot inch to centimeter function okay this is the way we provide the function with input is we fill in the height foot and height inch variables with the values that we want to convert right because that's the whole purpose of this function here is to convert feet in inches to centimeters so we have to supply the feet and inches and if you look at Main that's exactly what we're doing you see the parameters height feet inches to centimeters we're passing in the feet we're passing in the inches user feet user inches the computer is going to copy the value from user feed user inches into these two variables called height feet height inches okay that's called pass by value and that's another concept that a lot of students get hung up on right is that when we pass these parameters two functions we can use whatever variables or whatever values we want here in the parent function and the computer is going to copy whatever the value is inside of these variables or whatever values you call out to these new parameter variables right so they they're not the same right they're two different scopes okay so we're going to take whatever main passes us here as a parameter user feed whatever the value is stored in user feed whatever value is stored in user inch and we're going to copy it in the height feet and height inch okay that's important because when we look at this flow chart here right centimeters per inch well that's that's a a hard-coded value there inches per feet that is a constant value there no big deal but look at total inches total inches is equal to height feet well that was passed in so we have to know what that is times inches per feet plus height inches height inches is also another parameter that was populated and passed in so if you don't realize how these values are getting populated inside of this function when it when it goes to ask you questions later on about this function you know we're going to fall kind of on our face right so very important to understand how the parent function and its values relate to the child function and its input parameters okay so that's what the parameter variables are that's the input that is fed to the actual function itself local variables these are kind of uh temporary variables that are created and exist within this function within height feet inches to centimeters but only within this function so when we go back to main main cannot see these local variables the only function that can see these local variables are this height feet inches the centimeter um function okay these variables are local in scope they can only be seen by this particular function okay so this is input this these are variables used by the process remember the IPO paradigm this is the output this return variable here defines what is going to be returned or output by that particular function okay so this defines our output so whatever we put inside of height centimeter this height centimeter variable which we can see here is the last step in our flowchart whatever we put here is going to be returned back to main which means that we're going to replace this whole height feet inches the centimeter call this function call with whatever the return value is right so yeah and Paul is saying it's 30.48 um and assuming uh the input is 5 8 uh let's let's check that out okay so 5 8 is our input that means that 5 is going to go into our height uh give me a second height feet uh parameter variable and that eight oh my gosh my computer's going a little wacky here we go so 5 right user is going to input five for feet you're just gonna put eight for inches we print centimeters to the screen and then we call height feet inches to centimeter function with five and eight as the parameters so then five and eight come in here um and then we're going to come down here we're going to Define some constants and we're going to say you know what total inches is equal to height feet which I think was eight times inches per feet which is 12. right uh um I'm sorry five feet five feet five times twelve that's 60. right plus height inches height inches is eight so sixty plus eight should be 68. okay so total inches is equal to 68. and then so we know the total inches then we're going to take 68 times centimeters per inch which is 2.54 uh this is a lot of math I'd have to get out the calculator thank you Paul it looks like 176.78 is our total height in centimeters all right so height and centimeter is going to be set to 176 let's say 0.7 176.7 we hit end and that's going to because height centimeters is our return variable right this whole function is going to return 176.7 and that's what we're going to print output is 176.7 because this particular function returned a value okay if this function did not return any values then we wouldn't print anything to the display so this concept of defining parameters for input maybe creating some local variables kind of as a temporary variables that help you achieve whatever it is the function needs to do and then return variables that need to be defined for the output this is very important because we need to know how those values get into the parameters and how the return value or the return variable comes back to the calling function all right so with that said let's see if we can answer some of these questions down below here chat um let's try number one here uh well okay so it's basically going to have us use this height feet inches to centimeter um but you know what it's actually asking us which of these are valid and so we may not even need to walk through what this function is doing right it's asking us is it valid is this a valid syntax chat what about number one chat is this a valid syntax okay we've got height feet inches to centimeter this is uh the custom function that we just talked about we're passing two parameters well this height feet inches the centimeter take two parameters sure does both of them are integers both of them are integers that looks good and then does it have a return value that we can stick into X yep sure does has a return variable that is a float and we can put that value in X so this is valid exactly exactly and then let's try another one here what about let's see if we can get a little bit fancier here what about number three chat um is this a valid statement well let's see here we've got height feet inches of centimeters we're passing it two integers that'll re remember anytime you see this the computer is going to execute this take whatever that function returns and replaces that whole thing with whatever the return value is so it's going to basically return a float it's going to give us some sort of floating value here then it's going to come over here execute this give us back and because it's valid six comma one two integers as parameters that's exactly what that function takes give us another float so we're going to have a float plus a float we add those together because there's parentheses around this whole thing and then we're going to take whatever that resulting float is divided by 2.0 can we do a float divided by 2.0 yeah sure why not and not only that then we can take that whole value and remember we always evaluate what's to the right hand side of the equals operator first right we always do the right hand side first and then whatever it evaluates to then we stick it into the variable that's on the left hand side of the equals operator so yeah it looks valid to me all right um in fact almost all of these look valid um number four using the built-in function raise the power is the following valid here what do you think chat for number four um raise the power let's assume that uh raised to power takes two parameters one is the base and then the second is uh what power are we raising it to right so 3 to the power of two that's going to give us back a floating point value and then we're going to have two more parameters raised to power and that looks good to me right because raise the power it takes two parameters right and then that's going to return back afloat and then we're going to take that float and put it inside of our variable X that looks valid and even up here this looks 100 valid too so um they didn't try to trick us here I thought at least they would try to trade us with one of with one of these all right calling functions from functions right this is where we get all fancy schmancy okay so up to this point we we had a main program calling one function and then that function uh would basically return a value back to the main program and that's as deep as the rabbit hole got however we can go as deep as we want notice that in this example right participation activity 4.2.6 we have a main program let's kind of look at the flow chart right then they're not going to expect you to be able to write the pseudocode from scratch but we do need to be able to interpret Its Behavior okay that is Paramount not only when you're when you're a programmer it's not important just to write code it's also important to be able to read code correctly because that's how you're going to identify errors and that's how you're going to figure out where to put let's say some new features some new code right so let's see if we can read this correctly um we we always start with main we know that um we're asking the user for the pizza diameter user is going to type that in we're going to print that diameter to the display so anytime you see put just think print we're going to print whatever that diameter Pizza has uh to the to the display right so we're printing the pizza diameter and then we're printing uh some prompt here letting us know what the program is doing and then we're going to print pizza calories pizza diameter okay as soon as we see this and we see these parentheses we know that a function is being called okay in particular it's this pizza calories function and that looks to me like that is not a mathematical function that is not a system function that is a custom function that the user would would need to Define right so immediately I see this and I say okay where is pizza calories defined aha up here so we have another tab up here called pizza calories we click on that and we're going to see that okay this is what pizza calories does it has another local variable called calories per square inch we assign it a constant value 16.7 apparently under 16 calories for every square inch of of pizza probably more for my kind of pizza but that's beside the point and then notice it says total calories is equal to circle area Pizza diameter with parentheses right total count is equal to circle area Pizza diameter and as soon as we see that we go aha we have another function called circle area and this also looks to be a user-defined function this does not look like a system function this does not look like a standard math function so where in the heck is circle area defined and we'll see uh it's up here with the third tab so we've got pizza calories right so Maine is calling pizza calories pizza calories is then calling circle area right and in circle area so we go in a circle area and it has another constant value the value of pi right and we're going to take the diameter that was passed in as a parameter see this is where this is important where the heck did this circle diameter guy come from oh it's a parameter it's an input parameter that the previous parent function passed to us so Pizza diameter in this case whereas Pizza diameter was also an input parameter that came from Maine right well well Main has pizza diameter and it came from the user right so we need to be able to trace the input parameters throughout all of these functions so the user inputted 12 so the user put in 12 that goes into Pizza diameter right so piece of diameter is equal to 12. so when we call pizza calories we're passing the value 12 because that's what's inside a pizza diameter that means that the value 12 then goes into this parameter called Pizza diameter here in pizza calories by the way this pizza diameter here completely different from this piece of diameter here in Maine because of scope I know they have the same name but they are two completely different variables in memory to the computer to a human well Pizza diameter Pizza diameter no no this is the main Pizza diameter variable or parameter uh variable this is pizza calories pizza diameter which was passed in as input to the function so here this is set to 12 because our parent function gave us the number 12. and in here we call circle area again with the piece of diameter which is 12. so guess what we take that 12 and we pass it in here as a parameter right come becomes input so Circle diameter is equal to 12. okay so knowing how to do that traceability with with the the parameters of your function is very important the parameters always Supply the input to your function and you need to go to the previous function to figure out what in the heck are my input parameters what is being passed to me so main has the user main gets this input from the user and passes it to pizza calories pizza calorie gets its input from Maine and passes it to circle area circle area gets its parameter from pizza calories right and it is the value 12. all right so that's how we figure out what in the heck Circle diameter is circle diameter is 12. so then Circle radius is equal to Circle diameter which is 12 divided by 2 6 and then we can do a nice little calculation here circle area is equal to Pi times the circle radius which is 12. divided by 2 excuse me uh 12 divided by 2 is 6 6 times 6. right pi r squared is circle area now so that's all fine and dandy but notice we're way down in the rabbit hole we're in circle area we've just figured out what the the value uh that circle area is going to produce uh whatever Pi is let's say three times uh six times six twelve times three let's say 36 ish okay so circle area is equal to 36. why is that important because circle area look over here is the return variable right so whatever goes inside of this return variable which is 36 which is what circle area is equal to when we come here to end that transverse control back here to pizza calories okay and it sets because circle area returns that value right the return value is 36. the computer is going to overwrite the circle area Pizza diameter with the value 36. okay well why does that matter because we need 36 times calories per square inch oh my gosh big numbers 36 times whatever 16 is chat maybe you can help me out there um somewhere around 700 calories let's say ish all right why do we need that because that means total calories is equal to 700 and notice that total calories is a return variable so whatever goes in the total calories here right is going to be returned back to Maine and so now our pizza calories call right is going to be replaced with whatever was returned by pizza calories which is 700 uh sorry 600 601.2 thank you chat okay somewhere around 600 and 1.2 calories for one piece of pizza no way anyways um I hope not um so oh for a 12 inch 12 inch diameter pizza maybe let's go with that and that has to be cheese only there's no way that anyways um so it's very important that we can look at the functions as they're defined with these flow charts and be able to trace the behavior utilize the input parameters and know what is being passed right to each one of these child functions and then we have to be able to get to the end of each one of these child functions realize what the return variable is and apply whatever that return value that is stored inside of the return variable apply that to the parent function and work your way back to main okay so um okay let's see if we if we got a kind of a grasp on this functions calling functions um okay so for instance uh 4.2.7 chat we have a function here called pizza calories per slice here's the flow chart for that particular function start and here's all the nice wonderful variables okay and it wants it it says basically our pizza calories function which is a function that's not really shown here yet returns a pizza pizza's total calories given the pizza diameter passed in a pizza slices function also not defined yet Returns the norm slices in a pizza given the pizza diameter passes as an argument so knowing all this it's going to ask us chat question one um how can we create an expression that's going to compute the total calories for a pizza with a diameter Pizza diameter what do you think Chad how can we create a statement here that is going to allow us to compute the total calories of a pizza provided we know its diameter and use the information from the original problem statement here are we going to use this pizza pizza calories per slice function are we going to use this pizza calories function are we going to use this pizza slices function let's start there chat which one of these functions would we use to compute the total calories is it going to be pizza calories per slice pizza calories or pizza slices yeah we could get all crazy and do the math ourself but but look here the problem statement lets us know a pizza calories function returns a pizza's total calories given a piece of diameter well that's exactly what they're giving us here is the pizza diameter and it wants the total calories so we can use this function here called pizza calories right somebody was nice enough to go ahead and create this nice wonderful function for us called pizza calories we don't we don't even have to do any math right that's the beauty of functions it's all all this stuff is hidden under the hood of each function we just need to know what function to call so in this case we're going to call Pizza calories and well pizza calories takes a parameter uh looks like pizza diameter passed in as an argument well we know the pizza diameter is called Pizza this variable here called Pizza diameter Dynamic diameter oh my gosh okay Pizza diameter right and so if we if we do that and it kind of runs off the screen there we can use this function called pizza calories pass in the pizza diameter and it will return the total number of uh of calories what about number two chat uh type the expression to compute the calories per slice given these three functions that it defined for us how could we figure out the pizza calories per slice and notice that if we have good naming conventions it should be very easy to look at these function names and figure out which one is going to allow us to compute the calories per slice um to me a function called pizza calories per slice is going to calculate the pizza calories per slice so let's let's use this function in order to come up with that value right pizza calories per slice right we don't really care how that function calculates the calories we just know it's this nice wonderful function uh that can do it for us now the other thing we need to know not only the name of the function but what is the input parameter necessary for this function and up here it says uh well look it doesn't tell us here in the description but here is the flow chart here are the variables associated with pizza calories per slice flow chart and notice it calls out the parameter variables that is our input right and our parameter variable for this particular function is pizza diameter so we're also going to need to supply right everybody needs to know the pizza diameter in order to do anything with any of these functions oh what total calories divided by compute the calories per slice cow pizza calories per slice uh oh placeholder B sorry it is asking us up here in the flow chart I misread the question what needs to go here within this flow chart calories per slice equals right and in this case right we're going to want to take what the total calories that's for the whole pizza and we need to calories the right calories yeah total calories divided by then we need to know the number of slices right we need to know the number of slices so luckily we have a a function here called pizza slices that tells us that Returns the number of pizza slices so let's call that function also notice that the other thing is that we don't have any variables uh up here that let us know the number of slices so we have to call this pizza slices function in order to get the total number of pizza slices um uh it says Returns the number of slices given the pizza diameter passed as an argument so not only do we need to call pizza slices we also need to pass it the uh the diameter in order so it can figure out well how many slices is in uh that pizza I thought it was always eight but maybe if you have a big enough Pizza you need more than eight slices right so we need to read these questions very carefully and you know where I messed up there was not realizing um it was talking about one particular step within the flow chart here um that you know we needed to Define okay [Applause] what is this all right I think we're good with 4.2 4.3 why in the heck are we doing all this work to Define all these functions seems like it's complicating the heck out of everything why not just have one big main program that calls everything we need stop stop adding all this complexity makes makes no sense well um this is where you kind of see what separates a good programmer from a bad programmer right and Paul has hit the nail on the head here that anytime we can make the program more modular um AKA break it down um we can work on each individual piece um and it's going to be a little more uh we can reuse those pieces in a bunch of different places for instance let me show you what I'm talking about this particular program here notice that it has one two user functions Define steps to miles steps to calories okay we and and also note that um every program can be written a hundred a hundred different ways okay at the end of the day you're in Industry you're looking for a program that is testable that is maintainable that has something called dry principles which stands for do not repeat yourself because the problem is anytime you copy and paste code and you do not follow those dry principles if that piece of code that you copied and pasted has a bug in it and let's say that you copied and pasted it into a hundred different places in your program you just copied and pasted that bug into a hundred different places in your program and now the next programmer uh has to locate all 100 places where you copied and pasted that bug everywhere and let me tell you from experience um that and especially if the if the person that did that is still working with you on your team you give them an earful okay but typically what happens is that person is gone and all of a sudden this code lands in your lap with somebody who copied and pasted the code everywhere okay it is a little bit quicker to do but in the end you shoot yourself in the foot by doing that okay now what does that have to do with this example this same example here could be written like this this is the exact same program this program in this program here are identical the only difference is they uh the only difference is they haven't modularized it yet right they everything is just one big main program and if you run this program okay if you run it's gonna run exactly the same as this program and everybody's going to look at you and go see why did you spend all that extra time modularizing and and and and applying dry principles um but the problem is that and and here it's a little bit different too because we're not copying and pasting the same code uh all over the place but even from a readability standpoint that's exactly what what Paul is alluding to here this is much easier to read because we have nice descriptive function names right steps to miles that lets us know that this code here is calculating uh the number of steps in a mile uh that here steps to calories that is letting us know that this code here is converting the number of steps into calories okay so um and also notice here we have one variable associated with main we have three variables well maybe four associated with steps to calories and we have two variables associated with steps to models down here if we had one big main program we have one two three four five six seven eight variables that we have to keep track of as we add more code right um there's something called encapsulation also that when you get into the higher level languages outside of this class and you get into object oriented programming it's very important to hide to hide the variables that each function or class is responsible for so that nobody outside of that function or outside of that class can affect those variables because here the problem is if we have a bug because miles walked let's say this this variable called miles walked is suddenly set to some random value when when the program is running if everything is in one big main program we have no idea who who went wacky and and changed miles walked to an invalid value we have to literally step through every single line of code versus if we have a modular approach and we call out these specific functions we can see well what it steps to miles return was it valid what is step to calories return was it valid we have a way of testing individual Parts okay of the program and that is very very important all right enough about that let's see if we can answer some of these participation activities uh chat um okay 4.3.3 consider the above examples right then we just kind of walk through in the example without functions so this one without functions that has not been modularized how many statements are in the main function what do you think chat how many statements are in the main function um let me see I guess we could do a quick count one two three four five six seven eight nine ten eleven twelve thirteen fourteen we have 14 whoa it's a 16. um where is 16 coming from I guess the start and the end they consider the start and the end part of the function which I guess kind of makes sense so let's go with 16. all right perfect um number two chat in the example with functions with functions how many statements are in the main function with functions well that would be this one right and notice how much cleaner and nicer and shorter the code with functions is and in this case we have one two three four five six seven eight nine nine statements in Maine we have one two three four five six statements and steps to calories and we have one two three four five in steps to miles so Maine has nine statements okay um now this is a an interesting question three chat which has fewer total statements fewer total statements the program with or without functions fewer total statements what do you think chat yep all all definitely hit the nail on the head here notice that there is some overhead right when we modularize this code when we break out these functions um we do have to add a new start and end point right uh and not only that when we go to pseudocode we're going to see that the start and end points they they they they are a uh either a return statement or a uh function uh definition at the beginning that includes the parameters and so there is a little bit of additional code and thought process that needs to happen right when we modularize the code so there is some additional code being added um when we introduce the modularization when we introduce the functions so which has few fewer total statements that's the ones that's the program above without functions that big ugly long main function and that's typically you know that's another argument from a lot of novice programmers right well it's just faster why why you know you're adding all all these these functions is taking more time I have to think about what the input parameters are and what the output parameters are I just want to throw together the code and it's going to work it's going to work just the same as as your code but at the end of the day when you have a bug and your code is not modular you're going to spend three times the amount of time debugging your code looking for that for that exact bug because you did not make it modular right so it's pay now or pay later right pay now and do a little bit of work up front to make your code nice and clean and modular and dry and your life is going to be much much easier uh later on down the road when you either have a new feature to add or a bug comes in that you have to fix okay that's what that's what the difference is between a software engineer and a programmer a programmer just kind of throws together a bunch of code that works in the end but over the long term uh good luck a software engineer is planning for the future and following good software engineering principles so that it is modular it is adaptable it is testable it is maintainable it is well commented okay what about number four chat the program with functions called functions directly in output statements okay did the program without functions directly put calculations in output statements well I don't like this question I mean this is um you got to read this question really carefully and I kind of know what they're alluding to here I don't think you would see this sort of question on the OA but essentially uh the program with functions called functions directly in output statements in other words here print steps to miles right so we are calling functions within another function in order to get it to display to the output all right did the program without functions directly put calculations in the output statements well let's look no right here's a print statement miles walked and then we just print out the variable miles walked right um and so I guess what it's getting at here is that you have to have kind of a temporary variable that stores what you want to print out before you can print it out um if you don't have functions with a return value okay so at the end of the day oops the answer is no and but I it's it's worded so strangely that did the program without functions directly put yeah and the answer is no we don't in in all of these print statements we're not calling any functions because we need a temporary variable that stores the the value before we can print it print it out I don't know don't don't get stuck on that question that's a little bit uh getting I don't know um okay modular and incremental program development this is another biggie especially for industry they ask you well how do you want to develop your code and agile and incremental development is a very uh popular uh concept right now the old older approach was something called the waterfall model which um typically was was more like a modularized development than iterative or incremental development but definitely memorize this terminology right modular development is the process of dividing programs into separate modules that can be developed and tested separately right incremental is where you write a few statements and then you test those few statements hopefully you actually write software that tests the software okay and I've got a whole whole Spiel on why automated testing is a good thing but that's a little beyond the scope of this class okay but at the end of the day um you always want to have some sort of test code set up to test each individual part of your code not just the overall uh Black Box testing right we need white box unit tests before we do our Black Box testing okay and so incremental development is a bunch of white box testing it's not just jump to uh well what does your app do show me your application show me how your application works I want to test the inside of the application not just the outside of the application okay a function stub um this is popular when you are creating a new feature and for instance down here let's say in participation activity 4.3.4 um we know that we need to somehow convert kilometers to miles so we create this convert convert kilometers to miles function within Main um but we stub it out meaning we Define it down here but we don't figure out this code part until we're all done with Main right so we we come up with the main flow chart we we name the function appropriately because we know we need this function to help us out from kilometers to miles but we don't really Define this function convert kilometers to miles until after we've we've thought about the flow of Maine first okay that's what a function stub uh basically is it's basically creating a function definition that does not have a definition inside of it yet okay um wow I can't believe the time flew so I don't know let's try a couple couple of these um number one chat 4.3.5 number one incremental development may involve more frequent execution and testing But ultimately leads to faster development of a program true or false chat member pay now or pay later right and with incremental development um again you're testing small parts of code you're writing small parts of code and then you're testing it and then you're writing and you're testing it so you have a lot more time up up front because you have to keep writing that test code you have to keep doing tests with each incremental part but that is definitely true you're going to spend more time writing the code using incremental development but it's going to be a lot safer and a lot quicker uh not quicker a lot more reliable over the long term you won't have as many bug tickets okay um a key benefit of function stubs is faster running programs what do you think chat for question three function Cubs function stubs is faster running programs yeah faster is kind of subjective a little bit I guess they're asking um but the key is recognizing this word here stubs a stub means unfinished code right you are putting kind of a placeholder within your pseudocode or within your flow chart and you're going to think about it later so um stubs aren't your program's not even going to run with a stub it's just going to allow you to think about another part of the program uh maybe a little bit faster so I think this is false yeah we're capturing high level behavior before getting distracted by low level details that's a good way to explain that is that we want to focus in on what are the high level functions here not well what what is the implementation of each one of these functions all right number four um well kind of running out of time let's see what if anything's important down here um redundant statements can be replaced by multiple calls to one function exactly right this goes back to the dry principles right notice how this main program called circle area two times with two different Pizza diameters right we could have just as easily copied this code from circle area and pasted it here into the main program twice right and that would still provide uh the area of each one of these pizzas just the same give us the same results but let's say we said divide by 3.0 here instead of 2.0 right we introduced a bug let's say that whoever created this didn't know that this should be 3.0 we'd copy that same divided by 3.0 bug into two different places and again without without those dry principles without creating a a function um we're going to copy and paste that bug everywhere and it's even worse here it's not so bad because these two sections of code are kind of right underneath of each other no big deal because if you catch a bug up here you're probably going to see it down here but if this code was copied and pasted let's say into another file in a different place with that same bug um it could it could get into production very very easily so anytime you find yourself copying and pasting code and not creating a function that is a red flag you need to back up you need to create a function figure out what the input parameters are figure out what the return value is and then basically move on okay um so real quick we got four minutes left uh question one chat a key reason for creating functions is to help the main function run faster oh that's a good question what do you think chat for number one key reason for creating functions is to help the main function run faster well notice that you know these functions they definitely help us with dry principles they definitely help us um make things more modular but it's not going to make the program run any darn faster and it's another argument from people with uh well let's just let's just get it done and it's gonna the actual executable the program is going to run just as fast as if you make it modular but it goes back to can it be tested can it be debugged can it be maintained and without good modular programming functions it's not going to be able to do all three of those things all right so it's not going to run any faster avoid uh what about question two chat avoiding redundancy means to avoid calling a function from multiple places in a program that's another good question avoiding redundancy means to avoid calling a function from multiple places in a program true or false chat and here the key to this particular question right is that it says calling a function when we're calling a function that is not redundancy that is taking a code that would have been redundant and putting it in a function so we can call that same function a hundred times in that code that does not make it redundant what would make it redundant is if we did not create that function and we copied and pasted that code 100 times okay so um that is false um number three chat if a function statements are revised all function calls will have to be modified too aha is that true or false if we change some functionality some logic within an actual function well that change be applied everywhere that function is used and the answer is absolutely it will and that's the beauty of functions if I find a bug here in in circle area this the circle area function let's say that this 2.0 is 3.0 and I go oh my God that's supposed to be 2.0 I change it here everywhere that calls circle area now has that change I fixed that bug in one place and it is applied everywhere that is the beauty of dry modular programming okay you fix in one spot and that fix is applied everywhere right so typically no if you fix it here it will be fixed for everyone the only time uh that doesn't happen is if you change the input parameters or you change the return values then you're going to have to refactor in the rest of the code okay but typically bugs are found outside within the middle here not as the input parameter or not as the return value all right last question chat and then we're out of time a benefit of functions is to reduce I'm sorry is to increase redundant statements question for a chat a benefit of functions is to increase redundant statements yeah that's false the whole benefit of functions is to not have redundant statements right dry principles do not repeat yourself dry all right so with that said we don't even have any extra time at the end of this webinar chat um I'm I'm glad some of you can make it I know attendance was light uh this time around I think we have what Christmas and five days six days away uh so that's probably why there wasn't a whole lot of people at this particular webinar but that's also why I record it and I'm going to distribute this uh just as soon as we're done here if not today tomorrow um and so please continue to email me let me know if you're finding these uh webinars useful if there are particular topics that I haven't covered yet that you'd like to see me uh go over and I will definitely do that I was hoping to get through unit 4 today uh but uh yeah you really just can't power through this stuff I mean this stuff uh especially if you're new to programming you really have to slow down and kind of talk through each different uh uh each different part of this so that's why I didn't really uh blow through this and we got to 4.3 uh so we'll pick up unit four on the next webinar okay so uh everyone have a great happy holidays and uh email me if you need anything else but otherwise I will see you in a couple weeks I think for the next webinar I'm looking at my calendar it's probably gonna be three weeks because of the holidays yeah it's probably gonna be three weeks before the next webinar so merry Christmas happy holidays and uh email me if you need anything else