Return Value From Function Python
Well, many times, you might have observed that we execute a function, and that function returns some value. For example, when we want to take some user input, we make use of an input function, which returns a string. There are many other functions that return some value. So, this brings us to our question, that we can return some values from the functions that we write? Well, of course, the answer is going to be yes. We are going to explore here, that how we can return values from the function in python.
But before that, there might be another question, why would we return some value from the function? Till now we have been writing some function, which does the required task, and everything is fine till now. So why do we require that our function should return something?
Return Value From Function Python
Well, Let’s understand this with an example, of why we might require that the function should return something. Let’s consider that we want to create a function, which calculates the square of the given number. So, we simply would have a function, and it would take a number, calculate its square, and print it for us. Now, in our vision, we got the function to do what it was supposed to do. But what if we need that square in our code for further purposes? How are we going to get that?
Well, for this reason, we return some value from the function. Now, we are going to have a look at how we can return some value from the function. Let’s have the same example, where we would create a function, which would return the square of a given number.
As you can see, we have written the function, and it looks almost the same, except for that return thing, that you might be seeing for the first time. Well, the thing is that to return some value from the function, we just write return and the value that we want to return. So, remember the return keyword here.