Method parameters and overloading version 1 1
This presentation is the property of its rightful owner.
Sponsored Links
1 / 58

Method Parameters and Overloading Version 1.1 PowerPoint PPT Presentation


  • 82 Views
  • Uploaded on
  • Presentation posted in: General

Method Parameters and Overloading Version 1.1. Topics. The run-time stack Pass-by-value Pass-by-reference Method overloading Stub and driver methods. Objectives. At the completion of this topic, students should be able to:.

Download Presentation

Method Parameters and Overloading Version 1.1

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Method parameters and overloading version 1 1

MethodParameters and OverloadingVersion 1.1


Topics

Topics

The run-time stack

Pass-by-value

Pass-by-reference

Method overloading

Stub and driver methods


Objectives

Objectives

At the completion of this topic, students should be able to:

Correctly write methods pass parameters by value (pass by value)

Correctly write methods pass parameters by reference (pass by reference)

Explain what a side effect is

Explain what method overloading is, and correctly use

method overloading in a program

Explain how type conversion affects method overloading

Explain what a Driver and a Stub methodare, and use correctly their programs


Method parameters and overloading version 1 1

The Execution or Run-Time Stack

An important component in understanding how methods

work is the execution or run-time stack.

The following slides discuss how C# uses the run-time

stack when invoking a method. Note that this is only a

conceptual view of how the stack operates. It is slightly

more complicated than what is shown here, and operation

of the stack depends a great deal on the operating system,

the compiler, and the hardware environment.


Method parameters and overloading version 1 1

To get an idea of how the stack works, think of the

plate dispensers that you have seen in a cafeteria

When a plate is removed

from the top of the stack,

all of the other plates

pop up.

When a plate is pushed

onto the stack, all of the

other plates get pushed

down.


Method parameters and overloading version 1 1

When a method is called (invoked), the computer

builds a stack frame. The stack frame contains

* the parameters that are being passed to the method

* the address to return to where the method was called from

when the method is done

* any local variables declared in the method


Method parameters and overloading version 1 1

local variables

control returns to

the operating system

Stack frame

for Main( )

return address

Main( )’s parameters

come from the

command line

parameters

The Stack


Method parameters and overloading version 1 1

local variables

Stack frame

for method “B”

return address

Main( ) calls

method ”B”

parameters

local variables

Stack frame

for Main( )

return address

parameters

The Stack


Method parameters and overloading version 1 1

When method “B” is done,

it removes its stack frame.

local variables

Stack frame

for Main( )

return address

parameters

The Stack


Method parameters and overloading version 1 1

local variables

Main( ) now goes

on about its work.

Stack frame

for Main( )

return address

parameters

The Stack


Method parameters and overloading version 1 1

Example


Method parameters and overloading version 1 1

using System;

class Program

{

static void Main()

{

int a = 5;

int b = 3;

int c = Add(a,b);

Console.WriteLine("The answer is {0}", c);

Console.ReadLine( );

}//End Main()

static int add(int num1, int num2)

{

int sum = num1 + num2;

return sum;

}

}//End class Program


Method parameters and overloading version 1 1

static void Main()

{

int a = 5;

int b = 3;

int c = Add(a,b);

Console.WriteLine("The answer is {0}", c);

Console.ReadLine( );

}

a = 5

b = 3

return address

no parameters

The Stack


Method parameters and overloading version 1 1

static void Main()

{

int a = 5;

int b = 3;

int c = Add(a,b);

Console.WriteLine("The answer is {0}", c);

Console.ReadLine( );

}

a = 5

b = 3

return address

no parameters

The Stack


Method parameters and overloading version 1 1

The return address, from call to Add(), that goes on the stack is

right here … before the assignment part of

this statement.

a = 5

b = 3

static void Main()

{

int a = 5;

int b = 3;

int c = Add(a,b);

Console.WriteLine("The answer is {0}", c);

Console.ReadLine( );

}

return address

no parameters

The Stack


Method parameters and overloading version 1 1

The return address , from call to Add(), that goes on the stack is

right here … before the assignment part of

this statement.

