22
CIS162AD – C# Call-by-Reference Methods 06_methods_by_ref.ppt

CIS162AD – C#

Embed Size (px)

DESCRIPTION

CIS162AD – C#. Call-by-Reference Methods 06_methods_by_ref.ppt. Overview Of Topics. Review Top-Down Design Calling Event Methods Review pass-by-value Arguments Introduce Void Methods Introduce pass-by-reference Arguments. Top-Down Design. - PowerPoint PPT Presentation

Citation preview

Page 1: CIS162AD – C#

CIS162AD – C#

Call-by-Reference Methods

06_methods_by_ref.ppt

Page 2: CIS162AD – C#

CIS162AD 2

Overview Of Topics

Review Top-Down Design Calling Event Methods Review pass-by-value Arguments Introduce Void Methods Introduce pass-by-reference Arguments

Page 3: CIS162AD – C#

CIS162AD 3

Top-Down Design

A design method where the major task to be accomplished is divided into subtasks.

Each subtask should perform a single well-defined task.

Each subtask may produce some result. Treat them as small programs.

Input -> Process -> Output

Page 4: CIS162AD – C#

CIS162AD 4

Top-Down Design Implementation Top-Down design is implemented using methods. A method is a collection of statements that are grouped

together to perform a specific operation. Methods have inputs called arguments. Methods can only return one value through the return

statement. A method that will not return a value should have it’s

return type defined as void. Methods can “return” more than one value through

call-by-reference arguments.

Page 5: CIS162AD – C#

CIS162AD 5

Calling Event Methods One of the reasons to breakup programs into small

methods is so that code does not duplicated. If there is a method that does what we need to do, we just call it.

For example, we can add a menu option to perform the same calculations defined for a button (CS7).

Instead of duplicating the code, we can call the button method which has the code defined and tested from the menu method. Be sure to pass the required arguments.

private void mnuEditCalculate_Click(object sender, System.EventArgs e)

{btnCalculate_Click(sender, e);

}

Page 6: CIS162AD – C#

CIS162AD 6

Local Variables Variables are local to the method in which they are

defined. Variables defined in a particular method are

assigned their own memory and can only be referenced in that method.

Variables defined in calcExtendedPrice( ) are assigned their own memory and can only be referenced in calcExtendedPrice( ).

Different methods cannot see or reference each others variables.

They have separate memory allocations even though the variable names may be the same.

Page 7: CIS162AD – C#

CIS162AD 7

Pass-by-value Reviewed

Methods with pass-by-value arguments will be passing the values of the arguments into the methods local variables.

The method cannot change the values stored in the calling method.

Two different memory locations are used for each variable.

One in the calling method and one in the called method.

Page 8: CIS162AD – C#

CIS162AD 8

Pass-by-value - Exampleprivate void btnCalculate_Click(…){

int intQty;decimal decPrice, decExtendedPrice;

intQty = int.Parse(txtQuantity.Text);decPrice = decimal.Parse(txtPrice.Text);decExtendedPrice = calcExtendedPrice(intQty, decPrice);

}

private decimal calcExtendedPrice(int intQty, decimal decPrice){

decimal decExtended;

decExtended = intQty * decPrice;return decExtended;

}

Page 9: CIS162AD – C#

CIS162AD 9

Values are Passed between Methods

Procedure Address Variable Value

btnCalculate( ) 1010

10201030

intQtydecPricedecExtendedPrice

2

35.5071.00

calcExtendedPrice 1040

10501060

intQty

decPricedecExtended

2

35.5071.00

Page 10: CIS162AD – C#

CIS162AD 10

Methods and Pass-by-Value

Values of the arguments are passed to the method’s local variables.

Many values can be passed to methods, but only one value can be returned.

Enter void and pass-by-reference methods…

Page 11: CIS162AD – C#

CIS162AD 11

Void Methods

No value returned through return statement. Return statement is optional. Defined the same way as call-by-value,

but return type is void.

private void btnExit_Click(object sender, System.EventArgs e)

{this.Close();

}

Page 12: CIS162AD – C#

CIS162AD 12

Calling Void Methods

A method that returns a value needs a variable to the left of the equal sign.

decExtendedPrice = calcExtendedPrice(intQty, decPrice);

A call to a void method does not need a variable and equal sign on the left.

calcExtendedPrice(intQty, decPrice, out decExtendedPrice);

Page 13: CIS162AD – C#

CIS162AD 13

Pass-by-Reference

Addresses of the arguments are passed to the method’s local variables.

Many values can be passed to the methods, and many values can be “returned”.

Use the keyword ref or out to make the argument pass-by-reference.

If you leave ref or out off, the default is pass-by-value.

Page 14: CIS162AD – C#

CIS162AD 14

ref or out

ref is used for a variable that will be initialized before the method call.

out is used for a variable that might not be initialized before the method call, but the variable will initialized within the called method.

Page 15: CIS162AD – C#

CIS162AD 15

Pass-by-reference Example

private void btnCalculate_Click(…){

int intQty;decimal decPrice, decExtendedPrice;

intQty = int.Parse(txtQuantity.Text);decPrice = decimal.Parse(txtPrice.Text);calcExtendedPrice(intQty, decPrice, out decExtendedPrice);

}

private void calcExtendedPrice(int intQty, decimal decPrice, out decimal decExtended)

{decExtended = intQty * decPrice;return;

}

Page 16: CIS162AD – C#

CIS162AD 16

Declare Variables and Get Values

Procedure Address Variable Value

btnCalculate 1010

10201030

intQtydecPricedecExtendedPrice

235.500

calcExtendedPrice 1040

10501060

intQtydecPricedecExtended

0

00

Page 17: CIS162AD – C#

CIS162AD 17

calcExtendedPrice(intQty, decPrice, out decExtendedPrice) pass-by-reference: address of decExtendedPrice is sent

Procedure Address Variable Value

btnCalculate 1010

10201030

intQtydecPricedecExtendedPrice

235.500

calcExtendedPrice 1040

10501060

intQtydecPricedecExtended

2

35.501030

Page 18: CIS162AD – C#

CIS162AD 18

Method Uses Local Names of Argumentsprivate void btnCalculate_Click(…){

int intQty;decimal decPrice, decExtendedPrice;

intQty = int.Parse(txtQuantity.Text);decPrice = decimal.Parse(txtPrice.Text);calcExtendedPrice(intQty, decPrice, out decExtendedPrice);

}

private void calcExtendedPrice(int intQty, decimal decPrice, out decimal decExtended)

{decExtended = intQty * decPrice;return;

}

Page 19: CIS162AD – C#

CIS162AD 19

Perform Calculations decExtended = intQty * decPrice;

Procedure Address Variable Value

btnCalculate 1010

10201030

intQtydecPricedecExtendedPrice

235.5071.00

calcExtendedPrice 1040

10501060

intQtydecPricedecExtended

2

35.501030

Value is stored at memory location provided through argument.

Page 20: CIS162AD – C#

CIS162AD 20

Returning Multiple Values

Addresses of the arguments are passed to the method’s local variables instead of the values stored in the arguments.

The called method can then modify memory locations assigned to the calling method.

When it does modify its memory location, it can be considered to have “returned” a value.

Code many pass-by-reference (ref or out) arguments to “return” more than one value.

Page 21: CIS162AD – C#

CIS162AD 21

Summary of Arguments

Arguments can be a mixture of pass-by-value and pass-by reference.

If a method should NOT change the value, send it as a pass-by-value.

If values are being returned through the arguments, then make the method a void method.

Page 22: CIS162AD – C#

CIS162AD 22

Summary

Pass-by-value Reviewed Void Methods Pass-by-reference