codescracker


c++

C++ Passing Structure to Function



« Previous Tutorial Next Tutorial »


So far, all structures used in the preceding examples have been global and hence were available to all the functions within program. But, if you have a structure local to a function and you need to pass its values to another function, then it can be achieved in two ways :

Both these ways can be achieved by call by value as well as call by reference method of passing variables. Let's discuss these in details.

Passing Structure Elements to Functions

When an element of a structure is passed to a function, you are actually passing the values of that element to the function. Therefore, it is just like passing a simple variable (unless, of course, that element is complex such as an array of character). For example, consider the following structure :

struct date
{
	short day ;
	short month ;
	short year ;
}Bdate ;

Individual elements of this structure can be passed as follows :

func1(Bdate.day, Bdate.month, Bdate.year) ;

The above function-call invokes a function, func1() by passing values of individual structure elements of structure Bdate.

The function can either receive the values by creating its own copy for them (call by value) or by creating references for the original variables (call by reference). If You want that the values of the structure elements should not be altered by the function, then you should pass the structure elements by value and if you want the function to alter the original values, then you should pass the structure elements by reference.

But remember if one of the structure elements happens to be an array, it will automatically be passed by reference as the arrays cannot be passed by value.

Passing Entire Structure to Function

Passing entire structures makes the most sense when the structure is relatively compact. The entire structure can be passed to the functions both ways by value and by reference. Passing by value is useful when the original values are not to be changed and passing by reference is useful when original values are to be changed.

C++ Pass Structure to Function Call by Value

When a structure is used as an argument to a function, the entire structure is passed using the standard call-by-value method. Of course, this means that any changes made to the contents of the structure inside the function to which it is passed do not affect the structure used as an argument.

The receiving parameter for the passed structure must match the type of the passed structure.

Passing Structure to Function Call by Value Example

Consider the following example program, demonstrating how to pass structure to function with call by value method:

/* C++ Passing Structure to Function - Call by Value */

#include<iostream.h>
#include<conio.h>

struct distance
{
	int feet;
	int inches;
};

void prnsum(distance l1, distance l2);  // function prototype
void main()
{
	clrscr();

	distance length1, length2;   // two structures of type distance declared

	/* Read values for length1 */
	cout<<"Enter length 1:\n";
	cout<<"Feet: ";
	cin>>length1.feet;
	cout<<"\nInches: ";
	cin>>length1.inches;

	/* Read values for length2 */
	cout<<"\n\nEnter length 2:\n";
	cout<<"Feet: ";
	cin>>length2.feet;
	cout<<"\nInches: ";
	cin>>length2.inches;
	prnsum(length1, length2);      // print sum of length1 and length2

	getch();

}     // end of main()

void prnsum(distance l1, distance l2)
{
	distance l3;      // new structure
	l3.feet=l1.feet+l2.feet+(l1.inches+l2.inches)/12;  // 1 feet=12 inches
	l3.inches=(l1.inches+l2.inches)%12;
	cout<<"\n\nTotal Feet: "<<l3.feet<<"\n";
	cout<<"Total Inches: "<<l3.inches;
}

When the above C++ program is compile and executed, it will produce the following output:

c++ passing structure to function

The above program inputs two structures length1 and length2 of distance type and prints the sum of the two.

A function prnsum() is invoked by passing two structures length1 and length2 by value and which calculates the sum of the two and prints it. The function prnsum() creates its own copies for length1 and length2 namely l1 and l2 and works with it. Thus the original copies length1 and length2 remains untouched.

Passing Structure to Function Call by Reference

Structures can be passed by reference just as other simple types. When a structure is passed by reference the called function declares a reference for the passed structure and refers to the original structure elements through its reference. Thus, the called function works with the original values.

Passing Structure to Function Call by Reference Example

Following example program illustrates passing of structures to function with call by reference method:

/* C++ Passing Structure to Function
 * Passing Structure to Function with
 * Call by Reference Method in C++ */

#include<iostream.h>
#include<conio.h>

struct distance
{
	int feet;
	int inches;
};

void prnsum(distance &l1, distance &l2);  // watch the declaration

void main()
{
	clrscr();

	distance length1, length2;   // two structures of type distance declared

	/* Read values for length1 */
	cout<<"Enter Value for length 1:\n";
	cout<<"Feet: ";
	cin>>length1.feet;
	cout<<"\nInches: ";
	cin>>length1.inches;

	/* Read values for length2 */
	cout<<"\n\nEnter Value for length 2:\n";
	cout<<"Feet: ";
	cin>>length2.feet;
	cout<<"\nInches: ";
	cin>>length2.inches;
	prnsum(length1, length2);   // print sum of length1 and length2

	getch();
}

void prnsum(distance &l1, distance &l2)
{
	distance l3;          // new structure created
	l3.feet=l1.feet+l2.feet+(l1.inches+l2.inches)/12;  // 1 feet=12 inches
	l3.inches=(l1.inches+l2.inches)%12;
	cout<<"\n\nTotal Feet: "<<l3.feet<<"\n";
	cout<<"Total Inches: "<<l3.inches;
}

When the above C++ program is compile and executed, it will produce the following output:

c++ passing structure to function call by value reference

The above program invokes prnsum() by passing structures length1 and length2 by reference. The function prnsum() creates references l1 and l2 for structures length1 and length2 and thus, uses the original structures length1 and length2 by names l1 and l2 respectively.

Returning Structures from Functions in C++

Just like other types, functions can return structures also. Then the return type of the function is the same as that of the type of the structure returned. For example, if the function prnsum() (of the above 2 programs i.e. Program No. 1 and Program No. 2) has to return a structure of type distance, its declaration will change as shown below :

distance prnsum(distance L1, L2) ;

See, the return type of prnsum() is distance. For Program No. 1 i.e., when structures are passed by value and as

distance prnsum(distance &L1, distance &L2) ;

For Program No. 2 i.e, when structures are being passed by reference. The definition of the function prnsum() of program no. 1 will be as follows :

distance prnsum(distance L1, distance L2)
{
	distance L3 ;
	L3.feet = L1.feet + L2.feet + (L1.inches + L2.inches) / 12 ;     // 1 feet = 12 inches
	L3.inches = (L1.inches + L2.inches) % 12 ;
	return L3 ;        // structure returned
}

If the return value of the function prnsum() is being assigned to a variable, then that variable must be a structure variable of type distance. For example, notice the following code fragment :

:
distance total ;
total = prnsum(length1, length2) ;
cout << total.feet << "  " << total.inches ;

The above code fragment stores the return value of function prnsum() into the structure total which is also of type distance and then prints the values of structure elements. The function prnsum() that returns a structure and gets invoked in call-by-reference.

A function may even return a reference to a structure also. For example, consider the following function :

distance & sum (distance L1, distance L2) ;     //notice the declaration
distance L3 ;        // global structure declared
/* Function Definition follows */
distance & sum (distance L1, distance L2)
{
	L3.feet = L1.feet + L2.feet + (L1.inches + L2.inches) / 12 ;
	L3.inches = (L1.inches + L2.inches) % 12 ;
	return L3 ;
}

The above function is returning reference to a structure L3. When a function returns a reference, it returns the lvalue (location value) in place of rvalue (data value) of a variable.


« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test