Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 11 B Methods and Data Passing

Similar presentations


Presentation on theme: "Lecture 11 B Methods and Data Passing"— Presentation transcript:

1 Lecture 11 B Methods and Data Passing
Richard Gesick

2 Overview Terminology Why we have Methods Data Passing The Mantra
Calling Methods Scope of Variables

3 Terminology A method is a logical grouping of statements
Reusable chunks of code Write once Call as many times as you like Benefits Reusable Easy to work at higher level of abstraction Reduces complexity Reduces size of code

4 AKA (also known as) Methods can be called several things, depending on the book or context Examples: Procedure Module Method (OOP) Behavior (OOP) Member Function (OOP)

5 You’ve Already Seen Methods
Denotes a Method of the class Denotes a property of the class

6 MidiPlayer.Play(new NoteOn(0, GeneralMidiPercussion.BassDrum, 127));
Invoking Methods MidiPlayer.Play(new NoteOn(0, GeneralMidiPercussion.BassDrum, 127)); Two things going on here Creating a new NoteOn object Passing it three parameters “Constructing” (more later) Invoking the Play method of the MidiPlayer class Passing it one parameter (the NoteOn object) This is a “static” method (more later)

7 Why have Methods? (see anything similar?)
double average; int userNum1, userNum2; Console.WriteLine(“Please enter the 2 numbers”); userNum1 = Int32.Parse(Console.ReadLine()); userNum2 = Int32.Parse(Console.ReadLine()); average = (userNum1 + userNum2) / 2; … // a lot of other code

8 Why have Methods? (see anything similar?)
double average; int userNum1, userNum2; Console.WriteLine(“Please enter the 2 numbers”); userNum1 = Int32.Parse(Console.ReadLine()); userNum2 = Int32.Parse(Console.ReadLine()); average = (userNum1 + userNum2) / 2; … // a lot of other code All of this code is the same!

9 Basic Idea double average; int userNum1, userNum2; Console.WriteLine(“Please enter the 2 numbers”); userNum1 = Int32.Parse(Console.ReadLine()); userNum2 = Int32.Parse(Console.ReadLine()); average = (userNum1 + userNum2) / 2; … // a lot of other code

10 Basic Idea Create method instead
double average; int userNum1, userNum2; Console.WriteLine(“Please enter the 2 numbers”); userNum1 = Int32.Parse(Console.ReadLine()); userNum2 = Int32.Parse(Console.ReadLine()); average = (userNum1 + userNum2) / 2; … // a lot of other code Create method instead userNum1 = Int32.Parse(Console.ReadLine()); userNum2 = Int32.Parse(Console.ReadLine()); average = (userNum1 + userNum2) / 2;

11 Give the Method a Name myMethod
double average; int userNum1, userNum2; Console.WriteLine(“Please enter the 2 numbers”); userNum1 = Int32.Parse(Console.ReadLine()); userNum2 = Int32.Parse(Console.ReadLine()); average = (userNum1 + userNum2) / 2; … // a lot of other code myMethod userNum1 = Int32.Parse(Console.ReadLine()); userNum2 = Int32.Parse(Console.ReadLine()); average = (userNum1 + userNum2) / 2;

12 Call the Method (instead of writing all that code)
double average; int userNum1, userNum2; Console.WriteLine(“Please enter the 2 numbers”); myMethod … // a lot of other code myMethod userNum1 = Int32.Parse(Console.ReadLine()); userNum2 = Int32.Parse(Console.ReadLine()); average = (userNum1 + userNum2) / 2;

13 What have we done? Written the code once, but called it many times
Reduced the size of our code Easier to comprehend This is called procedural abstraction Tracing of code skips all around (no longer linear)

14 Scope of Variables Scope – “who can see what”
Variables that are defined within a method can only be seen by that method! We need a way to send information to the method We need a way for the method to send back information Example: method1 can’t see myInt method1 method2 char myChar; int myInt;

15 Examples private int sum(int x, int y) private string getUserChoice() private void isMatch(int guess)

16 The Mantra All methods follow a mantra The mantra is:
Return type, Method name, parameters

17 The return type A method has the option to return us (the calling method) some information If the method doesn’t return us any info, the return type is void Otherwise, the return type is the data type it’s going to return Example of return types: int char boolean

18 Figure out the return type
Method Name average double or float getLetterGrade char areYouAsleep bool getGPA double or float printMenu void // Don’t confuse what a method does with it’s // return type! getStudentName string

19 The Method’s name Similar to naming of variables
Can be almost anything except A reserved word (keywords) Can’t begin with a number Can’t contain strange symbols except _ and $ Method names should begin with a lower case Different standards/conventions are used If multiple words in method name, capitalize the first letter in each word (except the first) Example: thisIsAnExample

20 Parameters Methods cannot see each other’s variables (scope)
Special variables used to “catch” data being passed This is the only way the main algorithm and Methods have to communicate! Located between parentheses ( ) If no parameters are needed, leave the parentheses empty

21 Examples Remember the mantra What can you tell me about these methods?
void doSomething (int data) double average (int num1, int num2) boolean didHePass ( ) char whatWasHisGrade ( ) void scareStudent (char gradeOfStudent)

22 Getting the Method to Work for You
Call it by name Pass it the right stuff Pass the right number of parameters If it expects two things, pass it two things! Pass the right type of parameters If it expects a char, don’t pass it a double! Parameters must match exactly If it returns something, do something with it!

23 Example int num1, num2, num3; double result1, result2; num1 = 5; num2 = 7; num3 = 4; result1 = average (num1, num2); result2 = average (num3, num1); x y Memory double average (int x, int y) { return ( (x+y) / 2.0); } // Note: the average method is currently inactive

24 Example (declare variables)
int num1, num2, num3; double result1, result2; num1 = 5; num2 = 7; num3 = 4; result1 = average (num1, num2); result2 = average (num3, num1); x y Memory num1 num2 num3 double average (int x, int y) { return ( (x+y) / 2.0); } // Note: the average method is currently inactive

25 Example (declare variables)
int num1, num2, num3; double result1, result2; num1 = 5; num2 = 7; num3 = 4; result1 = average (num1, num2); result2 = average (num3, num1); x y Memory num1 num2 num3 double average (int x, int y) { return ( (x+y) / 2.0); } result1 result2 0.0 0.0

26 Example (set values) Memory
int num1, num2, num3; double result1, result2; num1 = 5; num2 = 7; num3 = 4; result1 = average (num1, num2); result2 = average (num3, num1); x y Memory num1 num2 num3 double average (int x, int y) { return ( (x+y) / 2.0); } 5 result1 result2 0.0 0.0

27 Example (set values) Memory
int num1, num2, num3; double result1, result2; num1 = 5; num2 = 7; num3 = 4; result1 = average (num1, num2); result2 = average (num3, num1); x y Memory num1 num2 num3 double average (int x, int y) { return ( (x+y) / 2.0); } 5 7 result1 result2 0.0 0.0

28 Example (set values) Memory
int num1, num2, num3; double result1, result2; num1 = 5; num2 = 7; num3 = 4; result1 = average (num1, num2); result2 = average (num3, num1); x y Memory num1 num2 num3 double average (int x, int y) { return ( (x+y) / 2.0); } 5 7 4 result1 result2 0.0 0.0

29 Example (invoke method)
int num1, num2, num3; double result1, result2; num1 = 5; num2 = 7; num3 = 4; result1 = average (num1, num2); result2 = average (num3, num1); WAKE UP! x y Memory num1 num2 num3 double average (int x, int y) { return ( (x+y) / 2.0); } 5 7 4 result1 result2 0.0 0.0

30 Example (data passing)
int num1, num2, num3; double result1, result2; num1 = 5; num2 = 7; num3 = 4; result1 = average (num1, num2); result2 = average (num3, num1); x y Memory 5 7 num1 num2 num3 double average (int x, int y) { return ( (x+y) / 2.0); } 5 7 4 result1 result2 0.0 0.0

