Anda di halaman 1dari 27

Parameter Passing

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.

swap1() Function Definition


// swap1() function definition
void swap1(int num1, int num2)
// swap the values of num1 and num2
{
int temp;
temp = num1;
num1 = num2;
num2 = temp;

// Why do we need temp?

}
3

swap1() Function Usage


int main()
{
int firstNum, secondNum;
firstNum = 30;
secondNum = 50;
// before calling print both numbers
swap1(firstNum, secondNum); //swap!
//after calling print both numbers
}
4

A Closer Look at swap1():


From main()
Memory
firstNum 30
secondNum 50

In main(), two
variables are
declared and
defined:
firstNum = 30
secondNum = 50
Memory cells in the
computer are
allocated to hold
these variables
5

A Closer Look at swap1():


After calling swap1()
The main() function
calls swap1()
Memory
num1 30
num2 50

Because swap defined


num1 and num2 as value
parameters, the
variables firstNum and
secondNum are now
copied to as num1 and
num2 respectively inside
the function and used
separate memory
locations.
6

A Closer Look at swap1():


Inside swap1()

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

A Closer Look at swap1():


Making the Exchange
Memory
num1 50
num2 50
temp30

Memory

Assign num2 to num1.

Notice that num1 value is now lost. Fortunately,


we saved its value inside temp

num1 50
num2 30
temp30

A Closer Look at swap1():


Finishing up

Memory
firstNum 30
secondNum 50

swap1() has completed its


routine. Its local variables
vanish, and the calling
variables have not affected
with the change because it
is in different memory
locations.

The main() function now


continues execution
beyond the function call.

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

swap2() Function Definition


// swap2() function definition
void swap2(int &num1, int &num2)
// swap the values of num1 and num2
{
int temp;
temp = num1;
num1 = num2;
num2 = temp;

// Why do we need temp?

}
11

swap2() Function Usage


int main()
{
int firstNum, secondNum;
firstNum = 30;
secondNum = 50;
// before calling print both numbers
swap2(firstNum, secondNum); //swap!
//after calling print both numbers
}
12

A Closer Look at swap2():


From main()
Memory
firstNum 30
secondNum 50

In main(), two
variables are
declared and
defined:
firstNum = 30
secondNum = 50
Memory cells in the
computer are
allocated to hold
these variables
13

A Closer Look at swap2():


After calling swap2()

Memory
num1(firstNum) 30
num2(secondNum)
50

The main() function


calls swap2()
Because swap defined
num1 and num2 as value
parameters, the
variables firstNum and
secondNum are now
referred to as num1 and
num2 respectively inside
the function and used
same memory locations.
14

A Closer Look at swap2():


Inside swap2()

Memory

In swap2(), a new
variable, temp, is
introduced to hold the
value of num1

num1(firstNum) 30
num2(seoncdNum)
50
temp

Without temp, the first


variable assignment
would overwrite the
other!
We see how in the next
slide.
15

A Closer Look at swap2():


Making the Exchange
Memory
num1(firstNum) 50
num2(secondNum)
50
temp30

Assign num2 to num1.

Notice that num1 value is now lost. Fortunately,


we saved its value inside temp

Memory
num1(firstNum) 50
num2(secondNum)
30
temp30

16

A Closer Look at swap2():


Finishing up

Memory
firstNum 50
secondNum 30

swap2() has completed its


routine. Its local variables
vanish, and the calling
variables have affected
with the change because it
uses the same memory
locations.

The main() function now


continues execution
beyond the function call.

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.

3. The value of the identifier is unknown.


4. Each memory location have its unique address, say for example
here assume the address is 0x251723e8. (this is not fixed b/c the
computer may take some other address in the next time)

Pointers
Operators used:
& - address of operator
*

- value at address operator

For example consider the following case


int a=10;

cout << a<<endl;


cout <<Address is << (&a) << endl;
cout << Address is << (&a)<< Its value is << *(&a) << endl;

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
}

Anda mungkin juga menyukai