Sometimes we require to return multiple values from a function in C. I found that many new programmers ask this question is because the C language does not allow to return multiple values from the function directly.
But no problem we have the way to take multiple values from the function and it is not much difficult. We can return the multiple values from the function using the pointer, array or structure. So let see few examples to understand the concept that how to return multiple values from a function in C.
Return multiple values from the function using the array
We can return multiple values from the function using the array but the limitation is that all values should have the same type.
In the example program, I am creating an integer array of size “n” ( int ptr[n] ) and passing this integer array to the function as the parameter to collect the first to “n” odd numbers. The function parameter type could be as int ptr[] , int ptr[n] or int *ptr.
Note: For more detail, you can see how to pass an array in the function.
#include <stdio.h> #define ARRAY_SIZE 10 // Function to get n odd numbers void collectOddNumbers(const int n, int *ptr) { int i =0; //Integer variable for (i = 0; i < n; ++i) { // Store odd numbers *(ptr + i) = ((i*2) + 1); } } int main() { //Array to store odd numbers int oddNumbers[ARRAY_SIZE] = {0}; int index = 0; // Get first 10 odd numbers collectOddNumbers(ARRAY_SIZE, oddNumbers); // Print all 10 odd numbers for (index = 0; index < ARRAY_SIZE; ++index) { printf("%d ", oddNumbers[index]); } return 0; }
Output: 1 3 5 7 9 11 13 15 17 19
You can also use the dynamic array to return multiple values from the function. Let see one more example of how we can return multiple values using the dynamic array. If you don’t know how to create a dynamic array, please see the article, how to create a dynamic array in c.
#include <stdio.h> #include <stdlib.h> #define ARRAY_SIZE 10 // Function to get n odd numbers int * collectOddNumbers(const int n) { int i =0; //Integer variable int *ptr = NULL; //Integer pointer //Allocate the memory ptr = malloc(sizeof(int)*n); //Check allocated memory if(ptr == NULL) { return NULL; } for (i = 0; i < n; i++) { // Calculate and store even number in numbers *(ptr + i) = ((i*2) + 1); } return ptr; } int main() { //integer variable int index = 0; //integer pointer int *oddNumbers = NULL; //Get first 10 odd numbers oddNumbers = collectOddNumbers(ARRAY_SIZE); // Print all 10 odd numbers for (index = 0; index < ARRAY_SIZE; ++index) { printf("%d ", oddNumbers[index]); } //free allocated memory free(oddNumbers); oddNumbers = NULL; return 0; }
Output: 1 3 5 7 9 11 13 15 17 19
If you want to learn more about the c language, here 10 Free days C video course for you.
Return multiple values from function using structure
When we require to return multiple values of the different types from the function then the structure is a good choice to use. We can also do this task with the help of pointer we will see them later. A structure is a user-defined data type, you can also see this article to understand the structure, structure in brief.
#include <stdio.h> #include <stdlib.h> //structure to store info typedef struct UserInfo { int age; float height; char name[24]; } s_Userinfo; //function to get user info s_Userinfo Getinfo(void) { s_Userinfo info = {0}; // structure variable printf("\n\n Enter User age = "); scanf("%d",&info.age); printf("\n\n Enter User height = "); scanf("%f",&info.height); printf("\n\n Enter User name = "); scanf("%s",info.name); return info; } int main() { //structure variable s_Userinfo info = {0}; //Get user info info = Getinfo(); printf("\n User age = %d",info.age); printf("\n User height = %f",info.height); printf("\n User name = %s",info.name); return 0; }
Output:
Return multiple values from a function in C- using the pointer
Using the call by reference we can get the multiple values from the function. Let see one example,
#include <stdio.h> #include <stdlib.h> //function to get user info void Getinfo(int *age, float *height, char *name) { printf("\n\n Enter User age = "); scanf("%d",age); printf("\n\n Enter User height = "); scanf("%f",height); printf("\n\n Enter User name = "); scanf("%s",name); } int main() { //variable to store age int age =0; //variable to store height float height = 0.0f; //variable to store name char name[24] = {0}; //Get user info Getinfo(&age, &height, name); printf("\n User age = %d",age); printf("\n User height = %f",height); printf("\n User name = %s",name); return 0; }
Recommended Posts for you
- how to create a dynamic array in c.
- how to pass an array in the function.
- Top 11 Structure Padding Interview Questions in C
- structure in C: you should know in-depth
- What is a flexible array member in c?
- What is the importance of struct hack in c?
- How to access pointer inside a structure in c.
- How to use the structure of function pointer in c language?
- Function pointer in structure.
- Pointer Arithmetic in C.
- Memory Layout in C.
- Union in C, A detailed Guide.
- typedef vs #define in C.
- Macro in C, with example code.
- enum in C, you should know.
- You should know the volatile Qualifier.
- 100 C interview Questions.
- Interview questions on bitwise operators in C
- 10 questions about dynamic memory allocation.
- File handling in C.
- Pointer in C.
- C format specifiers.
- C++ Interview Questions