9.11 – Returning more than one value

by subbu on November 22, 2013

How can we return multiple values from a function?

In C language, it is only possible to return a single value from a function using return statement but, we can make it possible to return different values at different incidents using a flag variable.

A function is defined with different implementations; proper implementation is executed and its result is returned according to the value we send to the flag variable.

Specification: Accept the limit, print the sum of natural numbers, sum of even and sum odd numbers from 1 to the given limit using a single function manip()

Logic:
The sub function manip() accepts two arguments that are limit and a flag. The function returns sum of natural numbers if the flag is 1, returns sum of even numbers if the flag is 2 and returns sum of odd numbers if the flag is 3.

if(flag==1)
  return sum;      /* returns sum of natural numbers */
else if(flag==2)
  return esum;     /* returns sum of even numbers   */
else if(flag==3)
  return osum;     /* return sum of odd numbers      */

Program:

#include<stdio.h>
int manip(int,int);
int main()
{
 int n;
 printf("Enter the limit:");
 scanf("%d",&n);
 printf("Sum of natural numbers %d",manip(n,1));
 printf("\nSum of even numbers %d",manip(n,2));
 printf("\nSum of odd numbers %d",manip(n,3));
 return 0;
}
int manip(int n,int flag)
{
 int i,sum=0,esum=0,osum=0;
 for(i=1;i<=n;i++)
 {
    sum=sum+i;         /* sum of natural numbers */
    if(i%2==0)
     esum=esum+i;      /* sum of even numbers    */
    else
     osum=osum+i;      /* sum of odd numbers     */
 }
if(flag==1)
 return sum;
else if(flag==2)
 return esum;
else if(flag==3)
 return osum;
}

Execution:
Enter the limit:10
Sum of natural numbers 55
Sum of even numbers 30
Sum of odd numbers 25

Specification: Accept n elements into an array and print the minimum and maximum elements of array using a single function limits()

Logic:
The function limits() accepts three arguments, that are an array, length of array and a flag variable. The function returns minimum element if the flag is 1 and returns maximum element if the flag is 2.

if(flag==1)
 return min;
else if(flag==2)
 return max;

Program:

#include<stdio.h>
int limits(int[],int,int);
int main()
{
 int a[50],n,i;
 printf("How many elements?");
 scanf("%d",&n);
 printf("Enter %d elements:\n",n);
 for(i=0;i<n;i++)
  scanf("%d",&a[i]);
 printf("Minimum element %d",limits(a,n,1));
 printf("\nMaximum element %d",limits(a,n,2));
 return 0;
}
int limits(int p[],int n,int flag)
{
 int i,min,max;
 for(min=max=p[0],i=1;i<n;i++)
 {
  if(p[i]<min)
    min=p[i];
  if(p[i]>max)
    max=p[i];
 }
 if(flag==1)
  return min;
 else if(flag==2)
  return max;
}

Execution:
How many elements?5
Enter 5 elements:
23  12  56  16  25
Minimum element 12
Maximum element 56

Note: We can return multiple values at a time to the calling function from the called by function using another technique called pass by reference, this we will discourse in later session under pointers.

Previous post:

Next post: