0% found this document useful (0 votes)
28 views29 pages

C++ Prog'g Chapter 2 and 3

Uploaded by

fegegbelulegn741
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views29 pages

C++ Prog'g Chapter 2 and 3

Uploaded by

fegegbelulegn741
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

INTRODUCTION TO FUNCTIONS

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;
}

• Identify the parts of this function.


Function Call Statements
• The main function starts executing
automatically when the program starts.
• All other functions have to be called
explicitly to start executing.
• This is done by using function call
statements.
Example With Library Functions
int main()
{
float a=5.0, b=3.0, c;
c = pow(a, b);
cout<<c<<endl;
return 0;
}
Function Overloading
• These functions having the same name but
different arguments are known as overloaded
functions. For example:
int test() { }
int test(int a) { }
float test(double a) { }
int test(int a, double b) { }
Recursion
• A function that calls itself is known as a
recursive function. And, this technique is
known as recursion.
• Example: write a function that calculate the
factorial of a given number.
int factorial(int n)
{
if (n > 1)
{
return n * factorial(n - 1);
}
else
{ return 1; } }
Chapter 3

Custom Data Types


Custom Data Types

So far, we have been using data types built
into the C++ language (like int, double, …)

C++ allows us to define our own data types

In this lecture, we will see three constructs
that can be used to define our own data types

Enumerations

Structures

Classes
Enumerated Data Types

An enumerated data type is a programmer
defined data type that contains a set of
named integer constants.

For an enumerated data type, the legal set of
values that its variables can have are
enumerated, or listed, as part of the
definition of the data type.

These permissible values are called
enumerators.
Mood as an Enumerated Type

Example:

enum Mood {Depressed, Sad,


Happy,Ecstatic};


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;

// Defining enum Year // Driver Code


enum year { int main()
Jan, {
Feb, int i;
Mar,
Apr, // Traversing the year enum
May, for (i = Jan; i <= Dec; i++)
Jun, cout << i << " ";
Jul,
Aug, return 0;
Sep, }
Oct,
Nov,
Dec What is the out put?
};
Structures

So far we have seen that

A variable stores a single value of one type

An array stores multiple values of the same type

C++ allows us to group a set of variables of
(possibly different types) together into a
single item known as a structure.

These variables are related in same way –
they hold data about the same entity.
Example

For a student we may have the following data

Name (a string)

ID Number (a string)

Age (an integer)

Gender (an enumerated type)

Year (an integer)

Cumulative GPA (a floating point number)
The Student Data
• char name[30];
• char id[12];
• unsigned short age;
• Gender sex;
• unsigned short year;
• float cgpa;


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?

• The members of structure variable is accessed


using a dot (.) operator.
• Suppose, you want to access age of structure
variable abe and assign it 25 to it. You can
perform this task by using following code
below:
• abe.age = 50;
Example: C++ Structure
#include <iostream>
using namespace std;
struct Person { char name[50];
int age;
float salary; };
int main() { Person p1;
cout << "Enter Full name: ";
cin.get(p1.name, 50);
cout << "Enter age: ";
cin >> p1.age;
cout << "Enter salary: ";
cin >> p1.salary;
cout << "\nDisplaying Information." << endl;
cout << "Name: " << p1.name << endl;
cout <<"Age: " << p1.age << endl; cout << "Salary: " << p1.salary; return 0; }
Can and Can't Do

You can access and use the individual
members as you would use any other
variable.
cout<<abe.name<<abe.age;
if (abe.year > 1) ...

You can't perform operations on the entire
struct
cout<<abe; //error
if (abe == kebe) //error
Initializing a Structure

You can initialize the members of a structure
in two ways

Using an initializer list (like in arrays)

Using a constructor (to be seen in 5.6)

Student abebe = {“Abebe”, “tcr/1234/01”, 19,


MALE, 2, 3.40};
Structure and Function
• Structure variables can be passed to a
function and returned in a similar way as
normal arguments.
Passing structure to function

• A structure variable can be passed to a


function in similar way as normal argument.
Consider this example:

#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

You might also like