Friday, July 24, 2015

How to return multiple values from a C function

#include
void fun(int ,int ,int *,int *,int *);
int main()
{
int a=30,b=20,sum,sub,pro;
fun(a,b,&sum,&sub,&pro);
print("%d %d %d\n",sum,sub,pro);
return 0;
}
void fun(int x,int y,int *s,int *su,int *mu)
{
*s=x+y;
*su=x-y;
*mu=x*y;
}

Ex: 2

#include

int main()
{
    int a=3,b=5;
    
    int *c=&a, *d=&b;
    
    int sum,avg;
     
     fun(c,d,&sum,&avg);
     
    printf("%d %d",sum,avg);
    getch();
    
}

int fun(int *a, int *b, int *s, int *avg)
{
    *s=*a+*b;
    *avg=(*a+*b)/2;
}


---------------------------------------

#include <stdio.h>
#include <conio.h>

/* This function returns an array of N even numbers */
int* getEvenNumbers(int N){
    /* Declaration of a static local integer array */
    static int evenNumberArray[100];
    int i, even = 2;
    
    for(i=0; i<N; i++){
        evenNumberArray[i] = even;
        even += 2;
    }
    /* Returning base address of evenNumberArray array*/
    return evenNumberArray;
}

int main(){
   int *array, counter;
   array = getEvenNumbers(10);
   printf("Even Numbers\n");
   for(counter=0; counter<10; counter++){
       printf("%d\n", array[counter]);
   }
   
   getch();
   return 0;




No comments: