Return Statement | Python | Tutorial 15

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey welcome to draft Academy my name is Mike in this tutorial I want to talk to you guys about using the return statement in Python functions now a Python function is basically just a collection of Python code that performs a specific task and when we want to perform that task in our Python programs we can call a Python function and a lot of times when we're calling a Python function we just kind of call it and it does its thing and then we move on with the program but sometimes when we call a function we're actually gonna want to get information back from that function so when I call the function I want it to basically execute its task I want it to execute all the code and then I want it to like give me some information back so that could be like a particular value it could be something like telling me how the task went basically the function can just communicate back to us like hey here's some information and that's what the return keyword can allow us to do the return keyword can basically allow Python to return information from a function so I'm gonna show you guys how we can do this I want to create a very simple function and it's just going to cube a number so when we cube a number we take it to the power of three so if I was gonna cube like two it'd be like two raised to the power of three right it's just pretty basic math so we're gonna make a function that will cube a number and I'll show you guys how we can use that return statement so first thing we want to do is make a function I'm just gonna say def cube will call the function cube and over here we can specify any parameters that this function will take so basically any values that we want to give this function so we're gonna give it one function which is just gonna be a number so I'm just gonna call it num and then over here I'm gonna type out a colon and then down here we can put any of the code that we want to be included in this function so basically this function is just gonna cube this number so we could just type out num times num times num right that's gonna give us num so that's all well and good if I come down here I can execute this function so I can basically just call it I'll say cube and why don't we pass it a three so I'm gonna go ahead and pass it to three and I'm gonna save this and now let's go ahead and run this code so we're on the code and you'll see down here nothing happened right so nothing got printed out in our program in fact when we called this cube function it didn't really do anything so let me try to print this out so maybe we print this function out it'll give us back the information that we want so what about print and we'll print out the value of the function I'm just gonna click play we will look down here it says none so when I printed out the value of this function it told me none but really this function is supposed to be cubing the number and here's how we can use the return statement in Python I can come over here and I can just say return and now python is gonna return whatever value we put over here to the right so when I call this cube function and I give it a three python is gonna go off its gonna execute this function when it sees this return statement it's gonna say okay I want to give a value back to whatever call of this function and it's gonna return this value now when I print out cube 3 it should print out the answer because we get an answer back so when I play this you'll see down here it says 27 so we got the correct answer so I could do this with anything why don't we cube like 4 or something so now I'll run this and you can see we're constantly getting the correct answer back so that's the basics of using this return statement it allows us to return a value back to the caller in other words back to whatever is calling the function so I just want to illustrate this point a little bit further I'm gonna come up here I'm actually gonna create a variable so I'm gonna create a variable called result and I'm just gonna set it equal to cube for this variable result is gonna store the value that gets returned from the queue function so it's it's not gonna store like cube for it's gonna store the value that gets returned from executing that function so now if I came down here and printed out result it's actually just gonna print out 64 again because that's the value that it's storing so this return statement can be really useful for like getting information back from a function we can use something like a parameter like this for as a parameter and we can give information to a function and then we can use the return statement to get information back from a function and I do just want to point out one more thing I'm actually not able to put any code after this return statement so if I came down here and tried to put like a print statement this actually isn't gonna work so this is never gonna get printed out so if I just typed out like code right here when we run this program it's not gonna type out code that's because when I use this return keyword it breaks us out of the function so pythons gonna go through it's gonna execute the code in the function whenever it sees this return keyword it's just gonna break back out and we're gonna be done so we'll never be able to reach this line of code so that's the basics of using return statements and you can return any data type you want we could easily return like a string or a boolean or an array it doesn't really matter you can return whatever you want and a lot of times in a lot of situations you're gonna want to get a value back from a function hey thanks for watching if you enjoyed the video please leave a like and subscribe to drop acad to be the first to know when we release new content also we're always looking to improve so if you have any constructive criticism or questions or anything leave a comment below finally if you're enjoying traffic Academy and you want to help us grow head over to draft Adam e-comm forward slash contribute and invest in our future
Info
Channel: Mike Dane
Views: 138,959
Rating: 4.9401088 out of 5
Keywords: Programming
Id: nuNXiEDnM44
Channel Id: undefined
Length: 6min 15sec (375 seconds)
Published: Sat Oct 21 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.