Docsity

Call-by-Reference Methods: Void, Pass-by-Value, and Pass-by-Reference, Slides of C programming

Partial preview of the text

Download Call-by-Reference Methods: Void, Pass-by-Value, and Pass-by-Reference and more Slides C programming in PDF only on Docsity!

Call-by-Reference Methods

Overview Of Topics

  • Void Methods
  • Pass-by-reference Arguments
  • Calling Event Methods

Pass-by-value - Example

private 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; }

Values are Passed between Methods

Procedure Address Variable Value

btnCalculate( ) (^1010)

1020 1030

intQty decPrice decExtendedPrice

calcExtendedPrice (^1040)

1050 1060

intQty decPrice decExtended

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(); }

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

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.

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

calcExtendedPrice(intQty, decPrice, out decExtendedPrice)

pass-by-reference: address of decExtendedPrice is

sent

Procedure Address Variable Value btnCalculate (^1010) 1020 1030

intQty decPrice decExtendedPrice

calcExtendedPrice (^1040) 1050 1060

intQty decPrice decExtended

Method Uses Local Names of Arguments

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

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.

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.

Summary

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