Structures and Functions

1 / 25

# Structures and Functions - PowerPoint PPT Presentation

Structures and Functions. Ghulam Nasir Khan. Important /\/otes. Programming is a fundamental course of IT as well as BICSE, so it will be very difficult to proceed without programming.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about 'Structures and Functions' - neron

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### Structures and Functions

Ghulam Nasir Khan

Important /\/otes
• Programming is a fundamental course of IT as well as BICSE, so it will be very difficult to proceed without programming.
• You will be thought different courses on programming throughout your course of study such as OOP using C++, Data Structures, Algorithms, Assembly Language, Web Designing so there is no way out.
• The key point is try to develop interest in programming and try to practice as much as you can.
Important /\/otes
• Try to clear all of your concepts and practice as much as possible you that can make a good understanding of programming
• Do not let the instructor proceed, even if you have a small confusion in mind.
• Try to learn as much programming as you can in this semester so that you do not have to cry over spilt milk.

BEST OF LUCK

### An Overview of Structures

What is Structure
• Structure is used to store data of same or different type. It is used to group data that may be of the same or different types.
• Basically it is used to store data that has a logical relation e.g. record of a student etc.
• It can also be said that structure is used to create a new data type comprising of the existing data types.
Composition of Structures
• Structures are composed of
• Data Members
• Member Functions
• Data Members are memory locations that are used to store data e.g. variables, arrays etc.
• Member Functions are used to manipulate the Data Members.
Memory !!!!
• How much memory is occupied by Structures?
• 1 Byte?
• 4 Bytes?
• 8 Bytes?
• N Bytes? (Specify the value of N)
• The answer is
• Zero Bytes
Structure Variable
• The representation of Structure is memory is called Structure Variable or Structure Object or simply Object.
• Object occupies space in the memory and object actually stores the data.
Memory !!!!
• How much memory is occupied by Structure Object?
• 0 Byte?
• 1 Byte?
• 4 Bytes?
• 8 Bytes?
• 16 Bytes?
• N Bytes? (Specify the value of N)
Memory !!!!
• The answer is
• The sum of memory occupied by all the data members of a structure.
• For Example, all the objects of a structure having two integer, one float and one character data member will occupy 4 * 2 + 4 + 1 = 8 + 4 + 1 = 13 Bytes
Declaring a Structure

struct StructureName

{

DataType variable 1;

DataType variable 2;

.

.

DataType variable n;

};

Creating Structure Object
• struct StructureName objname;
• StructureName objname;
• Here objname is a user defined name of the object. It must follow Variable Naming Rules.
/*

Program Name: struct.cpp

Author: Nasir

*/

#include<iostream.h>

struct emp

{

int empno;

float salary;

};

void main()

{

emp e;

cout<<"Enter employee number:”;

cin>>e.empno;

cout<<”Enter salary:”;

cin>>e.salary;

cout<<”You have entered:”<<endl;

cout<<”Employee #”<<e.empno<<endl;

cout<<”Salary:”<<e.salary;

}

### Today’s Lecture

Structures and Functions

Passing Structures (Objects) To Functions
• Like ordinary variables and arrays, structure object can also be passed to functions.
• The syntax for passing Structure Objects to functions is the same as ordinary variable. The only difference is that the DataType in that case is replaced by The StructureName.
Passing Structures (Objects) To Functions
• Syntax (in case of ordinary variables):

ReturnTypeFunctionName(inta,int b)

{

}

• Syntax (in case of structure objects):

ReturnTypeFunctionName(st a, st b)

{

}

Note:

• Here st is the name of the structure whose object is being passed to the function.
• Structure Objects are passed by value.
/*

Program Name: struct.cpp

Author: Nasir

*/

#include<iostream.h>

struct emp

{

int empno;

float salary;

};

void show(emp t);

void main()

{

emp e;

cout<<"Enter employee number:”;

cin>>e.empno;

cout<<”Enter salary:”;

cin>>e.salary;

cout<<”You have entered:”<<endl;

show(e);

}

void show(emp t)

{

cout<<”Employee #” << t.empno; cout<<endl;

cout<<”Salary:”<<t.salary;

}

Returning Structure Variables (Objects)
• Syntax (in case of ordinary variables):

ReturnType FunctionName(int a)

{

return variablename;

}

• Syntax (in case of structure objects):

st FunctionName(st a, int b)

{

return objname;

}

Note:

• Here st is the name of the structure whose object is being passed to the function.
• Here objname is the name of the object of structure st
/*

Program Name: struct.cpp

Author: Nasir

*/

#include<iostream.h>

struct emp

{

int empno;

float salary;

};

emp input(emp t);

void main()

{

emp e;

e=input(e);

cout<<”You have entered:”<<endl;

cout<<”Employee #” << e.empno; cout<<endl;

cout<<”Salary:”<<e.salary;

}

emp input(emp t)

{

cout<<"Enter employee number:”;

cin>>t.empno;

cout<<”Enter salary:”;

cin>>t.salary;

return t;

}