C++ Prog'g Chapter 2 and 3
C++ Prog'g Chapter 2 and 3
Modular programming
• A function is a collection of statements that
performs a specific task. (also called a module)
• A function performs some computation and
usually yields a result.
• Modular Programming: breaking down a program
into a set of manageable functions, or modules.
• Real world programs can easily have thousands of
lines of code and unless they are modularized,
they can be very difficult to modify and maintain.
Function Definition
• When defining a function, we give it
– Name: a function must have a unique name.
Naming rules are the same as those for variables
– Parameter List: a list of variables that hold values
being passed into the function
– Body: the set of the statements that make up the
function
– Return Type: the data type of the value being sent
back from the function.
Example
float cube(float num)
{
float r = num*num*num;
return r;
}
The declaration shown above creates a data type
named Mood.
A variable of type Mood may only have values
that are in the list inside the braces.
Defining and Using Variables
enum Mood {Depressed, Sad, Happy, Ecstatic};
Note that the above statement only defines the data
type and it does not create any variables.
A variable of the mood type can be defined like a
variable of any other type.
Mood today;
An assignment would look like
today = Happy;
A logical comparison would look like
if (today == Sad)
Example:
int main()
{
// Defining enum Gender
enum Gender { Male, Female };
// Creating Gender type variable
Gender gender = Male;
switch (gender)
{
case Male:
cout << "Gender is Male";
break;
case Female:
cout << "Gender is Female";
break;
default:
cout << "Value can be Male or Female";
}
return 0;
}
More example
using namespace std;
These individual definitions do not make it clear
that these variables are related.
Creating a Relationship
Package these together into a structure
struct Student //keyword struct followed by a name
{
char name[30];
char id[12];
unsigned short age;
Gender sex;
unsigned short year;
float cgpa;
}; //notice the semicolon
The variables are now known as members of the structure.
Continued
• Now Student structure has been created with 6 attribute.
• When a structure is created, no memory is allocated.
• The structure definition is only the blueprint for the
creating of variables. You can imagine it as a datatype.
When you define an integer as below:
• int foo;
• The int specifies that, variable foo can hold integer
element only. Similarly, structure definition only specifies
that, what property a structure variable holds when it is
defined.
How to define a structure variable?
A variable of type Student can now be defined just like
any other variable
Student abe;
• When structure variable is defined, only then the
required memory is allocated by the compiler.
• Considering you have either 32-bit or 64-bit system,
the memory of float is 4 bytes, memory of int is 4
bytes and memory of char is 1 byte.
• How many byte of memory is allocated for structure
student?
How to access members of a structure?
#include <iostream>
int main() {
using namespace void displayData(Person p)
Person p;
std; struct Person { cout << "\nDisplaying
cout << "Enter Full name:
{ char name[50]; Information." << endl;
"; cin.get(p.name, 50);
int age; cout << "Name: " <<
cout << "Enter age: ";
float salary; }; p.name << endl;
cin >> p.age;
void cout <<"Age: " << p.age <<
cout << "Enter salary: ";
displayData(Person); endl;
cin >> p.salary;
cout << "Salary: " <<
displayData(p);
p.salary; }
return 0; }
Returning structure from function
#include <iostream>
using namespace std; Person getData(Person p)
struct Person { { cout << "Enter Full name: ";
char name[50]; cin.get(p.name, 50);
int age; cout << "Enter age: ";
float salary; cin >> p.age;
}; cout << "Enter salary: ";
Person getData(Person); cin >> p.salary;
void displayData(Person); return p; }
void displayData(Person p)
int main() { {
Person p, temp; cout << "\nDisplaying Information." << endl;
temp = getData(p); cout << "Name: " << p.name << endl;
p = temp; cout <<"Age: " << p.age << endl;
displayData(p); cout << "Salary: " << p.salary;
return 0; }
}
Thank you.
Note: we will have exam next
weak