functions - ii chapter 9 and 10

Post on 01-Jan-2016

37 Views

Category:

Documents

5 Downloads

Preview:

Click to see full reader

DESCRIPTION

FUNCTIONS - II Chapter 9 and 10. Today we will continue with functions covering…. Passing by Reference  Scope Sorting variables Function Structure Charts (Ch10). This function changes its parameter. Q: What does x hold after fn call?. void countdown(float n) { - PowerPoint PPT Presentation

TRANSCRIPT

1

FUNCTIONS - IIChapter 9 and 10

2

Today we will continue with functions covering….

Passing by Reference Scope

Sorting variables

Function Structure Charts (Ch10)

3

Q: What does x hold after fn call?

void countdown(float n){

while (n>0) {cout <<n; n=n-1;}

cout << "...ZERO!!!“<<endl;}void main( ){ float x=5;

countdown(x);cout<<“after countdown(), x is “<<x;

}

This function changes its parameter

4

Program Output

54321…ZERO!!!

after countdown(), x is 5 (WHY?)

Pass-by-Value:Only a copy of the argument x is given to the function

Even though n changes, x retains original value

Sometimes you need the function to change the argument. Thus we also have …

5

Passing by Reference

This is done in situations where a function needs to change the value of the argument passed to it

To pass an argument by reference, simply append an ampersand ‘&’, to the type specifier in the function’s parameter list

6

Passing by Reference (contd..)

Then the local variable is only a reference to the argument passed to itThe argument is read-write instead of read-only(as in the case of pass by value)Any change to the local variable inside the function will cause the same change to the argument that was passed to it

7

Q: What does x hold after fn call?

void countdown(float& n){

while (n>0) { Added Only 1 Character!

cout <<n; n=n-1;}

cout << "...ZERO!!!“<<endl;}void main( ){ float x=5;

countdown(x);cout<<“after countdown(), x is “<<x;

}

This function uses PBR

8

New Program Output 54321…ZERO!!!

after countdown(), x is 0 (WHY?)

Pass-by-Reference:Direct access to argument x is given to the function (Read/Write)

When it changes “n”, it really changes x

Here n is another name for the argument, x

Often called a reference or “alias” to x

9

Demonstrate politic.cpp in Ch09PBR Functions can “return” more than 1 valueReference parameters can be same name or different name than arguments

Draw lines between arguments and parameters

Arguments to Reference parameters must be variables of the same type (no constants)Arguments to Value parameters can be constants, variables, or expressions, and type is more lenientIllustrate by modifying politic.cpp

10

Demonstrate circle.cpp, ch09

PBR Functions can calculate 2 or more quantities and return them

Some parameters are Value, some are Reference

Draw lines between arguments and parameters

11

Typical Test Question….output?void f(float x, float& y) { // changes reference argument to 99: x = 88; y = 99; }

void main() { float a = 22, b = 44; cout<< "a = “<< a << ", b = "<< b << endl; f(a,b); cout<< "a = "<< a << ", b = "<< b << endl; }

12

The call f(a,b) passes a by value to x and it passes b by reference to y

x is a local variable that is assigned a’s value of 22

y is an alias for the variable b whose value is 44

The function assigns 88 to x but that has no effect on a

When it assigns 99 to y it is really assigning 99 to b, because y is an alias for b

Finally, a still has its original value 22 (read-only), while b has the new value 99(read-write)

13

Summing up…

14

Your TurnWhat does this program display?

void func1 (int &x, int &y, int z); void main()

{ int a=2, b=2, c=2; func1 ( a, b, c); cout <<"a="<<a<<"b="<<b<<"c="<< c <<endl; }

void func1 (int &x, int &y, int z) { x=x+1; y=y-1; z=z+2; }

15

How do you know when to use &?

Don’t overdo it because you are not sure

Ask…will the function need to initialize or update an argument you are giving it?

YES…then use a reference parameter

NO…then use a value parameter

See circle.cpp again to study the value and reference parameters

16

Today we will continue with functions covering….

Passing by Reference

Scope Sorting variables

Function Structure Charts (Ch10)

17

Remember scope ?

The scope of a name consists of that part of the program where it can be used

It begins where the name is declared

The scope extends to the end of the innermost block that contains the declaration

18

void f(); // f() is global void g(); // g() is global float x = 11; // this x is global void main() { float x = 22; { float x = 33; cout<<"In block inside main(), x = "<<x<<endl; } // end scope of internal block cout<<"In main(), x = "<<x<< endl; cout<<"In main(), ::x = "<<::x<<endl; //global x f(); g(); } // end scope of main() void f() { float x = 44; cout << "In f(), x = " << x << endl; } // end scope of f() void g() { cout << "In g(), x = " << x << endl; } // end scope of g()

19

In the previous example…

f() and g() and the first x are global – their scope includes the entire file

The second x is declared inside main() , it is accessible only from within the main()

The third x is declared inside an internal block, so its scope is restricted to that internal block

20

PROGRAM OUTPUT for Previous Example

In block inside main(), x = 33

In main(), x = 22

In main(), ::x = 11

In f(), x = 44

In g(), x = 11

21

Scope and You: the Rules!!

Don’t use global variables for now. You don’t need them.

Lazy programmers try to skip writing parameter lists by making all function variables global—0 points if you do!!

Best is to declare all variables needed at beginning of main/other function

Advice—use different names for function call arguments and function parameters

22

Today we will continue with functions covering….

Passing by Reference

Scope

Sorting variables Function Structure Charts (Ch10)

23

Sorting Data

Arranging data in increasing order

A real “bread and butter” CS/IS task

Sorted data are easier to search

Usually a slow process for long lists of data

Efficiency of advanced sort methods can result in tremendous gains in processing time (CSIS 10B)

24

Sort works by using Swap One of the most useful algorithms

Swap two variables:

float x=3, y=5, temp;

temp=x;

x=y;

y=temp;

Used so much we want to make a function

x y temp

3

5

5

5

3

?

3

3

3

25

void swap(float& x, float& y) { // exchanges the values of x , y: float temp = x; x = y; y = temp; }

void main() { float a = 22.2, b = 44.4; cout<<"a = "<<a<<",b = "<<b<<endl; swap(a,b); cout<<"a = "<<a<<",b = "<<b<<endl; }

26

Let’s understand that program..

The formal parameters x and y are declared as reference variables :

float& x, float& y

The reference operator & makes x and y synonyms for the arguments passed to the function

27

Basic Sort

If two adjacent data are out of order, swap

float a, b;

cin>>a>>b;

if (a>b) swap(a, b);

28

Extending Sort for 3 variablescin>>a>>b>>c;If any two adjacent data are out of order, swap if (a>b) swap(a, b);

if (b>c) swap(b, c);

Recheck 1st two in case c was smallestif (a>b) swap(a, b);

29

Demonstrate alpha3.cpp, ch09

Modify to simplify, only swap w1 and w2 and show changes

What if you want to sort integers? Function overloading…

make another swap(int &x, int &y)

Compiler can tell which one to go to during execution by looking at the arguments you are using

30

Making a Sort3( ) function…revA//Sort3( ) Sorts three params in numeric order//IN/OUT: a, b, and c are returned in numeric order// currently only a stubvoid Sort3(float &a, float &b, float &c){ cout<<“values passed to Sort():”<<endl;

cout<< a <<“ ”<< b <<“ ”<< c << endl;a=1.0;b=2.0;c=3.0;

}

void main( ){ float r, s, t; cin>>r>>s>>t; Sort3(r, s, t); cout<<“in order:” << r <<“ ”<< s <<“ ”<< t << endl;}

This is called a

“function stub”

It just tests

parameter

passing

31

Making a sort3( ) function…revB

// Add swap definition here

//Sort3( ) Sorts three params in numeric order//IN/OUT: a, b, and c are returned in numeric ordervoid Sort3(float &a, float &b, float &c){ if (a>b) swap(a, b);if (b>c) swap(b, c);if (a>b) swap(a, b);

}void main( ){ float r, s, t; cin>>r>>s>>t; Sort3(r, s, t); cout<<“in order:” << r <<“ ”<< s <<“ ”<< t << endl;}

32

Today we will continue with functions covering….

Passing by Reference

Scope

Sorting variables

Function Structure Charts (Ch10)

33

Structure Charts (see p226 +)Show the large scale layout of your program

Each block is a function

Linkage between functions shownvalue and reference parameters

Demonstrate structure charts for Politic.cpp

Circle.cpp

Class.cpp (ch10 folder)

Will help you visualize Project 2 ATM

34

Demonstrate class.cpp

Notice the documentation of in and out parameters

Also notice the passing of a filestream to find_mark function

ofstream& fout

This will be important later in Project 2

35

THE END !!!

top related