Python Beginner Tutorial 3 - Function Return Values and Debugging

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone its Kevin from kjd electronics and welcome back to another one-stop programming video for absolute beginners on Python the last video I introduced functions and variables in this video we're going to go more in depth with different types of functions and how they can be used in a more practical way other than just hello world and my first variable we're going to get right to work here and we're going to clear away everything that we had before except for a couple lines so we rid of these last two lines we're going to keep the call to main we're going to get rid of my first variable and we're going to get rid of our hello world function and we're also going to clear out our main function so we're going to add some lines up top and we're going to define a new function called add so if you recall we're going to type death the name of our function which is add and in the last video we went over our function arguments but we only ever had functions that accepted one argument you can actually have functions that accept as many arguments as you want separated by commas so our add function is going to accept two arguments called num1 and num2 so at our colon and there's our function definition so we could have gone on and on with you know num 3 and so on forever and ever we want our add function to add number one and number two well before we go ahead and implement our function we want to define a comment for what our function actually is supposed to do and the way that done in Python is as something called a docstring so to create a doc string simply type 3 quotation marks 1 2 3 and after hitting it the third time the IDE will automatically add the trailing 3 and correct that for you we want to type returns num1 plus num2 now this is actually code this is just our comment that defines what we want our function to do you know I could write one-stop programming is cool and that's perfectly fine to write because it's just a comment and it's not actually executed note that this is also indented and on the next line we react sugar and define what our function does that also needs to be indented otherwise it won't be considered as part of the function I'm going to introduce a new keyword called return and what this keyword basically does is it allows the function to return a value return a variable this is what allows functions to take in arguments do something to them and then return a result so in this case we want to add number one and number two so we'll simply type num1 plus num2 and that's our function this function is simply going to return the sum of num1 and num2 now i used the plus operator to indicate that I want to add number one and number two there's also the subtract operator the multiply operator and the divide operator which are the subtract multiply and divide operators so I'm not going to actually implement those functions quite yet as those will be left for you guys to do on your own between this video and the next video for the rest of this video we're just going to dive into what all of this really means and how we can use it it doesn't look like a lot it's really only two lines of code with a comment but it's really important to understand exactly what's going on here so down here in our main function let's call our add function a couple times let's make sure we print out the result so we're going to actually see what it's doing so I'll type print add to call our function and then our two arguments which I'm just going to arbitrarily define as one and one so that will be one instance of this and we can go ahead and run this at this point so we can see that we got two which was our expected result because we're adding one plus one let's just call a couple more just just for fun just to make sure it works two plus four and last but not less least lets you look let's do 10 plus 10 3 print calls three calls to our ads function so they expect a result so I'm also going to introduce the so this is the comment for doc string there's also a single line comment which is just the pound sign so this is should be six sex two expects six and we expect 20 here let's run our application and we get to 620 which is exactly what we expected let's really dive into this now what is this really doing if we were to step through it well let's actually step through it so if we go to run debug we're going to actually step through this program if this pops up you're going to allow the access there it didn't actually do anything different but that's because we didn't use a breakpoint so if I'm going to click in the margin over here between the actual typing area and the line numbers and that should add this red dot which is called a breakpoint so now when I click the debug button which is now a you know a little bug which if this is a play button it's not going to be in debug mode and if it's this bug it will be in debug mode as you ever need to change that you just go up to run and run versus debug so we're going to debug and now notice that we get this new window down here that's the debugging window we have all these controls but for now we're just going to use these two which are step to the next line in this file or a step over and step to the next line executed also known as step into so let's do a step into and we're going to step into our add function and we can actually see the value of num1 and num2 in the debugger here and we now we can just do step over and then step over again and then if we click on the console tab we'll see that we printed two on the next one let's just use step into so we step into again we see that we passed in two and four step into again there's nothing to step into you because it can just execute this step into again and we get six now let's just step over the last one and we get 20 and then we just finish up we can click the play button and our process will end so the debugger is really powerful we'll be using it a lot in the way in later videos but that was just a quick introduction so now let's get down to what this is actually doing in a more of a line-by-line life as a computer stand point on this first line we add one plus one but we're calling it through our add function so we go up here to line three on our add function and we can imagine that this num 1 becomes 1 and this num 2 becomes 1 so in the computer sees this and it's going to evaluate 1 plus 1 which obviously is 2 so then it's going to return to so at this point we're done up here we then come down here and since we returned the value to this whole call the function just becomes 2 so we simplified that to print 2 which is exactly what happens so let me just undo all of those changes real fast this is just if we remember from the last video print is just a function itself that takes an argument that it prints to the console but let's say instead of printing the result of 1+1 we wanted to save it so I'm going to get rid of our other two calls here and I'm going to get rid of the print statement on our 1 plus 1 I'm just going to say my result equals add 1 comma 1 now this is going to do is this is going to create a new variable called my result just like we had in the last video where we had my first variable equal hello world except instead of a constant string of hello world we've got the result of the function from calling 1 plus 1 so now what we can do is we can print my result and run that or debug rather so I'm third debugger and we will step into and we'll see that oh I didn't actually change these 2 num1 num2 well I can go ahead and change it before execute the code while it's running and now it will work as intended and we can see by scrolling over it that that's number 1 and that's num2 so now we return from that step over and now we can see right here that my result is 2 and we see that down here in the variables window as well and then when we print out we'll see that we print 2 so that's all we're going to cover in this video I encourage you to go ahead and implement the subtract multiply and divide functions on your own just like we implemented the add function here remember to include your doc string comment so you remember what your function is doing later in the next video we're going to cover user input so they can build out our calculator application to accept input from the user I'm also going to introduce the concept of variable types and how to manage variable types effectively please remember to Like comment and subscribe I'm Kevin from kjd electronics I look forward to seeing you in the next video
Info
Channel: kjdElectronics
Views: 95,355
Rating: undefined out of 5
Keywords: Python, Beginner, Tutorial, Programming, Computer Science, PyCharm, JetBrains, IDE, Python3, Python Hello World, Python Tutorial, Programming Tutorial, Education, kjdElectronics, OneStopProgramming, One Stop Programming, OSP, KJD, Scope, Visibility, Functions, Function Arguments, Variables, Function Return Values, Debugging
Id: uPwztoPBVWI
Channel Id: undefined
Length: 11min 5sec (665 seconds)
Published: Thu May 25 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.