Methods and Data Passing

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Chapter 5 Functions.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 5 Function Basics.
Lecture 3: Topics If-then-else Operator precedence While loops Static methods Recursion.
Methods Liang, Chapter 4. What is a method? A method is a way of running an ‘encapsulated’ series of commands. System.out.println(“ Whazzup ”); JOptionPane.showMessageDialog(null,
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
Introduction to Java Programming, 4E Y. Daniel Liang.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Introduction to Methods
METHODS Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian
Methods and You. Up to this point, I have covered many different data types with you. Variables can be considered the nouns of an English sentence. If.
Chapter 6 Functions 1. Opening Problem 2 Find the sum of integers from 1 to 10, from 20 to 37, and from 35 to 49, respectively.
Parameters. Overview A Reminder Why Parameters are Needed How Parameters Work Value Parameters Reference Parameters Out Parameters.
Oct 15, 2007Sprenkle - CS1111 Objectives Creating your own functions.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 5.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
User Defined Functions Chapter 7 2 Chapter Topics Void Functions Without Parameters Void Functions With Parameters Reference Parameters Value and Reference.
1 Chapter 6 Methods. 2 Motivation Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
Chapter 5 Methods 1. Motivations Method : groups statements that perform a function.  Level of abstraction (black box)  Code Reuse – no need to reinvent.
Classes - Intermediate
Lecture 6: Methods MIT-AITI Kenya © 2005 MIT-Africa Internet Technology Initiative In this lecture, you will learn… What a method is Why we use.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 6 Methods Dr. Musab Zghoul.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 6 Methods.
INF120 Basics in JAVA Programming AUBG, COS dept Lecture 07 Title: Methods, part 1 Reference: MalikFarrell, chap 1, Liang Ch 5.
Functions + Overloading + Scope
User-Written Functions
Chapter 7 User-Defined Methods.
Suppose we want to print out the word MISSISSIPPI in big letters.
Suppose we want to print out the word MISSISSIPPI in big letters.
Chapter 6: Methods CS1: Java Programming Colorado State University
Chapter 6 Functions.
Chapter 5 Function Basics
Chapter 6 Methods 1.
Functions CIS 40 – Introduction to Programming in Python
Lecture 4 D&D Chapter 5 Methods including scope and overloading Date.
Methods.
Lecture 11 B Methods and Data Passing
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Lecture 11 C Parameters Richard Gesick.
User Defined Functions
Chapter 5 Function Basics
6 Chapter Functions.
Group Status Project Status.
CHAPTER 6 GENERAL-PURPOSE METHODS
Defining methods and more arrays
Chapter 6 Methods.
Chapter 5 Methods.
Topics Introduction to Functions Defining and Calling a Void Function
BBIT 212/ CISY 111 Object Oriented Programming (OOP)
Week 4 Lecture-2 Chapter 6 (Methods).
Methods and Data Passing
Methods and Data Passing
Methods and Data Passing
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Classes, Objects and Methods
Parameters, Overloading Methods, and Random Garbage
CPS125.
Methods and Data Passing
Methods and Data Passing
Chapter 6: Methods CS1: Java Programming Colorado State University
Presentation transcript:

Methods and Data Passing Module 5 Methods and Data Passing 2/16/2019 CSE 1321 Module 5

Overview Terminology The Return Type The Method’s Name Scope of Variables Parameters Calling Methods Runtime Stack Overloading 2/16/2019 CSE 1321 Module 5

Terminology A function or 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

Also Known As… Functions can be called several things, depending on the book or context Examples: Procedure Module Method (OOP) Behavior (OOP) Member function (OOP)

Why have functions? CREATE average, userNum1, userNum2 PRINT (“Please enter the 2 numbers”) READ userNum1 READ userNum2 average = (userNum1 + userNum2) / 2 // a lot of other code // more code here, then

Look! This code is the same! CREATE average, userNum1, userNum2 PRINT (“Please enter the 2 numbers”) READ userNum1 READ userNum2 average = (userNum1 + userNum2) / 2 // a lot of other code // more code here, then

Instead of Repeating Code CREATE average, userNum1, userNum2 PRINT (“Please enter the 2 numbers”) READ userNum1 READ userNum2 average = (userNum1 + userNum2) / 2 // a lot of other code // more code here, then

Create a Function Instead CREATE average, userNum1, userNum2 PRINT (“Please enter the 2 numbers”) READ userNum1 READ userNum2 average = (userNum1 + userNum2) / 2 // a lot of other code // more code here, then READ userNum1 READ userNum2 average=(userNum1 + userNum2)/2

Give the Function a Name CREATE average, userNum1, userNum2 PRINT (“Please enter the 2 numbers”) READ userNum1 READ userNum2 average = (userNum1 + userNum2) / 2 // a lot of other code // more code here, then myFunction READ userNum1 READ userNum2 average=(userNum1 + userNum2)/2

Then Call the Function Instead CREATE average, userNum1, userNum2 PRINT (“Please enter the 2 numbers”) myFunction // a lot of other code // more code here, then myFunction READ userNum1 READ userNum2 average=(userNum1 + userNum2)/2

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 around (no longer top to bottom) 2/16/2019 CSE 1321 Module 5

“Modularizing” Code Software design concept using modules. Can be used to reduce redundant coding and enable code reuse. Can also improve the quality of the program. 2/16/2019 CSE 1321 Module 5

What is a Method? Think of a method as a black box that does a specific task. The method may take use inputs (parameters) and may return an output with a specific type. 2/16/2019 CSE 1321 Module 5

Defining Methods A method has a header and a body. The header is the method declaration. It’s usually the top line The body is a collection of statements grouped together to perform an operation. It’s the rest of the method 2/16/2019 CSE 1321 Module 5

Header/Body Example // Header is top line public static int Sum(int num1, int num2) { // Begin the body int sum = 0; for(int i = num1; i <= num2; i++) { sum += i; } return sum; } // End the body 2/16/2019 CSE 1321 Module 4

Header/Body Example # Top line is the header def sum(num1, num2): # Body begins here sum = 0 for i in range(num1, num2 + 1): sum += i return sum # Body ends here 2/16/2019 CSE 1321 Module 4

Method Signature Method signature is the combination of the method name and the parameter list. It’s part of the top line of the method Examples of method signatures are circled on the following slide. 2/16/2019 CSE 1321 Module 5

Method Signature // Signature is Sum (int num1, int num2) public static int Sum(int num1, int num2) { int sum = 0; for(int i = num1; i <= num2; i++) sum += i; } return sum; 2/16/2019 CSE 1321 Module 4

Method Signature # Signature is sum(num1, num2) def sum(num1, num2): # Body begins here sum = 0 for i in range(num1, num2 + 1): sum += i return sum # Body ends here 2/16/2019 CSE 1321 Module 4

The return type The return type is the data type of the value the method returns. Java and C#: return type in the method header use void if no data will be returned. must be a return statement if return type is not void. Python: simply uses the return statement in the body of the function. No return type is defined in the header. 2/16/2019 CSE 1321 Module 5

Figure out the return type Method Name Return type average ? getLetterGrade ? areYouAsleep ? getGPA ? printMenu ? getStudentName ? calculateAverage ? 2/16/2019 CSE 1321 Module 5

Figure out the return type Method Name Return type average double or float getLetterGrade char areYouAsleep bool getGPA double or float // Don’t confuse what a method does with its return type! printMenu void getStudentName string calculateAverage double or float 2/16/2019 CSE 1321 Module 5

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 2/16/2019 CSE 1321 Module 5

Scope of Variables (or “who can see what”) Variables within a method can only be seen by that method – called a local variable! Need a way to send information to the method Method needs to send back information Example: method1 can’t see myInt; likewise, method 2 cannot see myChar method1 method2 myChar myInt 2/16/2019 CSE 1321 Module 5

Technically Speaking… The scope of a local variable starts from its when it’s declared and continues to the end of the block that contains the variable. Example: if (myVar > yourVar) { int myLocalVariable = 15; // Other code } // myLocalVariable doesn’t exist here 2/16/2019 CSE 1321 Module 5

Parameters Methods cannot see each other’s variables (scope) Parameters are 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 2/16/2019 CSE 1321 Module 5

More on Parameters Variables in the method header are called formal parameters Values passed to the method are called actual parameters or arguments. Arguments passed by value mean that a copy is passed to formal parameter (later) Arguments passed by reference mean the method gets the original and can change it! (later) In Python, all parameters are passed by reference. 2/16/2019 CSE 1321 Module 5

Find the Return Type and Parameters void doSomething (int data) double average (int num1, int num2) boolean didHePass ( ) char whatWasHisGrade ( ) void scareStudent (char gradeOfStudent) 2/16/2019 CSE 1321 Module 5

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! 2/16/2019 CSE 1321 Module 5

Example (always start at main) class MyClass { public static void main (String args[ ]) { int num1, num2, num3; double result1, result2; num1 = 5; num2 = 7; num3 = 4; result1 = average (num1, num2); result2 = average (num3, num1); } x y double average (int x, int y) { return ( (x+y) / 2); } // Note: the average function is currently inactive

Example (declare variables) class MyClass { public static void main (String args[ ]) { 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); } ? ? ? // Note: the average function is currently inactive

Example (declare variables) class MyClass { public static void main (String args[ ]) { 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); } ? ? ? result1 result2 ? ? // Note: the average function is currently inactive

Example Memory double average (int x, int y) { return ( (x+y) / 2); } class MyClass { public static void main (String args[ ]) { 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); } 5 7 4 result1 result2 ? ? // Note: the average function is currently inactive

Example (call the function) class MyClass { public static void main (String args[ ]) { 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); } 5 7 4 result1 result2 ? ?

Example (data passing) class MyClass { public static void main (String args[ ]) { int num1, num2, num3; double result1, result2; num1 = 5; num2 = 7; num3 = 4; result1 = average (num1, num2); result2 = average (num3, num1); } x y 5 7 Memory num1 num2 num3 double average (int x, int y) { return ( (x+y) / 2); } 5 7 4 result1 result2 ? ?

Example (main falls asleep) class MyClass { public static void main (String args[ ]) { int num1, num2, num3; double result1, result2; num1 = 5; num2 = 7; num3 = 4; result1 = average (num1, num2); result2 = average (num3, num1); } x y 5 7 Memory num1 num2 num3 double average (int x, int y) { return ( (x+y) / 2); } 5 7 4 result1 result2 ? ? // The function is now ACTIVE

Example (function is doin’ its thing) class MyClass { public static void main (String args[ ]) { int num1, num2, num3; double result1, result2; num1 = 5; num2 = 7; num3 = 4; result1 = average (num1, num2); result2 = average (num3, num1); } x y 5 7 Memory num1 num2 num3 double average (int x, int y) { return ( (x+y) / 2); } 5 7 4 result1 result2 ? ? // 5 + 7 is 12; 12 / 2 == 6

Example (function finishes and passes info back) class MyClass { public static void main (String args[ ]) { int num1, num2, num3; double result1, result2; num1 = 5; num2 = 7; num3 = 4; result1 = average (num1, num2); result2 = average (num3, num1); } x y 5 7 Memory num1 num2 num3 6 double average (int x, int y) { return ( (x+y) / 2); } 5 7 4 result1 result2 ? ? // 5 + 7 is 12; 12 / 2 == 6

Example (main wakes back up; average sleeps) class MyClass { public static void main (String args[ ]) { int num1, num2, num3; double result1, result2; num1 = 5; num2 = 7; num3 = 4; result1 = average (num1, num2); // 6 result2 = average (num3, num1); } sleep x y Memory num1 num2 num3 double average (int x, int y) { return ( (x+y) / 2); } 5 7 4 result1 result2 6 ?

Example (re-using the average function with num3 and num1) class MyClass { public static void main (String args[ ]) { int num1, num2, num3; double result1, result2; num1 = 5; num2 = 7; num3 = 4; result1 = average (num1, num2); // 6 result2 = average (num3, num1); } WAKE UP! x y Memory num1 num2 num3 double average (int x, int y) { return ( (x+y) / 2); } 5 7 4 result1 result2 6 ?

Example (re-using the average function with num3 and num1) class MyClass { public static void main (String args[ ]) { int num1, num2, num3; double result1, result2; num1 = 5; num2 = 7; num3 = 4; result1 = average (num1, num2); result2 = average (num3, num1); } x y 4 5 Memory num1 num2 num3 double average (int x, int y) { return ( (x+y) / 2); } 5 7 4 result1 result2 6 ?

Example (main asleep, function awake) class MyClass { public static void main (String args[ ]) { int num1, num2, num3; double result1, result2; num1 = 5; num2 = 7; num3 = 4; result1 = average (num1, num2); result2 = average (num3, num1); } x y 4 5 Memory num1 num2 num3 double average (int x, int y) { return ( (x+y) / 2); } 5 7 4 result1 result2 6 ?

Example (function doin’ it again) class MyClass { public static void main (String args[ ]) { int num1, num2, num3; double result1, result2; num1 = 5; num2 = 7; num3 = 4; result1 = average (num1, num2); result2 = average (num3, num1); } x y 4 5 Memory num1 num2 num3 double average (int x, int y) { return ( (x+y) / 2); } 5 7 4 result1 result2 6 ? // 4 + 5 == 9; 9 / 2 == 4.5

Example (function doin’ it again) class MyClass { public static void main (String args[ ]) { int num1, num2, num3; double result1, result2; num1 = 5; num2 = 7; num3 = 4; result1 = average (num1, num2); result2 = average (num3, num1); } x y 4.5 4 5 Memory num1 num2 num3 double average (int x, int y) { return ( (x+y) / 2); } 5 7 4 result1 result2 6 ? // 4 + 5 == 9; 9 / 2 == 4.5

Example (main wakes back up; average sleeps) class MyClass { public static void main (String args[ ]) { int num1, num2, num3; double result1, result2; num1 = 5; num2 = 7; num3 = 4; result1 = average (num1, num2); // 6 result2 = average (num3, num1); // 4.5 } sleep x y Memory num1 num2 num3 double average (int x, int y) { return ( (x+y) / 2); } 5 7 4 result1 result2 6 4.5

Another Quick Example (void return types) class MyClass { public static void main (String args[ ]) { int num1, num2, num3; num1 = 5; num2 = 7; num3 = 4; printNum (num1); } sleep myNum void printNum (int myNum) { PRINT (myNum); }

Another Quick Example (declare variables) class MyClass { public static void main (String args[ ]) { int num1, num2, num3; num1 = 5; num2 = 7; num3 = 4; printNum (num1); } sleep myNum Memory num1 num2 num3 void printNum (int myNum) { PRINT (myNum); } ? ? ?

Another Quick Example (initialize variables) class MyClass { public static void main (String args[ ]) { int num1, num2, num3; num1 = 5; num2 = 7; num3 = 4; printNum (num1); } sleep myNum Memory num1 num2 num3 void printNum (int myNum) { PRINT (myNum); } 5 7 4

Another Quick Example (call the function) class MyClass { public static void main (String args[ ]) { int num1, num2, num3; num1 = 5; num2 = 7; num3 = 4; printNum (num1); } WAKE UP! myNum Memory num1 num2 num3 void printNum (int myNum) { PRINT (myNum); } 5 7 4

Another Quick Example (data passing) class MyClass { public static void main (String args[ ]) { int num1, num2, num3; num1 = 5; num2 = 7; num3 = 4; printNum (num1); } myNum 5 Memory num1 num2 num3 void printNum (int myNum) { PRINT (myNum); } 5 7 4

Another Quick Example (function does its thing; main asleep) class MyClass { public static void main (String args[ ]) { int num1, num2, num3; num1 = 5; num2 = 7; num3 = 4; printNum (num1); } myNum 5 Memory num1 num2 num3 void printNum (int myNum) { PRINT (myNum); } 5 7 4

Another Quick Example (function does its thing; main asleep) class MyClass { public static void main (String args[ ]) { int num1, num2, num3; num1 = 5; num2 = 7; num3 = 4; printNum (num1); } Output 5 myNum 5 Memory num1 num2 num3 void printNum (int myNum) { PRINT (myNum); } 5 7 4

Another Quick Example (function is done) class MyClass { public static void main (String args[ ]) { int num1, num2, num3; num1 = 5; num2 = 7; num3 = 4; printNum (num1); } Output 5 myNum 5 Memory num1 num2 num3 void printNum (int myNum) { PRINT (myNum); } 5 7 4

Another Quick Example (function falls asleep; main awake) class MyClass { public static void main (String args[ ]) { int num1, num2, num3; num1 = 5; num2 = 7; num3 = 4; printNum (num1); } Output 5 myNum Memory num1 num2 num3 void printNum (int myNum) { PRINT (myNum); } 5 7 4

In-class Problem Problem Statement: Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively. 2/16/2019 CSE 1321 Module 5

Psuedocode - A Bad Solution (where is the repeated code?) sum ← 0 FOR (i ← 1 i <= 10 i ← i+1) sum ← sum + i ENDFOR PRINT("Sum from 1 to 10 is “, sum) FOR (int i ← 20 i <= 30 i ← i+1) PRINT("Sum from 20 to 30 is “, sum) for (int i ← 35 i <= 45 i ← i+1) PRINT("Sum from 35 to 45 is “, sum) 2/16/2019 CSE 1321 Module 5

METHOD MAIN BEGIN CREATE result ← SUM(arguments: 1,10) PRINT("Sum from 1 to 10 is:”, result) result ← SUM(arguments: 20,30) PRINT("Sum from 20 to 30 is:”, result) result ← SUM(arguments: 35,45) PRINT("Sum from 35 to 45 is:”, result) END MAIN METHOD SUM(parameters: num1, num2) BEGIN CREATE sum ← 0 FOR (i ← num1, i <= num2, i ← i+1 ) sum ← sum + i ENDFOR RETURN sum END SUM Ps 2/16/2019 CSE 1321 Module 5

Java Example – Method Sum public static int Sum(int num1, int num2) { int sum = 0; for(int i = num1; i <= num2; i++) sum += i; } return sum; public static void main(String[] args) int result = Sum(1, 10); System.out.println("Sum from 1 to 10 is " + result); result = Sum(20, 30); System.out.println("Sum from 20 to 30 is " + result); result = Sum(35, 45); System.out.println("Sum from 35 to 45 is " + result); 2/16/2019 CSE 1321 Module 5

C# Example – Method Sum public static int Sum(int num1, int num2) { for(int i = num1; i <= num2; i ++) sum += i; } return sum; public static void Main(string[] args) int result = Sum(1, 10); Console.WriteLine("Sum from 1 to 10 is " + result); result = Sum(20, 30); Console.WriteLine("Sum from 20 to 30 is " + result); result = Sum(35, 45); Console.WriteLine("Sum from 35 to 45 is " + result); 2/16/2019 CSE 1321 Module 5

Python Example – Method Sum def sum(num1, num2): sum = 0 for i in range(num1, num2 + 1): sum += i return sum def main(): print("Sum from 1 to 10 is ", sum(1, 10)) print("Sum from 20 to 30 is ", sum(20, 30)) print("Sum from 35 to 45 is ", sum(35,45)) main() #Call the main function 2/16/2019 CSE 1321 Module 5

Next Problem Problem Statement: Write a method that accepts two integers via parameters, determines the largest number, and returns that value. 2/16/2019 CSE 1321 Module 4

Psuedocode - Method Max METHOD MAIN BEGIN CREATE i ← 5 CREATE j ← 2 CREATE k ← max(i, j) PRINT("The maximum of ", i , " and ” , j ," is ", k) END MAIN METHOD MAX(parameters: num1, num2) BEGIN CREATE result if (num1 > num2) result ← num1 else result ← num2 RETURN result END MAX 62 2/16/2019 CSE 1321 Module 5

Java – Method Max public static void main(String[] args) { int i = 5, j = 2; int k = max(i, j); System.out.println("The maximum of "+i+" and "+j+" is "+k); } public static int max(int num1, int num2) { int result; if (num1 > num2) result = num1; else result = num2; return result; } 2/16/2019 CSE 1321 Module 5

C# – Method Max public static int max(int num1, int num2) { int result; if (num1 > num2) result = num1; else result = num2; return result; } public static void Main(string[] args) int i = 5, j = 2; int k = max(i, j); Console.WriteLine("The maximum of {0} and {1}is{2}", i, j, k); 2/16/2019 CSE 1321 Module 5

Python – Method Max def max(num1, num2): result = 0 if(num1 > num2): result = num1 else: result = num2 return result def main(): i=2 j=5 print("Max between ",j," and ",i," is ",max(i,j)) main() #Call to the main function 2/16/2019 CSE 1321 Module 5

The Runtime Stack The runtime stack keeps track of active (currently running) methods in the program, and order of method calls. The top of the stack represents the currently running (active) method in the program. The bottom represents main method often program. When a method finished, it is removed from the stack. We’ll study these later when we talk about “recursion” 66 2/16/2019 CSE 1321 Module 5

Call Stacks 2/16/2019 CSE 1321 Module 5

Overloading Methods Overloading is create a multiple functions with the same name, but different types of parameters (type and/or number). The IDE knows which method to call based on the parameters that are passed to it. 2/16/2019 CSE 1321 Module 5

public static int math(int x, int y) { return x+y; } class Main { public static int math(int x, int y) { return x+y; } public static int math (int x) { return x*x; public static double math(double x, double y) { return x*y; public static String math (){ return "Why does this work?"; public static void main(String[] args) { System.out.println(math(7.5, 9.5)); // Prints 71.25 System.out.println (math(4)); // Prints 16 System.out.println (math()); // Prints ”Why does this work?” 2/16/2019 CSE 1321 Module 4

public static int math(int x, int y) { return x+y; } class MainClass { public static int math(int x, int y) { return x+y; } public static int math (int x) { return x*x; public static double math(double x, double y) { return x*y; public static string math (){ return "Why does this work?"; public static void Main(string[] args) { System.out.println(math(7.5, 9.5)); // Prints 71.25 System.out.println (math(4)); // Prints 16 System.out.println (math()); // Prints ”Why does this work?” 2/16/2019 CSE 1321 Module 4

Python Example – Method Max Python does not support overloading. In fact, if you define the same method twice, the most recent implementation is the only one that can be used. However, Python is a dynamically typed language, which means that it automatically figures out the data type according to the value assigned, or in this case, passed. 2/16/2019 CSE 1321 Module 5

Final Rules You cannot define a method inside of another method Methods always reside in a class in C# and Java (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 2/16/2019 CSE 1321 Module 5

End of Module 5 2/16/2019 CSE 1321 Module 5