31 Example (caller sleeps)
int num1, num2, num3; double result1, result2; num1 = 5; num2 = 7; num3 = 4; result1 = average (num1, num2); result2 = average (num3, num1); x y Memory 5 7 num1 num2 num3 double average (int x, int y) { return ( (x+y) / 2.0); } 5 7 4 result1 result2 0.0 0.0 // The method is now ACTIVE

32 Example (method active)
int num1, num2, num3; double result1, result2; num1 = 5; num2 = 7; num3 = 4; result1 = average (num1, num2); result2 = average (num3, num1); x y Memory 5 7 num1 num2 num3 double average (int x, int y) { return ( (x+y) / 2.0); } 5 7 4 result1 result2 0.0 0.0 // is 12; 12 / 2 is 6

33 Example (value returned)
int num1, num2, num3; double result1, result2; num1 = 5; num2 = 7; num3 = 4; result1 = average (num1, num2); result2 = average (num3, num1); x y Memory 6 5 7 num1 num2 num3 double average (int x, int y) { return ( (x+y) / 2.0); } 5 7 4 result1 result2 0.0 0.0

34 Example (caller resumes)
int num1, num2, num3; double result1, result2; num1 = 5; num2 = 7; num3 = 4; result1 = average (num1, num2); result2 = average (num3, num1); sleep x y Memory num1 num2 num3 double average (int x, int y) { return ( (x+y) / 2.0); } 5 7 4 result1 result2 6.0 0.0

35 Example (call method again)
int num1, num2, num3; double result1, result2; num1 = 5; num2 = 7; num3 = 4; result1 = average (num1, num2); result2 = average (num3, num1); WAKE UP! x y Memory num1 num2 num3 double average (int x, int y) { return ( (x+y) / 2.0); } 5 7 4 result1 result2 6.0 0.0

36 Example (data passing)
int num1, num2, num3; double result1, result2; num1 = 5; num2 = 7; num3 = 4; result1 = average (num1, num2); result2 = average (num3, num1); x y Memory 4 5 num1 num2 num3 double average (int x, int y) { return ( (x+y) / 2.0); } 5 7 4 result1 result2 6.0 0.0

37 Example (caller sleeps)
int num1, num2, num3; double result1, result2; num1 = 5; num2 = 7; num3 = 4; result1 = average (num1, num2); result2 = average (num3, num1); x y Memory 4 5 num1 num2 num3 double average (int x, int y) { return ( (x+y) / 2.0); } 5 7 4 result1 result2 6.0 0.0 // The method is now ACTIVE

38 Example (method active)
int num1, num2, num3; double result1, result2; num1 = 5; num2 = 7; num3 = 4; result1 = average (num1, num2); result2 = average (num3, num1); x y Memory 4 5 num1 num2 num3 double average (int x, int y) { return ( (x+y) / 2.0); } 5 7 4 result1 result2 6.0 0.0 // is 9; 9 / 2 = 4.5

39 Example (value returned)
int num1, num2, num3; double result1, result2; num1 = 5; num2 = 7; num3 = 4; result1 = average (num1, num2); result2 = average (num3, num1); x y Memory 4 5 4.5 num1 num2 num3 double average (int x, int y) { return ( (x+y) / 2.0); } 5 7 4 result1 result2 6.0 0.0

40 Example (caller resumes)
int num1, num2, num3; double result1, result2; num1 = 5; num2 = 7; num3 = 4; result1 = average (num1, num2); result2 = average (num3, num1); sleep x y Memory num1 num2 num3 double average (int x, int y) { return ( (x+y) / 2.0); } 5 7 4 result1 result2 6.0 4.5

41 Method Rules You cannot define a method inside of another method
Methods always reside in a class in C# (as you’ve seen numerous time already) Methods cannot see each other’s variables You’ve seen “private static” in our examples… more on this later

42 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

43 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

44 Overloading Operators
In C#, not only can methods be overloaded, operators can be overloaded as well.

45


Download ppt "Lecture 11 B Methods and Data Passing"

Similar presentations


Ads by Google