Sunday, 5 July 2020

Change/return more than value from function | C Language

Q : Write down three different methods used for referencing multiple values from a function.


Answer : you can return only one value from function using return statement but you can change multiple values from a function using a pointer, array, and structure. The following program will show you a demo. 




#include<stdio.h>
#include<conio.h>
#include "mylib.h"
/*============================
     Girfa Student Help
     Multiple value set from function
==============================*/
struct data
{
     int roll;
     int marks;
};

void swap(int*,int*);
void setzero(int[]);
void change_struct(struct data*);
void main()
{
  int a=10,b=20;
  int ar[5]={1,2,3,4,5};
  struct data ob={101,50};
  clrscr();
  swap(&a,&b);
  printf("\n1. By Pointer A=%d,B=%d",a,b);
  setzero(ar);
  printf("\n2. Printing array changed value\n");
  for(a=0;a<5;a++)
     printf("\t%d",ar[a]);
  change_struct(&ob);
  printf("\n3. Printing changed struct value Roll=%d,Marks=%d",ob.roll,ob.marks);
  getch();
}
void change_struct(struct data *ob)
{
     ob->roll=0;
     ob->marks=0;
}
void setzero(int ar[])
{
     int i;
     for(i=0;i<5;i++)
           ar[i]=0;
}
void swap(int *a,int *b)
{
     int tmp;
     tmp=*a;
     *a=*b;
     *b=tmp;
}


No comments:

Post a Comment