How to return multiple values from a function?

Answers were Sorted based on User's Feedback



How to return multiple values from a function? ..

Answer / r. kumaran

Make array of values and return the array address as long.
Now using pointer traverse through the value.

Is This Answer Correct ?    61 Yes 19 No

How to return multiple values from a function? ..

Answer / prasanta maiti

Make array of values and return the array address as long.
simple example:
void main()
{
int a[10],i,n;
int *new_value_array;
printf("\n Enter how many number you want to enter? ");
scanf("%d",&n);
for(i=0;i<n;i++)
sacnf("%d",&a[i]);
printf("\n old value of the array is:");
for(i=0;i<n;i++)
printf("\t%d",a[i]);
new_value_array = array_modify(a,n);
printf("\n new value of the array is: \n");
for(i=0;i<n;i++)
printf("\t%d",*(new_value_array+i));
getch();
}
int *array_modify(int a[10],int n)
{
int i;
for(i=0;i<n;i++)
a[i]=a[i]*4;
return(a);
}

Is This Answer Correct ?    45 Yes 14 No

How to return multiple values from a function? ..

Answer / shruti

Multiple values can be returned from a function,
using the call by refrance method.

in this method, we pass pointers as the argument to the
funtion..

Is This Answer Correct ?    38 Yes 15 No

How to return multiple values from a function? ..

Answer / saurabh

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct
{
int a;
int b;
}Mystruct;

Mystruct myfun();

int main()
{
Mystruct ms2;
ms2 = myfun();
printf("val1: %d val2: %d",ms2.a,ms2.b);
return 0;
}

Mystruct myfun()
{
int a,b;
Mystruct ms;

a = 10;
b = 20;
ms.a=a;
ms.b=b;

return(ms);
}

Is This Answer Correct ?    14 Yes 15 No

How to return multiple values from a function? ..

Answer / manjunath sudnye

Just Make Array Of Pointer For Any Data Type And Return The
Base Address Of Thgat Pointer.
e.g
int *Ptr[5]={1,2,3,4,5};
//now see you can use 5 pointers for manupulation the
valuesand can return it by.
return Ptr;

Is This Answer Correct ?    2 Yes 5 No

How to return multiple values from a function? ..

Answer / j.nithya

we can return multiple values from a function by using pointer.... Just store your multiple values which you want to return and return the base address......Store the values in the array and return the base address of the array.

Is This Answer Correct ?    3 Yes 10 No

How to return multiple values from a function? ..

Answer / splurgeop

you can return multiple values from the function by using
mechanism called "call-by-reference".
for example:

void main()
{
int area,*circumference;
area=function(area,circumference);
cout<<"the area is "<<area;
cout<<"the circumference is"<<circumference;
}
int function(int a,int *b)
{
a=12;
int temp=a*3;
b=&temp;
return a;
}

// this is just an example............


// you can also use array and return the adress of the
array in the main and can use to traverse the entire array
and return multiple values from function.

Is This Answer Correct ?    34 Yes 60 No

Post New Answer

More C Code Interview Questions

How do I write a program to print proper subset of given string . Eg :input: abc output:{},{a},{b},{c},{a,b},{a,c},{b,c}, {a,b,c}.I desperately need this program please mail me to saravana6m@gmail.com

11 Answers   Deshaw, Infosys,


plz tell me the solution.......... in c language program guess any one number from 1 to 50 and tell that number within 8 asking question in yes or no...............

2 Answers   Wipro,


main() { printf("%d, %d", sizeof('c'), sizeof(100)); } a. 2, 2 b. 2, 100 c. 4, 100 d. 4, 4

18 Answers   HCL, IBM, Infosys, LG Soft, Satyam,


write a program to find out roots of quadratic equation "x=-b+-(b^2-4ac0^-1/2/2a"

2 Answers  


can you use proc sql to manpulate a data set or would u prefer to use proc report ? if so why ? make up an example and explain in detail

0 Answers   TCS,






C program to print magic square of order n where n > 3 and n is odd

2 Answers   Accenture,


How can i find first 5 natural Numbers without using any loop in c language????????

2 Answers   Microsoft,


I need your help, i need a Turbo C code for this problem.. hope u'll help me guys.? Your program will have a 3x3 array. The user will input the sum of each row and each column. Then the user will input 3 values and store them anywhere, or any location or index, temporarily in the array. Your program will supply the remaining six (6) values and determine the exact location of each value in the array. Example: Input: Sum of row 1: 6 Sum of row 2: 15 Sum of row 3: 24 Sum of column 1: 12 Sum of column 2: 15 Sum of column 3: 18 Value 1: 3 Value 2: 5 Value 3: 6 Output: Sum of Row 1 2 3 6 4 5 6 15 7 8 9 24 Sum of Column 12 15 18 Note: Your program will not necessary sort the walues in the array Thanks..

0 Answers  


main() { int i; clrscr(); printf("%d", &i)+1; scanf("%d", i)-1; } a. Runtime error. b. Runtime error. Access violation. c. Compile error. Illegal syntax d. None of the above

1 Answers   HCL,


program to Reverse a linked list

12 Answers   Aricent, Microsoft, Ness Technologies,


main() { int i =10, j = 20; clrscr(); printf("%d, %d, ", j-- , --i); printf("%d, %d ", j++ , ++i); } a. 20, 10, 20, 10 b. 20, 9, 20, 10 c. 20, 9, 19, 10 d. 19, 9, 20, 10

4 Answers   HCL,


void main() { int c; c=printf("Hello world"); printf("\n%d",c); }

2 Answers  


Categories