static void Main()

{

int a = 5;

int b = 3;

int c = Add(a,b);

Console.WriteLine("The answer is {0}", c);

Console.ReadLine( );

}

return address

3

5

a = 5

b = 3

return address

no parameters

The Stack


Method parameters and overloading version 1 1

sum

return address

static int Add(intnum1, int num2)

{

int sum;

sum = num1 + num2;

return sum,

}

num2

3

5

num1

a = 5

b = 3

return address

no parameters

The Stack


Method parameters and overloading version 1 1

sum = 8

return address

static int Add(intnum1, int num2)

{

int sum;

sum = num1 + num2;

return sum,

}

num2

3

5

num1

a = 5

b = 3

return address

no parameters

The Stack


Method parameters and overloading version 1 1

The stack frame

for the add method

is removed from the

stack.

intAdd(intnum1, int num2)

{

int sum;

sum = num1 + num2;

return sum,

}

a = 5

b = 3

return address

no parameters

Values returned from a method

are passed in a special hardware’

register

eax

8

The Stack


Method parameters and overloading version 1 1

control returns here

static void Main()

{

int a = 5;

int b = 3;

int c = Add(a,b);

Console.WriteLine("The answer is {0}", c);

Console.ReadLine( );

}

a = 5

b = 3

c = ?

return address

no parameters

eax

8

The Stack


Method parameters and overloading version 1 1

static void Main()

{

int a = 5;

int b = 3;

int c = add(a,b);

Console.WriteLine("The answer is {0}", c);

Console.ReadLine( );

}

a = 5

b = 3

c = 8

eax

8

return address

no parameters

The Stack


Pass by value

Pass By Value

When a parameter is passed by value, a copy

of the value is made and passed to the method

on the run-time stack.


Method parameters and overloading version 1 1

These names are local to the method Divide( ).

The parameters passed to the method are

given these names so that we can use

them inside of the method.

static double Divide(int n, int d)

{

double r = (double)n / d;

n++;

d++;

return r;

}


Method parameters and overloading version 1 1

static void Main()

{

int num = 0, den = 0;

do

{

Console.Write("Enter in an integer value: ");

num = int.Parse(Console.ReadLine());

Console.Write("Enter in another integer value: ");

den = int.Parse(Console.ReadLine());

if (den != 0)

{

double result = Divide(num, den);

Console.WriteLine("{0}/{1} = {2}", num, den, result);

}

} while (den != 0);

Console.ReadLine( );

}//End Main()

num and den are called the

actual parameters, or arguments.


Method parameters and overloading version 1 1

let the value of num = 9

and the value of den = 7

return here

if (den != 0)

{

Console.WriteLine("{0}/{1} = {2}", num, den, result);

}

num = 9

den = 7

double result = Divide (num, den);

return address

no parameters

The Stack


Method parameters and overloading version 1 1

= 1.285

r

static double Divide(int n, int d)

{

double r = (double)n / d;

n++;

d++;

return r;

}

return address

d

8

10

7

9

n

num = 9

den = 7

Notice that the original

values in main’s stack

frame don’t change.

This is because n and d

are names that are local

to the Divide method.

return address

no parameters

The Stack


Method parameters and overloading version 1 1

Control now returns to the point where the

function was called. In this case, the return

value, in eax register, is then copied to “result”.

1.285

eax

double result = Divide (num, den);

num = 9

den = 7

result = 1.285

return address

no parameters

The Stack


Pass by reference

Pass By Reference

When a parameter is passed by reference, a reference

to the value is made and passed to the method

on the run-time stack.


Method parameters and overloading version 1 1

the keyword refdenotes that this

parameter is passed by reference!

static double Divide(refint n, refint d)

{

double r = (double)n / d;

n++;

d++;

return r;

}


Method parameters and overloading version 1 1

double result = Divide (ref num, ref den);

Console.WriteLine("{0}/{1} = {2}", num, den, result);

num = 9

den = 7

result = 1.285

return address

no parameters

The Stack


Method parameters and overloading version 1 1

Build the stack frame to call the divide method

