Pages

Codes

Returning multiple values from a method?


Often we come across this question, how to return multiple values from a method? The snippets below describes the various approaches possible.

Normal circumstances

int f1()
{
    int x =10;
    return x;
}

The above scenario describes the most basic way a value can be returned from a method. We will expand this base to return multiple value from a method.

Using structures

This is the most efficient way to return multiple values from a method. A structure can be declared to hold as many values as desired. And the method return type can return the structure type. Below snippets shows how to do this using both pointer way and non-pointer way.

Without pointers

#include
#include   // required for strcpy() method
// declare a structure to hold multiple values
struct record
{
   int roll;
   float marks;
   char str[50];
};
// define an initialization method which return a struct
struct record init()
{
   struct record rec;
   rec.roll = 1;
   rec.marks = 10;
   strcpy(rec.str,"default");
   return rec;
}
int main()
{
   struct record temp;
   temp = init();
   printf("Default record\n Roll = %d\n Marks = %f\n Name = %s", temp.roll, temp.marks, temp.str);
   return 0;
}
Output:
Default record
Roll = 1;
Marks = 10.000000
Name = default

With pointers

#include
#include   // required for strcpy() method
#include   // required for malloc() method

// declare a struct
struct record
{
   int roll;
   float marks;
   char* str;
};

// define an initialization method to return a struct
struct record* init()
{
   // declare a pointer of type struct record
   struct record *rec;  

   // allocate some blocks of memory to the pointer 
   // type-casting infront of malloc is required because malloc allocates a memory area and return a void type
   rec = (struct record*) malloc( sizeof( struct record )); 

   // assign values to the struct members
   // since rec is a pointer type variable, ->(arrow) should be used instead of a .(dot) to access memberes
   rec->roll = 1;
   rec->marks = 10; 

   // since str is a pointer, we need to allocate memory to this too to have it hold some values
   rec->str = (char*) malloc(100);
   strcpy(rec->str,"default");

   // return the pointer. We don't need to return *rec, since rec is already declared as *rec
    return rec;
}

int main()
{
   // declare a pointer variable of type struct record
   struct record *temp;
   temp = init();

   printf("Default record\n Roll = %d\n Marks = %f\n Name = %s", temp.roll, temp.marks, temp.str);

   // free the memory used. This frees both the memory blocks allocated in init() method.
   // not deleting the memory block might work fine for this method, but will eventually have a memory 
  // leak if a complex program is written which utilizes heavy system resources
   free(temp) 

   return 0;
}

Output:
Default record
Roll = 1;
Marks = 10.000000
Name = default
   



As we saw from the above snippets, returning multiple values from a method is very easy. The structures could be extended to hold as many values as required. And a method return type signature could be used to return the structure type.

Have fun with C!



2 comments:

  1. Nice article. Can a method have a return type as class as well?

    ReplyDelete
    Replies
    1. Yes. Classes are nothing but an extension of the structures. You can have a return type of class as well, and the class can be composed of numerous member variables and member functions. So, you have access to numerous stuffs in the LHS of the calling function on return.

      Delete