Presentation is loading. Please wait.

Presentation is loading. Please wait.

Parameters, Overloading Methods, and Random Garbage

Similar presentations


Presentation on theme: "Parameters, Overloading Methods, and Random Garbage"— Presentation transcript:

1 Parameters, Overloading Methods, and Random Garbage
Lecture 11 Parameters, Overloading Methods, and Random Garbage CSE 1322 4/26/2018

2 What is a Method? Think of a method as a black box that contains the detailed implementation for a specific task. The method may use inputs (parameters) and may return an output with a specific type. 7/16/2019

3 Method Structure Return Type Method Name Formal Parameters Modifiers Method Header public static int addTwoNums (int x, int y) { return x + y; } Method Body 7/16/2019

4 Actual parameters (called ”arguments”)
Calling the Method Return Type Method Name Formal Parameters Modifiers Method Header public static int addTwoNums (int x, int y) { return x + y; } Method Body Actual parameters (called ”arguments”) int result = addTwoNums (5, 2); 7/16/2019

5 Parameters There are three main types of parameters:
Value - passes a copy of the value of the variable to the method.  This is the default. Reference (C#)- passes a reference to the actual variable.  Keyword is ref. Use this when you want to pass a value in and have any change to that value be persistent when the method is complete Out (C#) - passes a reference to the actual variable.  Keyword is out. Use this when you want the method to initialize a value and place it for later use in the actual variable (persists when the method is complete) Note that Java passes all parameters by value. C++ uses both Call-by-reference and Call-by-value 4/26/2018

6 Example 1 - and the output is ?
static void B (int x) { // Built-in type argument passing x += 9; PRINT (x); } public static void M/main (S/string[] args) { int a = 42; PRINT (a); // Prints 42 B (a); // Prints 51 because is call-by-value; PRINT (a); // Prints 42 } 4/26/2018

7 Example 2 - and the output is ?
static void B (ref int x) { x += 9; Console.WriteLine (x); } public static void Main (string[] args) { int a = 42; Console.WriteLine (a); // Prints 42 B (ref a); // Prints 51 BUT! a has changed! Console.WriteLine (a); // Prints 51 } 4/26/2018

8 What about out? Remember, this is used to initialize a variable
Why even have it? We have ref, right? Honestly, out isn’t used often, but: Marks the “intent” of what you’re doing Could prevent bad data from coming in 4/26/2018

9 Example 3 - and the output is ?
static void B (out int x) { x = 9; Console.WriteLine (x); } public static void Main (string[] args) { int a; // We can't print this yet - not initialized B (out a); // Initializes a and prints 9 Console.WriteLine (a); // Prints 9 } 4/26/2018

10 What about Complex Data Types?
With classes/objects, we still pass by value HOWEVER, we are copying the reference We’ll start with code, then look at what’s going on 4/26/2018

11 class Dog { public int weight; } static void B(Dog x) { // IMPORTANT
class Dog { public int weight; } static void B(Dog x) { // IMPORTANT! x is referencing the same // chunk of memory myDog is referencing! x.weight += 9; PRINT(x.weight); } public static void M/main(S/string[] args) { Dog myDog = new Dog(); myDog.weight = 10; PRINT(myDog.weight); // 10 B(myDog); // 19 PRINT(myDog.weight); // 19 4/26/2018

12 class Dog { public int weight; } static void B(Dog x) { x
class Dog { public int weight; } static void B(Dog x) { x.weight += 9; PRINT(x.weight); } public static void M/main(S/string[] args) { Dog myDog = new Dog(); myDog.weight = 10; PRINT(myDog.weight); B(myDog); myDog weight = 0; 4/26/2018

13 class Dog { public int weight; } static void B(Dog x) { x
class Dog { public int weight; } static void B(Dog x) { x.weight += 9; PRINT(x.weight); } public static void M/main(S/string[] args) { Dog myDog = new Dog(); myDog.weight = 10; PRINT(myDog.weight); B(myDog); myDog weight = 10; 4/26/2018

14 class Dog { public int weight; } static void B(Dog x) { x
class Dog { public int weight; } static void B(Dog x) { x.weight += 9; PRINT(x.weight); } public static void M/main(S/string[] args) { Dog myDog = new Dog(); myDog.weight = 10; PRINT(myDog.weight); B(myDog); // PRINTS 10 to the screen myDog weight = 10; 4/26/2018

15 class Dog { public int weight; } static void B(Dog x) { x
class Dog { public int weight; } static void B(Dog x) { x.weight += 9; PRINT(x.weight); } public static void M/main(S/string[] args) { Dog myDog = new Dog(); myDog.weight = 10; PRINT(myDog.weight); B(myDog); // MAGIC HAPPENS! x is a copy of myDog... // That is, x points to the same thing // myDog points to x myDog weight = 10; 4/26/2018

16 Watch what happens to the weight variable
IMPORTANT Watch what happens to the weight variable 4/26/2018

17 class Dog { public int weight; } static void B(Dog x) { x
class Dog { public int weight; } static void B(Dog x) { x.weight += 9; // Look at weight before… PRINT(x.weight); } public static void M/main(S/string[] args) { Dog myDog = new Dog(); myDog.weight = 10; PRINT(myDog.weight); B(myDog); x myDog weight = 10; 4/26/2018

18 class Dog { public int weight; } static void B(Dog x) { x
class Dog { public int weight; } static void B(Dog x) { x.weight += 9; // Look at weight after! PRINT(x.weight); } public static void M/main(S/string[] args) { Dog myDog = new Dog(); myDog.weight = 10; PRINT(myDog.weight); B(myDog); x myDog weight = 19; 4/26/2018

19 Now, myDog’s weight has now changed!
IMPORTANT Now, myDog’s weight has now changed! 4/26/2018

20 What about adding ref? class Dog { public int weight; } static void B(ref Dog x) { x = new Dog(); x.weight = 9; Console.WriteLine(x.weight); } public static void Main(string[] args) { Dog myDog = new Dog(); myDog.weight = 10; Console.WriteLine(myDog.weight); B(ref myDog); 4/26/2018

21 What about adding ref? myDog weight = 0;
class Dog { public int weight; } static void B(ref Dog x) { x = new Dog(); x.weight = 9; Console.WriteLine(x.weight); } public static void Main(string[] args) { Dog myDog = new Dog(); myDog.weight = 10; Console.WriteLine(myDog.weight); B(ref myDog); myDog weight = 0; 4/26/2018

22 What about adding ref? myDog weight = 10;
class Dog { public int weight; } static void B(ref Dog x) { x = new Dog(); x.weight = 9; Console.WriteLine(x.weight); } public static void Main(string[] args) { Dog myDog = new Dog(); myDog.weight = 10; Console.WriteLine(myDog.weight); B(ref myDog); myDog weight = 10; 4/26/2018

23 What about adding ref? myDog weight = 10;
class Dog { public int weight; } static void B(ref Dog x) { x = new Dog(); x.weight = 9; Console.WriteLine(x.weight); } public static void Main(string[] args) { Dog myDog = new Dog(); myDog.weight = 10; Console.WriteLine(myDog.weight); B(ref myDog); // OUTPUT 10 myDog weight = 10; 4/26/2018

24 What about adding ref? myDog == x weight = 10;
class Dog { public int weight; } static void B(ref Dog x) { x = new Dog(); x.weight = 9; Console.WriteLine(x.weight); } public static void Main(string[] args) { Dog myDog = new Dog(); myDog.weight = 10; Console.WriteLine(myDog.weight); B(ref myDog); myDog == x weight = 10; 4/26/2018

25 What about adding ref? weight = 0; myDog == x weight = 10;
class Dog { public int weight; } static void B(ref Dog x) { x = new Dog(); x.weight = 9; Console.WriteLine(x.weight); } public static void Main(string[] args) { Dog myDog = new Dog(); myDog.weight = 10; Console.WriteLine(myDog.weight); B(ref myDog); // Uh oh... weight = 0; myDog == x weight = 10; 4/26/2018

26 Our first piece of garbage
No one is pointing to the old dog anymore! We’ve created trash Yes, it’s really called that (or garbage) Yes, we have a garbage collector (GC) The GC picks up unloved objects There’s no way to recover the unloved dog Rule: when no one points to an object, it’s TRASHED! weight = 0; myDog == x weight = 10; 4/26/2018

27 What about adding ref? weight = 9; myDog == x weight = 10;
class Dog { public int weight; } static void B(ref Dog x) { x = new Dog(); x.weight = 9; Console.WriteLine(x.weight); } public static void Main(string[] args) { Dog myDog = new Dog(); myDog.weight = 10; Console.WriteLine(myDog.weight); B(ref myDog); weight = 9; myDog == x weight = 10; 4/26/2018

28 What about adding ref? weight = 9; myDog == x weight = 10;
class Dog { public int weight; } static void B(ref Dog x) { x = new Dog(); x.weight = 9; Console.WriteLine(x.weight); } public static void Main(string[] args) { Dog myDog = new Dog(); myDog.weight = 10; Console.WriteLine(myDog.weight); B(ref myDog); // OUTPUT 9 to the screen weight = 9; myDog == x weight = 10; 4/26/2018

29 What about adding ref? weight = 9; myDog == x weight = 10;
class Dog { public int weight; } static void B(ref Dog x) { x = new Dog(); x.weight = 9; Console.WriteLine(x.weight); } public static void Main(string[] args) { Dog myDog = new Dog(); myDog.weight = 10; Console.WriteLine(myDog.weight); B(ref myDog); // OUTPUT 9 to the screen AGAIN because myDog has changed weight = 9; myDog == x weight = 10; 4/26/2018

30 Pass-by-value vs. Pass-by-reference vs. Pass-by-reference-type
Be careful to note the difference between a pass-by-reference parameter and a parameter of a reference type. Use the activation stack to track local variables and how parameters of the above types affect the variables from one stack frame to the next. 4/26/2018

31 Overloading Methods Method overloading: using the same method name for multiple methods Do not confuse with overriding (from inheritance) The signature of each overloaded method must be unique The signature includes the number, type, and order of the parameters The compiler determines which version of the method is being invoked by analyzing the parameters The return type of the method is not part of the signature 4/26/2018

32 Overloading Methods float tryMe (int x) { return x + .375; }
float tryMe (int x, float y) return x*y; // Which tryMe is called? result = tryMe (25, 4.32f); 4/26/2018

33 Overloading Operators
In C# and C++, not only can methods be overloaded, even operators (e.g. +, -, *, /, ==, ++, etc) can be overloaded. Java does not allow operator overloading 4/26/2018

34 Operator overloading C# enables you to overload most operators to make them sensitive to the context in which they are used. Use operator overloading when it makes an application clearer than accomplishing the same operations with explicit method calls. Example: Class ComplexNumber overloads the plus (+), minus (-) and multiplication (*) operators to enable programs to add, subtract and multiply instances of class ComplexNumber using common mathematical notation 4/26/2018

35 Rules for Operator Overloading
Keyword operator, followed by an operator symbol, indicates that a method overloads the specified operator. Methods that overload binary operators must take two arguments—the first argument is the left operand, and the second argument is the right operand. Overloaded operator methods must be public and static. 4/26/2018

36 public class Dog { public string name; public int weight; public Dog(string name, int weight) { this.name = name; this.weight = weight; } public static Dog operator+ (Dog d1, Dog d2) {// Overload the + operator Dog mergedDog = new Dog("ComboDog", 0); mergedDog.name = d1.name+":"+d2.name; mergedDog.weight = d1.weight+d2.weight; return mergedDog; } } class MainClass { public static void Main(String[] args) { Dog myDog = new Dog("CSE", 8); Dog yourDog = new Dog ("1322", 9); Dog uberDog = myDog + yourDog; // Add two dogs together! Console.WriteLine (uberDog.name + " weighs " + uberDog.weight); } // OUTPUT is CSE:1322 weighs 17 4/26/2018


Download ppt "Parameters, Overloading Methods, and Random Garbage"

Similar presentations


Ads by Google