Loading....
Coupon Accepted Successfully!

 

Passing Structure to Functions

  • It is possible to pass functions inside any structure.
  • It can be done by using
    • Call by value
    • Call by reference
  • To pass an element to function it is similar to normal method of passing value to a variable.
  • For example

Struct date{

short day;

short month;

} sdate;

To access the data
Fun (sdate.day,sdate.month);
  • If one of the structure elements happens to be an array, it will automatically passed by reference as the arrays cannot be passed by value.

Program

#include<iostream.h>

#include<conio.h>

struct distance

{

int feet;

int inches;

};

main()

{

clrscr();

distance length1,length2;

void num(distance l1,distance l2);

cout<<"enter the length 1:";

cout<<"feet";

cin>>length1.feet;

cout<<"inches";

cin>>length1.inches;

cout<<"enter length2";

cout<<"feet";

cin>>length2.feet;

cout<<"inches";

cin>>length2.inches;

num(length1,length2);

}

void num(distance l1, distancel2)

{

Distance l3;

l3.feet=l1.feet+l2.feet+(l1.inches+l2.inches) / 12;

l3.inches=(l1.inches +l2.inches) %12;

cout<<"total feet"<<l3.feet;

cout<<"total inches"<<l3.inches;

return;

}





Test Your Skills Now!
Take a Quiz now
Reviewer Name