Pass by Value
When a parameter is passed by value, any
change to the parameter inside the
function has absolutely no effect outside
the function!
If all the parameters are passed by value,
the only effect the function has on the
calling program is through the returned
value.
}
3
In main(), two
variables are
declared and
defined:
firstNum = 30
secondNum = 50
Memory cells in the
computer are
allocated to hold
these variables
5
Memory
num1 30
num2 50
temp
In swap1(), a new
variable, temp, is
introduced to hold the
value of num1
Without temp, the first
variable assignment
would overwrite the
other!
We see how in the next
slide.
7
Memory
num1 50
num2 30
temp30
Memory
firstNum 30
secondNum 50
Pass by Reference
A C++ program can pass to a function
the memory locations (references) of the
variables used to make the function call,
instead of copying their values.
Any change to the value of parameters
will change the value of the variables of
the calling function.
10
}
11
In main(), two
variables are
declared and
defined:
firstNum = 30
secondNum = 50
Memory cells in the
computer are
allocated to hold
these variables
13
Memory
num1(firstNum) 30
num2(secondNum)
50
Memory
In swap2(), a new
variable, temp, is
introduced to hold the
value of num1
num1(firstNum) 30
num2(seoncdNum)
50
temp
Memory
num1(firstNum) 50
num2(secondNum)
30
temp30
16
Memory
firstNum 50
secondNum 30
17
Pointers
Introduction to Pointers:
Consider the declaration
int a;
What it specifies to the compiler?
1. It specifies to reserve memory location (2 bytes) to hold an
integer value.
2. Associate the name a to the reserved memory location.
Pointers
Operators used:
& - address of operator
*
Pointers
How to declare pointer variable?
int *i;
pointer variables are variables whose values are memory addresses.
This tells the compiler that i will be used to store the address of an integer value. In other
words i points to an integer.
i.e. int a, *i;
a=10;
i = &a;
cout<<i<<endl; //it prints ?
cout<<(*i)<<endl; //it prints ?
Pointers
What is a pointer?
A pointer variable is declared by giving it a type
and a name (e.g. int *ptr) where the asterisk (*)
tells the compiler that the variable named ptr is a
pointer variable and the type tells the compiler
what type the pointer is to point to (integer in
this case).
Structures
Definition:
A structure is a collection of one or more variables grouped
under a single name for easy manipulation. A structure can
contain any of C++ data types. Each variable within a structure
is called a member of the structure.
In simple a structure contains a number of data types grouped
together. These data types may or may not of the same type.
Structures
Example:
How to Declare?
struct person
{
char name[20];
int age;
float salary;
};
struct person p1,p2,p3;
Structures
How to Read/Print?
cin >> p1.name;
cin >> p1.age;
cin >> p1.salary;
cout << p1.name;
cout << p1.age;
cout << p1.salary;
Structures
Attributes of a book are
Book number, name, cost, no. of pages in it, author of the book, etc.
structure to store book information?
struct book
{
int bnum;
char bname[15];
float cost;
int page;
char author[25];
};
Structures
Arrays of Structures
struct book
{
int bnum;
char bname[15];
float cost;
int page;
char author[25];
};
struct book b[100];
Structures
How to Read/Print?
cin >> n;
for(i=0;i<n;i++)
{
cin >> b[i].bnum;
cin >> b[i].bname;
cin >> b[i].cost;
cin >> b[i].page;
cin >> b[i].author
}