Structues and Functions

Requirements

  • Basic knowledge of structure
  • Programming knowledge 
  • Compiler 

Function decomposes any complex program into several manageable modules. Each module is referred as function. Functions are compiled separately. So, they can be tested separately. At last, all the functions are invoked to the main program.

Structure can be passed to the function as a single variable.

In this page, one program is implemented using functions and structue. 

Program

Write a program which can collects several information such as employee id, sex, age etc. and can display the entered information to the standard display by using functions and structures. 

Program for functions and structure

#include<iostream.h>


// definition of structure
struct company{
	long int employee_id;
	char sex;
	int age ;
};
// object defination of the structure
company employee;
int main(){
	
	company employee;
	// function definition
	void information(company employee);
	void display(company employee);
	
	cout << "A program for collecting information form"
        cout << "the employee using functions and structuer";
	
	//function calling
	information(employee);
	display(employee);
	
	cin.get();
	return 0;
}
void information(company employee){
	cout << "Enter the following information:" << endl;
	cout << endl;
	
	cout << "Employee ID:"; cin >> employee.employee_id;
	cout << "Sex        :"; cin >> employee.sex;
	cout << "Age        :"; cin >> employee.age;
	cout << endl;
	
}
void display(company employee){
	
	cout << "Recorded information of the employees:";
	cout << endl;
	
	cout << "ID	Sex	Age" << endl;
	
	cout << employee.employee_id << "\t";
	cout << employee.sex << "\t";
	cout << employee.age << endl;
}

Output of the program

Output of the program
Output of the program

Short discussion of the program

For this program, the structure is defined as follows:

struct company{
	long int employee_id;
	char sex;
	int age ;
};

The structure object is defined by the following code line:

company employee;


This program has two functions. They are:

void information(company employee);
void display(company employee);

"Information(company employee)" is used to collect data form the user. And "display(company employee)" is used to display the entered information to the screen.

By using the functions, the program is modularized and easily to understand.

Give your opinion.

Is the tutorial helpful?

  • Yes
  • No
  • Nothing to say
See results without voting

More by this Author


Comments

No comments yet.

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working