Method 1 : Using Array
- If more than two variables of same type is to be returned then we can use array .
- Store each and every value to be returned in an array and return base address of that array.
Method 2 : Using Pointer and One Return Statement
- Pointer Variable can updated directly using Value at ['*'] Operator.
- Usually Function can return single value.
- If we need to return more than two variables then update 1 variable directly using pointer and return second variable using ‘return Statement‘. [ Call by Value + Call by Reference ]
EX:
#include
int* function2(int a){
int* array = (int*)malloc(2);
int b=(65*a)/100;
int c=5*8;
array[0]=b;
array[1]=c;
return array;
}
int main(void)
{
int a=18,i;
int* array;
array=function2(a);
for(i=0;i<2 div="" i="">
{
printf("array[%d]: %d\n",i,array[i]);
}
free(array);
getch();
}
2>
Method 3 : Using Structure
- Construct a Structure containing values to be returned and then return the base address of the structure to the calling function.
#include
struct dont { int x; double y; };
struct dont fred(void)
{
struct dont b;
b.x = 1;
b.y = 91.99919;
return b;
}
int main(int argc, char **argv)
{
struct dont look = fred();
printf("look.x = %d, look.y = %lf\n", look.x, look.y);
getch();
return 0;
}
No comments:
Post a Comment