Return here when

done executing the

function

return address

ref to den

ref to num

double result = Divide (ref num, ref den);

Console.WriteLine("{0}/{1} = {2}", num, den, result);

num = 9

den = 7

result = 1.285

return address

no parameters

The Stack


Method parameters and overloading version 1 1

return address

d

ref to den

ref to num

static double Divide(refint n, refint d)

{

double r = (double)n / d;

n++;

d++;

return r;

}

n

num = 9

den = 7

result = 1.285

return address

no parameters

The Stack


Method parameters and overloading version 1 1

d

return address

n

ref to den

ref to num

static double Divide(refint n, refint d)

{

double r = (double)n / d;

n++;

d++;

return r;

}

num = 10

den = 8

result = 1.285

These local variable, in main’s

Stack frame, change, because

d and n refer to them.

return address

no parameters

The Stack


Method parameters and overloading version 1 1

If you are passing simple data to a method,

you should use pass-by-value

Rule of Thumb

avoids side effects!


Example of using a side effect

Example of Using a Side Effect

Problem: Write a method that exchanges the values

of two variables.


Method parameters and overloading version 1 1

The exchange code ………

for exchanging integers

value1 = value2;

value2 = value1;

int temp = value1;

value1 = value2;

value2 = temp;


Method parameters and overloading version 1 1

Using pass by value …

temp = 7

return address

These are

copies of

num1 and num2

7

5

5

7

n2

n1

int num1 = 5;

int num2 = 7;

Swap (num1, num2);

num1 = 5

num2 = 7

return address

void Swap (int n1, int n2)

{

int temp = n1;

n1 = n2;

n2 = temp;

}

no parameters

The Stack


Method parameters and overloading version 1 1

Only the local variables allocated in Swap’s

stack frame get swapped.

The original values are not changed.

To make the Swap work correctly, pass the parameters

by reference.


Method parameters and overloading version 1 1

Using pass by reference …

temp = 7

return address

These are

references to

num1 and num2

n2

n1

int num1 = 5;

int num2 = 7;

Swap (ref num1, ref num2);

ref to num2

ref to num1

num1 = 5

num2 = 7

return address

void Swap (ref intn1, ref intn2)

{

int temp = n1;

n1 = n2;

n2 = temp;

}

no parameters

The Stack


Method parameters and overloading version 1 1

Using pass by reference …

temp = 7

return address

ref to num2

ref to num1

n2

n1

int num1 = 5;

int num2 = 7;

Swap (ref num1, ref num2);

num1 = 7

num2 = 5

So … the changes

occur to num1 and

num2

void Swap (ref intn1, ref intn2)

{

int temp = n1;

n1 = n2;

n2 = temp;

}

return address

no parameters

The Stack


Mixed parameter lists

Mixed Parameter Lists

It is perfectly valid to mix pass-by-value and

pass-by-reference parameters in the same method:

void MethodTwo (ref intnum1, int num2);


Method overloading

Method Overloading

In C# you can give two different methods the

identical method name (but with different parameters)

This is called method overloading.

When a method is invoked, the compiler figures

out which of the methods to use, based on the

method name and the number, type and order

of parameters.


Example

this method has three parameters

static int Max (int n1, int n2, int n3)

{

if ( n1 < n2 )

if ( n2 < n3 )

return n3;

else

return n2;

else

if ( n1 < n3 )

return n3;

else

return n1;

}

Example

this method has two parameters

static int Max (int n1, int n2)

{

if ( n1 < n2 )

return n2;

else

return n1;

}

int biggest = Max (5, 3);

int largest = Max (5,3,7);

this code will invoke this method

this code will invoke this method


Method signature

Method Signature

A method’s signature refers to the method name

and the number, sequence and type of parameters.

A method is overloaded when the methods have the

Same name but have differentsignatures.


Type conversion and overloading

Type Conversion and Overloading

static double Mpg (double miles, double gallons)

{

return (miles / gallons);

}

if this method is called with the following code …

int m = 15;

int g = 3;

double result = Mpg (m, g);

m and g will be converted to double

