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

main(){ unsigned int i; for(i=1;i>-2;i--) printf("c aptitude"); }

2 Answers  


# include<stdio.h> aaa() { printf("hi"); } bbb(){ printf("hello"); } ccc(){ printf("bye"); } main() { int (*ptr[3])(); ptr[0]=aaa; ptr[1]=bbb; ptr[2]=ccc; ptr[2](); }

1 Answers  


There are 21 people in a room. They have to form groups of 3 people each. How many combinations are possible? Write a C program to print the same.

1 Answers   TCS,


main( ) { char *q; int j; for (j=0; j<3; j++) scanf(“%s” ,(q+j)); for (j=0; j<3; j++) printf(“%c” ,*(q+j)); for (j=0; j<3; j++) printf(“%s” ,(q+j)); }

1 Answers  


main() { int i=-1,j=-1,k=0,l=2,m; m=i++&&j++&&k++||l++; printf("%d %d %d %d %d",i,j,k,l,m); }

1 Answers  






main() { char *p="hai friends",*p1; p1=p; while(*p!='\0') ++*p++; printf("%s %s",p,p1); }

3 Answers  


In a gymnastic competition, scoring is based on the average of all scores given by the judges excluding the maximum and minimum scores. Let the user input the number of judges, after that, input the scores from the judges. Output the average score. Note: In case, more than two judges give the same score and it happens that score is the maximum or minimum then just eliminate two scores. For example, if the number of judges is 5 and all of them give 10 points each. Then the maximum and minimum score is 10. So the computation would be 10+10+10, this time. The output should be 10 because 30/3 is 10.

0 Answers   TCS,


Write a procedure to implement highlight as a blinking operation

2 Answers  


String copy logic in one line.

11 Answers   Microsoft, NetApp,


main() { static int a[3][3]={1,2,3,4,5,6,7,8,9}; int i,j; static *p[]={a,a+1,a+2}; for(i=0;i<3;i++) { for(j=0;j<3;j++) printf("%d\t%d\t%d\t%d\n",*(*(p+i)+j), *(*(j+p)+i),*(*(i+p)+j),*(*(p+j)+i)); } }

1 Answers  


posted by surbhi just now main() { float a = 5.375; char *p; int i; p=(char*)&a; for(i=0;i<=3;i++) printf("%02x",(unsigned char) p[i]); } how is the output of this program is :: 0000ac40 please let me know y this output has come

2 Answers   GATE,


Is the following code legal? struct a { int x; struct a *b; }

2 Answers  


Categories