You can't return two values. However, you can return a single value that is a struct that contains two values.

Answer from Ted Hopp on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ how-can-i-return-multiple-values-from-a-function
How can I return multiple values from a function? - GeeksforGeeks
July 23, 2025 - In C programming, a function can return only one value directly. However, C also provides several indirect methods in to return multiple values from a function.
๐ŸŒ
Career Karma
careerkarma.com โ€บ blog โ€บ python โ€บ python: return multiple values from a function
Python: Return Multiple Values from a Function | Career Karma
December 1, 2023 - 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.
Top answer
1 of 7
15

It's not a sign of anything, and is not neither good nor bad design or coding style.

Returning multiple values can actually be appropriate and allow to write less code. Let's take an example of a method which takes a string like "-123abc" and converts it to an integer like -123:

(bool, int) ParseInteger(string text)
{
    // Code goes here.
}

returns both:

  • a value indicating whether the operation was a success,
  • the number converted from string.

How can we refactor this?

1. Exceptions

We can add exceptions, if the language supports them. Remember than in most languages, exceptions are expensive in resources. It means that if you have to deal with lots of non-numbers, it's better to avoid to throw an exception every time the string cannot be converted to a number.

2. New class

We can create a class and return an instance of an object of this class.

For example:

class ParsedInteger
{
    bool IsSuccess { get; set; }
    int Number { get; set; }
}

Is it easier to understand? Shorter to write? Does it bring anything? I don't think so.

3. Out parameters

If the language supports it, we can also use out parameters. This is the approach of C# where returning multiple values is not possible. For example, when parsing a number, we use: bool isSuccess = int.TryParse("-123abc", out i). I'm not sure how is it better to use out parameters compared to multiple values. The syntax is not obvious, and even StyleCop itself (the tool used to enforce the default Microsoft style rules on the code) complains about those parameters, suggesting to remove them when possible.


Finally, in languages as C# where there is no such a thing as returning multiple values, things are progressively added to imitate the behavior. For example, Tuple was added to allow returning several values without having to write your own class or use out parameters.

2 of 7
7

When your function returns a reference to an object that contains multiple members, is it returning one value or many? In the example you show, the function is actually returning an object of type tuple. Python just happens to support syntactic 'sugar' so that you don't have to explicitly dereference the members when making assignments from the return value of the function.

๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python
How to Return Multiple Values from a Function in Python | note.nkmk.me
April 23, 2025 - ... For an introduction to the basics of functions in Python, refer to the following article. ... In Python, you can return multiple values by simply separating them with commas in the return statement.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-can-we-return-multiple-values-from-a-function-in-c-cplusplus
How can we return multiple values from a function in C/C++?
In C or C++, we cannot return multiple values from a function directly. In this Article, we will see how to use some trick to return more than one value from a function. Returning Multiple Values from a Function We can return multiple values from a f
๐ŸŒ
Vultr
docs.vultr.com โ€บ python โ€บ examples โ€บ return-multiple-values-from-a-function
Python Program to Return Multiple Values From a Function | Vultr Docs
December 31, 2024 - Functions in Python are capable of returning multiple values, a feature that can significantly simplify and enhance the functionality of your programs.
Find elsewhere
๐ŸŒ
Flexiple
flexiple.com โ€บ python โ€บ python-return-multiple-values
Return multiple values from a function in Python | Flexiple Tutorials - Flexiple
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. ... #Returning Multiple Values using List def multiple(): operation = "Sum" total = 5+10 return [operation, total]; values = multiple() print(values) #Output = ["Sum", 15]
๐ŸŒ
CoreUI
coreui.io โ€บ blog โ€บ how-to-return-multiple-values-from-a-javascript-function
How to return multiple values from a JavaScript function ยท CoreUI
June 27, 2024 - In JavaScript, returning multiple values from a function can be effectively achieved using arrays and objects. This approach allows you to return more than one value from the same javascript function call.
๐ŸŒ
Cameron Nugent
cnuge.github.io โ€บ post โ€บ multi_variable
Returning multiple values from a function in R | Cameron Nugent
In this example, the function line_stats takes a single input (line) and returns 3 values. These three values are then assigned to the three variable names (question, dialogue, word_count) on the left side of the equation. This is not a viable thing to do in R. We can see below that having multiple ...
๐ŸŒ
JavaScript Tutorial
javascripttutorial.net โ€บ home โ€บ javascript tutorial โ€บ returning multiple values from a function
Returning Multiple Values from a Function in JavaScript
November 7, 2024 - And you can get the return value as an object like this: let names = getNames(); let firstName = names.firstName, lastName = names.lastName;Code language: JavaScript (javascript) If you want to unpack properties from an object, you can use the object destructuring syntax as follows: let { firstName, lastName } = getNames();Code language: JavaScript (javascript) JavaScript doesnโ€™t support functions that return multiple values.
๐ŸŒ
PHP
php.net โ€บ manual โ€บ en โ€บ functions.returning-values.php
PHP: Returning values - Manual
If the return is omitted the value null will be returned. ... A function can not return multiple values, but similar results can be obtained by returning an array.
๐ŸŒ
Drbeane
drbeane.github.io โ€บ python โ€บ pages โ€บ functions โ€บ returning_multiple.html
Returning Multiple Values โ€” Python for Data Science
It is possible for a Python function to return multiple values. To To accomplish this, we can combine the desired return values together into a list or a tuple, which we then return.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ how-to-return-multiple-values-from-a-function-in-c-or-cpp
How to return multiple values from a function in C or C++? - GeeksforGeeks
July 11, 2025 - New programmers are usually in the search of ways to return multiple values from a function. Unfortunately, C and C++ do not allow this directly. But fortunately, with a little bit of clever programming, we can easily achieve this.
๐ŸŒ
Boot.dev
boot.dev โ€บ lessons โ€บ 3c5fe40f-41e3-4d7e-a035-be67c8d83536
Learn to Code in Python: Multiple Return Values | Boot.dev
A function can return more than one value by separating them with commas. def cast_iceblast(wizard_level, start_mana): damage = wizard_level * 2 new_mana = start_mana - 10 return damage, new_mana # return two values ยท When calling a function ...
๐ŸŒ
Go by Example
gobyexample.com โ€บ multiple-return-values
Go by Example: Multiple Return Values
Next example: Variadic Functions. by Mark McGranaghan and Eli Bendersky | source | license