How can a function return multiple values
Python basically uses a tuple to achieve this. Although this is true, Python does not always require brackets to identify a tuple. Using Python Lists: Similar to the previous methods, lists can also be used to return multiple values, too. In case you are not familiar with lists, they are a collection of items. They are quite similar to arrays however they can be used to store values of various data types.
Since they are a collection of items we could add values to the list and return the list. Backend developer at a Digital Media Company.
View 6 Similar Projects. Other tutorials How to check if a list is empty in python? Looking for Freelance Jobs? View 6 Open Projects. We return a dictionary back to our main program. Our program returns the same response as earlier. This is expected because all we have done is changed the way in which we return values to our main program. Our calculation logic remains the same.
You can return multiple values from a function using either a dictionary, a tuple, or a list. These data types all let you store multiple values. There is no specific syntax for returning multiple values, but these methods act as a good substitute. The comma-separated method is more concise and easier to understand if you are working with a small set of values. Say you are returning a larger range of values from a function. You may want to use a dictionary to send multiple values back to the main program.
Do you want to accelerate your journey learning the Python programming language? Check out our How to Learn Python guide for advice on top Python courses and online learning resources. About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication. James Gallagher is a self-taught programmer and the technical content manager at Career Karma.
James has written hundreds of programming tutorials, and he frequently contributes to publications like Codecademy, Treehouse, Repl. He also serves as a researcher at Career Karma, publishing comprehensive reports on the bootcamp market. Read more by James Gallagher. With help from Career Karma, you can find a training program that meets your needs and will set you up for a long-term, well-paid career in tech. Find the right bootcamp for you.
By continuing you agree to our Terms of Service and Privacy Policy , and you consent to receive offers and opportunities from Career Karma by telephone, text message, and email. Find a top-rated training program. Find Your Bootcamp Match. Career Karma matches you with top tech bootcamps Get exclusive scholarships and prep courses. Please enter a valid phone number. Find centralized, trusted content and collaborate around the technologies you use most.
Connect and share knowledge within a single location that is structured and easy to search. Technically, you can't return more than one value. However, there are multiple ways to work around that limitation. The way that acts most like returning multiple values, is with the list keyword:. Technically, you're returning an array and using list to store the elements of that array in different values instead of storing the actual array.
Using this technique will make it feel most like returning multiple values. The list solution is a rather php-specific one. There are a few languages with similar structures, but more languages that don't. There's another way that's commonly used to "return" multiple values and it's available in just about every language in one way or another.
However, this method will look quite different so may need some getting used to. This technique is also used in some functions defined by php itself e. This might feel quite different from returning multiple values, but it is worth at least knowing about.
A third method is to use an object to hold the different values you need. This is more typing, so it's not used quite as often as the two methods above. It may make sense to use this, though, when using the same set of variables in a number of places or of course, working in a language that doesn't support the above methods or allows you to do this without extra typing.
The above methods sum up the main ways of returning multiple values from a function. However, there are variations on these methods. The most interesting variations to look at, are those in which you are actually returning an array, simply because there's so much you can do with arrays in PHP.
The most interesting part about the code above is that the code inside the function is the same as in the very first example I provided; only the code calling the function changed. This means that it's up to the one calling the function how to treat the result the function returns. Php does have the compact function that allows you to do same as above but while writing less code.
Well, the sample won't have less code, but a real world application probably would. However, I think the amount of typing saving is minimal and it makes the code harder to read, so I wouldn't do it myself. Nevertheless, here's a sample:. It should be noted that while compact does have a counterpart in extract that could be used in the calling code here, but since it's a bad idea to use it especially for something as simple as this I won't even give a sample for it.
The problem is that it will do "magic" and create variables for you, while you can't see which variables are created without going to other parts of the code. Finally, I would like to mention that list doesn't really play well with associative array.
The following will do what you expect:. If you used list with an associative array, and someone else has to change the code in the called function in the future which may happen just about any situation it may suddenly break, so I would recommend against combining list with associative arrays.
There is no way of returning 2 variables. Although, you can propagate an array and return it; create a conditional to return a dynamic variable, etc. In your example, the second return will never happen - the first return is the last thing PHP will run. If you need to return multiple values, return an array:. Since PHP 7. Thereby you can do things like this:. New features.
In PHP 5. For PHP 7. It's OK for me if you want to return variables, otherwise you should use an object with the desired properties. I know that I am pretty late, but there is a nice and simple solution for this problem. It's possible to return multiple values at once using destructuring. Its not possible have two return statement. However it doesn't throw error but when function is called you will receive only first return statement value.
We can use return of array to get multiple values in return. For Example:. Best Practice is to put your returned variables into array and then use list to assign array values to variables. That said, it's worth taking a moment and thinking about whatever you're trying to solve. While returning a complex result value like an array, or an object is perfectly valid, if you're thinking is that "I want to return two values", you might be designing poorly.
Without more detail in your question, it's hard to say, but it never hurts to stop and think twice. The answer that's given the green tick above is actually incorrect. You can return multiple values in PHP, if you return an array. See the following code for an example:. I have implement like this for multiple return value PHP function. Functions in PHP can return only one variable.
I would suggest that you look into the classes.
0コメント