Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Returning Data to the Caller

00:00 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.

00:19 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.

00:37 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.

01:02 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.

01:15 This is going to return the string 'foo'. Because it returns a value, I can save it to a variable. I want to see what the value of s is. There it is.

01:29 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.

01:59 Let’s create a new function f(). In this case, it’s going to return a dictionary. Now, perhaps in a meaningful function, there was a more complicated process to create this dictionary,

02:14 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.

02:31 If I define a function f() to return a string,

02:39 then I can slice it. So, the value of f() is that string. I can perform a slice on it

02:51 because it’s treating that return value as that type of object. Same thing if my f() returns a list.

03:12 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.

03:37 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:02 If I let t hold the result of that function,

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.

04:33 If you don’t provide a value with your return statement or you don’t have a return at all, then the function is treated—if you need to treat it as a value—as None.

04:45 So if I create a function f() that just has a return… Well, since it doesn’t have a value that we can use meaningfully, we don’t see anything.

04:59 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.

05:25 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 None.

05:44 Next up, we’re going to take a look at side effects again. We’re going to make another comparison between C++ and Python on how variables are or aren’t modified.

Become a Member to join the conversation.