Home » Learn C++ » Returning Structure from Functions

Returning Structure from Functions

Returning Structure from Functions:

            Since a structure type data can be passed to a function, a function can also return a structure type data. Usually, the structure type data returned by a function is assigned to a structure type variable. For example:

Program:

 

# include <iostream.h>
# include <conio.h>
# include <stdio.h>
struct temp
{
char mn[12];
float marks;
main ( )

{

temp abc (void);

temp xyz;

clrsrc ( );

xyz = abc ( );

cout << “Name = ” << xyz.mn << endl;

cout << “Marks = ” << xyz.marks << endl;

cout << “Ok”;

}

temp abc (void);

{

temp rec;

cout << “Enter Name ”;

gets (rec.mn);

cout << “Enter Marks ? ”;

cin >> rec.marks;

return (rec);

}

 

            In the above function, the “temp” is the tag name of structure. The function “abc” is declared that has no arguments and its returned data type is “temp” i.e. structure type. The variable “xyz” is also declared as of structure type. When the function is called, then its returning data is assigned to variable “xyz” after executing body of the function. In the function definition, the variable “rec” is also declared as data type “temp”. The data is entered into the “rec” and the same is returned to the calling program by return statement.

 

Other Topics

comment closed

Get Adobe Flash playerPlugin by wpburn.com wordpress themes
Copyright © 2010 Information Village. All rights reserved.