CIS 162 AD C CallbyReference Methods 06methodsbyref ppt
CIS 162 AD – 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 CIS 162 AD 2
Top-Down Design ¨ A design method where the major task to be accomplished is divided into subtasks. ¨ Each subtask should perform a single welldefined task. ¨ Each subtask may produce some result. ¨ Treat them as small programs. Input -> Process -> Output CIS 162 AD 3
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. CIS 162 AD 4
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 (CS 7). ¨ 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 mnu. Edit. Calculate_Click (object sender, System. Event. Args e) { btn. Calculate_Click(sender, e); } CIS 162 AD 5
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 calc. Extended. Price( ) are assigned their own memory and can only be referenced in calc. Extended. Price( ). ¨ Different methods cannot see or reference each others variables. ¨ They have separate memory allocations even though the variable names may be the same. 6 CIS 162 AD
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. CIS 162 AD 7
Pass-by-value - Example private void btn. Calculate_Click(…) { } int. Qty; decimal dec. Price, dec. Extended. Price; int. Qty = int. Parse(txt. Quantity. Text); dec. Price = decimal. Parse(txt. Price. Text); dec. Extended. Price = calc. Extended. Price(int. Qty, dec. Price); private decimal calc. Extended. Price(int int. Qty, decimal dec. Price) { decimal dec. Extended; } CIS 162 AD dec. Extended = int. Qty * dec. Price; return dec. Extended; 8
Values are Passed between Methods Procedure Address Variable Value btn. Calculate( ) 1010 1020 1030 int. Qty dec. Price 2 35. 50 71. 00 1040 1050 1060 int. Qty 2 dec. Price 35. 50 dec. Extended 71. 00 calc. Extended. Price CIS 162 AD dec. Extended. Price 9
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… CIS 162 AD 10
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 btn. Exit_Click (object sender, System. Event. Args e) { this. Close(); } CIS 162 AD 11
Calling Void Methods ¨ A method that returns a value needs a variable to the left of the equal sign. dec. Extended. Price = calc. Extended. Price(int. Qty, dec. Price); ¨ A call to a void method does not need a variable and equal sign on the left. calc. Extended. Price(int. Qty, dec. Price, out dec. Extended. Price); CIS 162 AD 12
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 passby-value. CIS 162 AD 13
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. CIS 162 AD 14
Pass-by-reference Example private void btn. Calculate_Click(…) { } int. Qty; decimal dec. Price, dec. Extended. Price; int. Qty = int. Parse(txt. Quantity. Text); dec. Price = decimal. Parse(txt. Price. Text); calc. Extended. Price(int. Qty, dec. Price, out dec. Extended. Price); private void calc. Extended. Price(int int. Qty, decimal dec. Price, out decimal dec. Extended) { } dec. Extended = int. Qty * dec. Price; return; CIS 162 AD 15
Declare Variables and Get Values Procedure Address Variable Value btn. Calculate 1010 1020 1030 1040 1050 1060 int. Qty dec. Price 2 35. 50 0 calc. Extended. Price CIS 162 AD dec. Extended. Price int. Qty 0 dec. Price 0 dec. Extended 0 16
calc. Extended. Price(int. Qty, dec. Price, out dec. Extended. Price) pass-by-reference: address of dec. Extended. Price is sent Procedure Address Variable Value btn. Calculate 1010 1020 1030 1040 1050 1060 int. Qty dec. Price 2 35. 50 0 calc. Extended. Price CIS 162 AD dec. Extended. Price int. Qty 2 dec. Price 35. 50 dec. Extended 1030 17
Method Uses Local Names of Arguments private void btn. Calculate_Click(…) { } int. Qty; decimal dec. Price, dec. Extended. Price; int. Qty = int. Parse(txt. Quantity. Text); dec. Price = decimal. Parse(txt. Price. Text); calc. Extended. Price(int. Qty, dec. Price, out dec. Extended. Price); private void calc. Extended. Price(int int. Qty, decimal dec. Price, out decimal dec. Extended) { } dec. Extended = int. Qty * dec. Price; return; CIS 162 AD 18
Perform Calculations dec. Extended = int. Qty * dec. Price; Procedure Address Variable Value btn. Calculate 1010 1020 1030 1040 1050 1060 int. Qty dec. Price 2 35. 50 71. 00 calc. Extended. Price dec. Extended. Price int. Qty 2 dec. Price 35. 50 dec. Extended 1030 Value is stored at memory location provided through argument. CIS 162 AD 19
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. CIS 162 AD 20
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. CIS 162 AD 21
Summary ¨ Pass-by-value Reviewed ¨ Void Methods ¨ Pass-by-reference CIS 162 AD 22
- Slides: 22