when they are passed to the method.


Method parameters and overloading version 1 1

So … what happens if you also have this

method in your program?

intMpg (int goals, int misses)

{

return ( goals – misses);

}

and you make the method call

int miles = 2;

int gallons = 8;

int result = Mpg (m,g);

?


Rules for resolving overloading

Rules for Resolving Overloading

  • If there is a method whose signature exactly matches the parameters in the method call, than that method is selected first.

  • Otherwise, if there is a method whose signature matches the parameters of the method call, after doing some type upcasting conversion, then that method is selected.


Drivers

Drivers

When programming a large project, it is common

to code each method independently and then write

a driver method that tests that method. A driver is simply a

Method that invokes through its code the method being tested

in different ways to insure that the method works as expected.

Driver methods are temporary code that are not part of the

finished program, so they don’t have to be fancy.


Example1

Example

// calcAreamethod

// purpose: calculate the area of a rectangular region

// parameters: a integer length l and an integer width w

// returns: an integer result = l * w

static intCalcArea(int l, int w)

{

return l * w;

}


Method parameters and overloading version 1 1

the Driver method

int main( )

{

intheight=0, width=0, area=0;

char yes_no = ‘N’;

do

{

Console.WriteLine(“--------------------------";

Console.Write(“Enter an integer height: “);

height = int.Parse(Console.ReadLine( ) );

Console.Write(“Enter an integer width: “);

width = int.Parse(Console.ReadLine( ) );

area = CalcArea(height, width);

Console.WriteLine(“The area = {0}“, area);

Console.Write(“Test another pair of values (y or n): “);

yes_no = char.Parse(Console.ReadLine( ) );

yes_no = char.ToLower(yes_no);

}

while (yes_no == 'y');

}//End Main()


Method parameters and overloading version 1 1

Once a method has been debugged and you are

satisfied that it contains no errors, then move on

to creating and testing the next method.

Each method should be tested in a program where

it is the only untested component in the program.

Thus, if the program fails, you know where to look

for the error.


Stub methods

Stub Methods

Sometimes it is impossible to test one method

without using some other method which may not

yet be written or debugged.

In these cases you can write a simplified version of

the required method that only delivers sufficient

data to test the other method. These simplified

methods are called stubs.

Another purpose of a stud method is to be skeleton

as a place holder to remind us that we need to write

such a method.


Example2

Example

If we were testing some method that depended on

the CalcArea method, and the CalcArea method had

not yet been written and tested, we could provide

a stub that might look like this --

intCalcArea( int w, int l)

{

return 100;

}

double CalcArea(double w1, double l1)//skeleton stub method

{

return 0.0;

}

the method does not calculate the area

correctly, but just getting some data

returned might be sufficient to test the

other method.


Practice

Practice

Write a method, Add( ), that takes two integer

parameters. The parameters are passed by value.

The method returns the sum as an integer.


Practice1

Practice

Write a method, Add( ), that takes two integer

parameters. The parameters are passed by reference.

The method returns the sum as an integer.


Practice2

Practice

Write a Main( ) method that gets two values from the user

and then calls the Add method. Pass the parameters by value.

Then get two more values from the user. Call the add method

but this time pass the parameters by reference.


Practice3

Practice

Write a program that converts time in 24 hour notation to

its equivalent time in 12 hour notation. For example, 1320

would convert to 1:20pm. Write a method that does the

conversion. How will you pass the parameters? Could you

have used this method in the first project that you did?


Method parameters and overloading version 1 1

Practice

We want to write a program that tells what coins to give for

any amount of change from 1 to 99 cents. For example, for

86 cents, the program should output something like

86 cents can be given as

3 quarter(s) 1 dime(s) and 1 penny(pennies)

Only use coin denominations of 25 cents ( quarters ), 10 cents

( dimes ), and 1 cent ( pennies ).

Your program should use a method of the following form:

void ComputeCoins( intcoinValue, ref intnumber, ref intleft );

For example, if the amount left is 86 cents, and the coinValue is 25,

the value of number will be 3 and the new amount left will be 11.


  • Login