简体   繁体   中英

Returning values from function

I am very new to c programming. I have written the fallowing code

 float value;  //golbal variable
 unsigned int data; //golbal variable

 void Maxphase(void) 
 { 
  float  MAX = 0.0;
   unsigned int i,index;
 for (i=0;i<=360;i++) 
 {              
    phaseset(i); 
    data = readvalue(); 
    value = voltage(data);    
    if(value>MAX)   //find max value 
    { 
      MAX = value;    //max voltage 
      index = i;   
     }  
  }                          
    printf("Max Voltage Value:%f\r\n", MAX); 
    printf("Related index Value:%d\r\n", index); 
} 

the above code working perfectly and printing maximum voltage and index. I want return both values "Max" and "index" from this function and I have to save Max value in one variable and index value in other variable like.

void runCom(void){ 
     c=getchar();
 switch(c){ 
    case '1': 
           Maxphase();
          Vin= (I want to store MAX value of that function)
          p1= ( I want to store Index of that function)
          break; 
    default:
         break;
    }
  }

Actually I want call that function and it has to return two variables MAX and index value, thus I want to store those two values in different variables.

I know function can't return two values.

I have searched, i found it is possible with a struct or make the function to handle the arguments with pointers. I tried with struct as shown below.

typedef struct {
   float v;
   unsigned int p;
  }volphase;

I have declared this struct in header file. I am including this header file in all files where i am calling.

volphase Maxphase()
{
   volphase vp;
float  MAX = 0.0;
   unsigned int i,index;
 for (i=0;i<=360;i++) 
 {              
    phaseset(i); 
    data = readvalue(); 
    value = voltage(data);    
    if(value>MAX)   //find max value 
    { 
      MAX = value;    //max voltage 
      index = i;   
     }  
  }                          
   vp.v=MAX;
   vp.p=index; 
   return vp;
  } 

This is written in "bvr.c" file. But I am thinking how to call this "struct" in case'1'(main.c) and how to store vp.v in one variable and vp.p in another variable.

Please suggest me if any thing wrong in writing struct. or any other easiest way that will return two values.

please help me how to do this.

Returning a struct from the function is the least common of the two ways to return multiple values. Using pointers is more common:

void Maxphase(float *max, unsigned int *index) 
{
    *max = 0.0;
    float value;
    unsigned int i, data;
    for (i=0;i<=360;i++) 
    {              
        phaseset(i); 
        data = readvalue(); 
        value = voltage(mux1);    
        if(value > *max)   //find max value 
        { 
            *max = value;    //max voltage 
            *index = i;   
        }  
    }                          
    printf("Max Voltage Value:%f\r\n", *max); 
    printf("Related index Value:%d\r\n", *index); 
}

Here is how you call this function:

int main() {
    float max;
    unsigned idx;
    Maxphase(&max, &idx);
    printf("Max Voltage Value:%f\r\n", max); 
    printf("Related index Value:%d\r\n", idx); 
    return 0;
}

I would return the phase from the function - the function name is MaxPhase, which implies that it returns a value of maximum phase. The index at which it found the max can be returned using a pointer.

Note that the data value is unused and mux1 is undefined. Note also that I used idx instead of index as the latter is sometimes already defined in standard libraries (although perhaps not in yours).

float MaxPhase(int * maxindex)
{
    float max = 0.0;
    int idx = -1;

    for (int i=0; i<=360; i++) {
        phaseset(i);
        unsigned int data = readvalue();
        float value = voltage(mux1);
        if (value > max) {
            max = value;
            idx = i;
        }
    }
    *maxindex = idx;
    return max;
}

void caller(void)
{
    int idx = 0;
    float phase = MaxPhase(&idx);

    printf("Max Voltage Value:%f\n", phase);
    printf("Related index Value:%d\n", idx);

    ...
}
#include <stdio.h>

struct lol
{
    float val;
    int ind;
};

void olol(struct lol *lol1)
{
    lol1->val = 5;
    lol1->ind = 3;
}

int main(void) {
    struct lol mylol = {0,0};
    olol(&mylol);
    printf("lololol %f %d \n", mylol.val, mylol.ind);
    printf("lol\n");
    return 0;
}

You can solve this in several different ways:

1.define a struct containing the two values and returning that struct.

#include <stdlib.h>
struct Values {
  int v1, v2;
};
struct Values *get2Values () {
  struct Values *x=(struct Values*)malloc (sizeof (struct Values));
  x->v1=1;
  x->v2=1231;
  return x;
}

and voila, you have a memory leak if you dont treat the returned value right...

2.use pointers as parameters where the values will go, eg Pointers:

void get2Values (int *v1, int *v2) {
  *v1=1;
  *v2=131231;
 }
 int main () {
   int a1, a2;
   get2Values (&a1, &a2);
 }

Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM