Structues and Functions
:: Big Index :: C/C++ Programming Tutorials ::
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
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.
More about "Structuers"
- Write a C/C++ program for displaying today's date by...
Programming Tutorials : C/C++ Programming It's a simple program to start learning structure in C/C++. This program will display today's date. Suppose today's date is 30.10.2008. So, the output of the program... - Structures and arrays
Programming Tutorials : C/C++ Programming Structure and array can be implemented together. An array is a group of identical data items. All array elements are sorted consecutive memory location under a... - Nested Structures : Structures within Structures
Programming Tutorials : C/C++ Programming Structures can be implemented with functions and arrays. Moreover, structures can be implemented as the member of other structure. This is termed as structure within... - Structues and Functions
Programming Tutorials : C/C++ Programming Function decomposes any complex program into several manageable modules. Each module is referred as function. Functions are compiled separately. So, they can be...