Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
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.
Can you return multiple values from a function in C?
3.2.
How many values can be passed to a function?
3.3.
Which return type Cannot be used for a function in C?
4.
Conclusion
Last Updated: Mar 27, 2024
Easy

How can we return multiple values from a function?

Author Sanjana Yadav
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

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.

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;
}

Output

var1 = 40, var2 = 50, var3 = X

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;
}

Output

var1 = 40, var2 = 50, var3 = X

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;
}

Output

var1 = 40, var2 = 50, var3 = 60

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

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Frequently Asked Questions

Can you return multiple values from a function in C?

Multiple values cannot be returned from a function in C or C++.

How many values can be passed to a function?

A function can be passed any number of parameters. There are no restrictions. Stack memory is used to hold function arguments rather than heap memory.

Which return type Cannot be used for a function in C?

In C, the return-types struct, void, and char * cannot be used for functions.

 

Also Read About Difference between argument and parameter

Conclusion

In this article, we have learned to return multiple values from a function in C. We saw three different methods for the same.

We hope that this blog has helped you enhance your knowledge of functions and their return types, and if you would like to learn more about them, check out our articles Return multiple values from a function in C++ - Coding Ninjas Coding Ninjas StudioLearn Functions.

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc., you must look at the problemsinterview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Previous article
Function Declaration in C
Next article
Random Password Generator in C
Live masterclass