parameter passing & pointers

27
Parameter Passing

Upload: prince-shri-jayaprakkash

Post on 08-Dec-2015

233 views

Category:

Documents


1 download

DESCRIPTION

parameter passing and pointers c++

TRANSCRIPT

Page 1: Parameter Passing & Pointers

Parameter Passing

Page 2: Parameter Passing & Pointers

2

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.

Page 3: Parameter Passing & Pointers

3

swap1() Function Definition

// swap1() function definition

void swap1(int num1, int num2)

// swap the values of num1 and num2

{

int temp;

temp = num1; // Why do we need temp?

num1 = num2;

num2 = temp;

}

Page 4: Parameter Passing & Pointers

4

swap1() Function Usage

int main(){int firstNum, secondNum; firstNum = 30; secondNum = 50;

// before calling print both numbersswap1(firstNum, secondNum);//swap!

//after calling print both numbers}

Page 5: Parameter Passing & Pointers

5

A Closer Look at swap1():From main()

• In main(), two variables are declared and defined:firstNum = 30

secondNum = 50

• Memory cells in the computer are allocated to hold these variables

Memory

firstNum 30 secondNum 50

Page 6: Parameter Passing & Pointers

6

A Closer Look at swap1(): After calling swap1()

• The main() function calls swap1()

• 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.

Memory

num1 30 num2 50

Page 7: Parameter Passing & Pointers

7

A Closer Look at swap1(): Inside swap1()

• 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.

Memory

num1 30 num2 50 temp

Page 8: Parameter Passing & Pointers

8

A Closer Look at swap1(): Making the Exchange

• Assign num2 to num1.

• Notice that num1 value is now lost. Fortunately, we saved its value inside temp

Memory

num1 50 num2 50 temp 30

Memory

num1 50 num2 30 temp 30

Page 9: Parameter Passing & Pointers

9

A Closer Look at swap1(): Finishing up

• 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.

Memory

firstNum 30 secondNum 50

Page 10: Parameter Passing & Pointers

10

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.

Page 11: Parameter Passing & Pointers

11

swap2() Function Definition

// swap2() function definition

void swap2(int &num1, int &num2)

// swap the values of num1 and num2

{

int temp;

temp = num1; // Why do we need temp?

num1 = num2;

num2 = temp;

}

Page 12: Parameter Passing & Pointers

12

swap2() Function Usage

int main(){int firstNum, secondNum; firstNum = 30; secondNum = 50;

// before calling print both numbersswap2(firstNum, secondNum);//swap!

//after calling print both numbers}

Page 13: Parameter Passing & Pointers

13

A Closer Look at swap2():From main()

• In main(), two variables are declared and defined:firstNum = 30

secondNum = 50

• Memory cells in the computer are allocated to hold these variables

Memory

firstNum 30 secondNum 50

Page 14: Parameter Passing & Pointers

14

A Closer Look at swap2(): After calling swap2()

• 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.

Memory

num1(firstNum) 30 num2(secondNum) 50

Page 15: Parameter Passing & Pointers

15

A Closer Look at swap2(): Inside swap2()

• In swap2(), 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.

Memory

num1(firstNum) 30 num2(seoncdNum) 50 temp

Page 16: Parameter Passing & Pointers

16

A Closer Look at swap2(): Making the Exchange

• Assign num2 to num1.

• Notice that num1 value is now lost. Fortunately, we saved its value inside temp

Memory

num1(firstNum) 50 num2(secondNum) 50 temp 30

Memory

num1(firstNum) 50 num2(secondNum) 30 temp 30

Page 17: Parameter Passing & Pointers

17

A Closer Look at swap2(): Finishing up

• 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.

Memory

firstNum 50 secondNum 30

Page 18: Parameter Passing & Pointers

PointersIntroduction to Pointers:

Consider the declarationint 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.

a ? 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)

Page 19: Parameter Passing & Pointers

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;

Page 20: Parameter Passing & Pointers

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 ?

Page 21: Parameter Passing & Pointers

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).

Page 22: Parameter Passing & Pointers

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.

Page 23: Parameter Passing & Pointers

Structures

Example:How to Declare?struct person

{

char name[20];

int age;

float salary;

} ;

struct person p1,p2,p3;

Page 24: Parameter Passing & Pointers

Structures

How to Read/Print?cin >> p1.name;cin >> p1.age;cin >> p1.salary;

cout << p1.name;cout << p1.age;cout << p1.salary;

Page 25: Parameter Passing & Pointers

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

};

Page 26: Parameter Passing & Pointers

Structures

Arrays of Structures

struct book {

int bnum; char bname[15]; float cost; int page; char author[25]; };

struct book b[100];

Page 27: Parameter Passing & Pointers

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

}