Table of contents
1.
Introduction
2.
Return multiple values from a function in C
2.1.
Using Pointers
2.1.1.
Example
2.2.
Using Structures
2.2.1.
Example
2.3.
Using Arrays
2.3.1.
Example
3.
Frequently Asked Questions
3.1.
How to Return Multiple Values in a Function?
3.2.
How to Return Multiple Values in a Function in R?
3.3.
Can a Function Return Multiple Values in JavaScript?
4.
Conclusion
Last Updated: Feb 25, 2025
Easy

How can we return multiple values from a function?

Author Sanjana Yadav
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

A function is a collection of statements that work together to complete a job. Every C program contains at least one function, main(), and even the most straightforward programs can specify more functions.

How can we return multiple values from a function?

When a function completes its task, it returns a value to the script or function that called it. 

A function in C can return only one value. In this section, we will see how can we return multiple values from a function.

Also See, Sum of Digits in C and C Static Function.

Return multiple values from a function in C

We already know that the design of C functions prohibits us from returning multiple values. However, programmers frequently want a function to return multiple values. Fortunately, there are a few solutions for returning multiple values in C.

The methods for returning multiple values from a function in C are as follows:

  1. Using pointers.
  2. Using structures.
  3. Using Arrays.

Let us look at each method separately with examples

Using Pointers

In C, we may employ pointers to return more than one value from a function by passing pointers as function arguments and using them to set several values visible in the caller function.

Example

#include <stdio.h>
// Function to return multiple values using pointers
void func(int *var1, int *var2, char *var3)
{
    *var1 = 40;
    *var2 = 50;
    *var3 = 'X';
}
int main(void)
{
    int var1, var2;
    char var3;
 
    func(&var1, &var2, &var3);
    printf("var1 = %d, var2 = %d, var3 = %c", var1, var2, var3);
    return 0;
}
You can also try this code with Online C Compiler
Run Code

Output

var1 = 40, var2 = 50, var3 = X
You can also try this code with Online C Compiler
Run Code

Using Structures

Structures in C can also return more than one value from a function. In C, a structure is a user-defined datatype that may store multiple data types of the same or different types.

The objective is to construct a struct with all essential data types as members and return that struct from our function. Then, we can obtain the desired data from the struct within our caller function.

Example

#include <stdio.h>
// wrap the multiple values into a struct
struct Tuple
{
    int var1, var2;
    char var3;
};
// Function to return multiple values using struct
struct Tuple struc()
{
    struct Tuple vars = {40, 50, 'X'};
    return vars;
}
int main(void)
{
    int var1, var2;
    char var3;
    
    struct Tuple vars = struc();
    var1 = vars.var1;
    var2 = vars.var2;
    var3 = vars.var3;
    
    printf("var1 = %d, var2 = %d, var3 = %c", var1, var2, var3);

    return 0;
}
You can also try this code with Online C Compiler
Run Code

Output

var1 = 40, var2 = 50, var3 = X
You can also try this code with Online C Compiler
Run Code

Using Arrays

We've seen how to use pointers and structures to return values of different data types from a function. If all of the values are of the same data type, we may wrap them in an array and return it, as seen below:

Example

#include <stdio.h>
#include <stdlib.h>
// Function to return multiple values using an array
int *func()
{
    // Dynamically allocate memory
    int *tempVar = (int *)malloc(sizeof(int) * 3);


    *tempVar = 40;
    *(tempVar + 1) = 50;
    *(tempVar + 2) = 60;


    return tempVar;
}
int main(void)
{
    int var1, var2, var3;


    int *arr = func();


    var1 = arr[0];
    var2 = arr[1];
    var3 = arr[2];


    printf("var1 = %d, var2 = %d, var3 = %d", var1, var2, var3);
    return 0;
}
You can also try this code with Online C Compiler
Run Code

Output

var1 = 40, var2 = 50, var3 = 60
You can also try this code with Online C Compiler
Run Code

This method should not be used since the variable information is not passed to the caller function. For example, we're retrieving the values of our variables by using the array's index. Also, keep in mind that the array must be allocated dynamically in a heap. After employing the static array, it disappears when we exit the method, and accessing it within the caller function results in undefined behaviors.

Also see, Tribonacci Series and  Short int in C Programming

Frequently Asked Questions

How to Return Multiple Values in a Function?

A function can return multiple values using tuples (Python), structures (C), objects (JavaScript, Java), or out parameters (C#).

How to Return Multiple Values in a Function in R?

In R, multiple values can be returned using lists, vectors, or data frames, allowing the function to return structured data efficiently.

Can a Function Return Multiple Values in JavaScript?

Yes, JavaScript functions can return multiple values using arrays, objects, or destructuring assignments to efficiently pass multiple values.

Conclusion

Returning multiple values from a function is a common requirement in programming, and different languages provide various ways to achieve it. Whether using tuples in Python, lists in R, objects in JavaScript, or structures in C, the approach depends on the language's features and best practices. Understanding these techniques helps write more efficient and readable code, improving data handling and function flexibility.

Live masterclass