Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 11 Parameters CSE 1322 4/26/2018.

Similar presentations


Presentation on theme: "Lecture 11 Parameters CSE 1322 4/26/2018."— Presentation transcript:

1 Lecture 11 Parameters 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. 4/26/2019

3 Method Structure 4/26/2019

4 Method Structure 4/26/2019

5 Parameters and Argument Passing
4/26/2019

6 Parameters There are three main types of parameters:
Value - passes a copy (value) of the variable to the method.  This is the default. Reference (C#)- passes a reference to the actual variable.  Marked with "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.  Marked with "out", use this when you want the method to generate 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

7 Example 1 and the output is ?
static void Main(String[] args) { int a = 42; Console.WriteLine (a); // Prints 42 B (a); // Prints 51 // Call-by-value; Console.WriteLine (a); // Prints 42 } static void B (int x) // Built-in type argument passing x += 9; Console.WriteLine (x); A Call-by-value Method with C# 4/26/2018

8 Example 1 and the output is ?
4/26/2018

9 Example 1 and the output is ?
static void Main(String[] args) { int a = 42; System.out.println (a); // 42 B (a); // 51 System.out.println (a); // 42 } static void B (int x) x += 9; System.out.println (x); 4/26/2018

10 A Call-By-Value Example with Java
public class CallByValueEx2 { public static void main(String[] args) { String myname = "Mokter"; // myname is a is String reference local variable System.out.println("Initially, inside main(), Name = " + myname ); callMe(myname); System.out.println("After calling callMe(), Name = " + myname); // Look here ??!!?? System.out.println("Now, inside main(), Name = " + myname ); } static void callMe(String newname ) { // newname is String reference argument newname = "Dr. Mokter Hossain"; System.out.println("Inside callMe(), Name = " + newname ); 4/26/2018

11 A Call-By-Value Example with Java
4/26/2018

12 Example 2 and the output is ?
static void Main(String[] args ) { int a = 42; Console.WriteLine (a); B (ref a); } static void B (ref int x) x += 9; Console.WriteLine (x); 4/26/2018

13 Example 2 and the output is ?
4/26/2018

14 Example 42 and the output is ?
4/26/2018

15 Example 3 and the output is ?
The out works same as ref but it is optional to initialize in the caller method Example 3 and the output is ? static void Main(String[] args) { int a; // NOT initialized B (out a); Console.WriteLine (a); } static void B (out int x) { x = 9; Console.WriteLine (x); Source: 4/26/2018

16 Example 3 and the output is ?
4/26/2018

17 Example 4 and the output is ?
using System; namespace CSE1322ReviewCS{ class Program { class Z { public int y; } static void Main(String[] args) { Z myZ = new Z(); myZ.y = 42; Console.WriteLine(myZ.y); B(myZ); Console.WriteLine("Press any key to exit."); Console.ReadKey(); } static void B(Z x) { x.y += 9; Console.WriteLine(x.y); 4/26/2018

18 Example 4 and the output is ?
class Z { public int y; } static void Main() { Z myZ = new Z(); myZ.y = 42; System.out.println (myZ.y); B (myZ); } static void B (Z x) x.y += 9; System.out.println (x.y); 4/26/2018

19 Example 4 and the output is ?
4/26/2018

20 Example 5 and the output is ?
using System; class Example5 { class Z { public int y; } static void Main(String[] args) { Console.WriteLine("Example5...."); Z myZ = new Z(); myZ.y = 42; Console.WriteLine(myZ.y); B(ref myZ); Console.ReadKey(); } static void B(ref Z x) { x = new Z(); x.y = 1; Console.WriteLine(x.y); 4/26/2018

21 Example 5 and the output is ?
4/26/2018

22 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

23 Overloading Methods Method overloading is the process of using the same method name for multiple methods 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

24 Overloading Methods float tryMe (int x) { return x + .375; } Version 1
float tryMe (int x, float y) { return x*y; } Version 2 result = tryMe (25, 4.32f) Invocation 4/26/2018

25 Overloading Operators
In C#, not only can methods be overloaded, operators can be overloaded as well. Java does not allow operator overloading 4/26/2018

26 4/26/2018

27 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. 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

28 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

29 4/26/2018

30 Overload the plus operator (+) to perform addition of ComplexNumbers
4/26/2018


Download ppt "Lecture 11 Parameters CSE 1322 4/26/2018."

Similar presentations


Ads by Google