Conditional Multiple-Return Functions
00:10 How would you go about programming a similar type of function in Python? Well, consider what type of function we might be talking about. One function like this is one that attempts to perform a task but could fail at that attempt.
00:25 It returns an actual result if successful, but what should it return if not successful? One such example would be a function that converts a string to a number, like on a web form text input box where a user has to type in a numeric value.
01:15 It’s a built-in function of the language and even uses examples from Microsoft’s C# documentation website. C++ doesn’t have one that behaves quite like C#’s, so I’ve invented one. I won’t go over the code in this lesson, but you will see it and it will be available on the set of source code files for this course if you want to examine it more closely.
It’s actually very similar to the Python version you’ll see later in this lesson. This function,
tryParse(), attempts to convert a provided string into a numeric value—in this case, an integer. It’s coded to take into account the possibility that the string doesn’t represent a number. It takes two arguments.
Here’s a C++ version of the program Microsoft has on its website to test C#’s
tryParse() function. It creates a collection of strings to convert, or at least attempt to convert, then processes the collection one at a time, calling the
tryParse() function on each string.
Since the function returns
False based on if the conversion was successful or not, before it uses the numeric value—in this case, just to display it—it must test to see if the function returned
I’m not necessarily happy with them either, but this is the conversion the underlying C++ function performed. The point is, you still see output from both branches of the
if statement, meaning this function is performing mostly the way it should.
Since Python doesn’t have pass by reference, how would we write a function that behaves similarly? Well, the short answer is, since Python functions can return multiple values, just return both the Boolean success result and the converted number if successful. Here, we call Python’s
int() function on the string.
A call to this function should expect a tuple with two values in either case—the Boolean success result and the numeric value of
False. To use it in its current state is a little awkward, but it works. To test if the conversion was successful, you call the function on the string to parse and then check the first position, index
0, of the returned tuple. If that returns
True, you know the second position, index
1, contains the converted value.
So, here is the Python version of the
tryParse() function, same thing you saw on the slide, and here’s where we try it out. Again, calling the function and testing the element at index
0 of the tuple to see if the conversion worked or not.
07:44 So in this version, you call the function before the test, saving each element of the tuple to a separate variable. This is why it’s so important to return a tuple in both cases. Before calling the function, there’s no way to tell if the conversion will be successful or not.
08:01 That’s what the function does. But you don’t want to have a function like this return a tuple sometimes and other times not. It should always return a tuple, so in this call you can always unpack it into two separate variables. After that, the logic of this version is the same. If the conversion was successful, we use the number it converted to.
See? Same output. Just an additional step before the
else branch to save the two values returned from the function. In the next lesson, I’ll show you a third version of this, which takes advantage of a feature introduced in Python 3.8.
Become a Member to join the conversation.