Returning Data to the Caller
Now let’s look at the other use of Python’s
return statement, and that is returning data to the caller. If a
return statement inside a Python function is followed by an expression, then in the calling environment, the function call evaluates to the value of that expression.
So any value that you need to communicate back to where the function was called, you provide as a
return statement, which very often will be saved to a variable to be used however that value is needed once that function has provided it.
In this lesson, you’re going to see a lot of very simple functions, only looking at the
return statement. Again, a meaningful function is going to have significant code to do some task, we’re just looking at the last part of the process and looking at the variety of types of expressions that we can put in there and how they can be used after they’ve been returned.
I can define my function
f(). I don’t need to worry about parameters, although a function can have parameters and return something. We’re focusing in this lesson on just the things that it can return.
Depending on the type of object returned, that will have some effect on what I can do with the value when I get it. So, for example, if I redefine
f(), define a new function
f() that returns an integer, then what I could do with
s in the first case is going to be different than what I can do with it in the second case. So, for example, if I want, I can return a dictionary that my function created.
but if I call
f() and just look at its value, I can see that its value is a dictionary. But since it is a dictionary, I can treat it like a dictionary, which means I can ask it, “What does
'baz' map to in this dictionary?” and it will tell me that.
So now my
f() returns that list, and there, if I ask what the value of
f() is, I get that list. I can pick out one element of that list like I would any other list, and I can perform a slice on this, too, if I want.
And all of that works. If you want to return multiple values, you would separate them by a comma (
,), and Python packs them together as a tuple. I can create another function
f() and this is going to return multiple strings.
04:10 I can see that it got a tuple. And there, I can see that representation for that tuple. Personally, I have to say, this is a pretty cool feature. Many other languages don’t have a mechanism for returning more than one value, so Python’s ability to do this really is interesting for me personally, at least.
But if I really want to get a hold of what it returns, we do see that it gives us that
None value. And if we don’t have a
return statement… Here we see an example of a method stub where we don’t provide a body, we just say
pass. In a practical use, this would be in preparation for writing a meaningful function later.
We just need to know where to call it in some other part of the program that we are writing. But again, since this doesn’t have a
return statement and we want to interpret it with some type of value, that value would be
Become a Member to join the conversation.