Methods and Data Passing

Slides:



Advertisements
Similar presentations
What is output line of the following C++ code? Please trace int i = 1, QP; DATA: 4, B, 3, A double credit, totCredit=0.0; double num = 0.0; string LG;
Advertisements

5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
©2004 Brooks/Cole Chapter 6 Methods. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Using Methods We've already seen examples of using methods.
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,
Methods & Activation Record. Recap: what’s a method?
Warm-Up: April 21 Write a loop (type of your choosing) that prints every 3 rd number between 10 and 50.
Conditionals (Cont’d). 2 Nested if/else question Formula for body mass index (BMI): Write a program that produces output like the following: This program.
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.
M. Taimoor Khan #include void main() { //This is my first C++ Program /* This program will display a string message on.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Methods (a.k.a. Functions)
Methods F Hello World! F Java program compilation F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters by value F Overloading.
FUNCTIONS IN C++. DEFINITION OF A FUNCTION A function is a group of statements that together perform a task. Every C++ program has at least one function,
1 Creating Web Services Presented by Ashraf Memon Hands-on Ghulam Memon, Longjiang Ding.
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.
FOR LOOP WALK THROUGH public class NestedFor { public static void main(String [] args) { for (int i = 1; i
BİL527 – Bilgisayar Programlama I Functions 1. Contents Functions Delegates 2.
TOPIC 6 Methods F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters F Pass by Value F Overloading Methods F Method Abstraction.
1 Chapter 5: Methods FIntroducing Methods FDeclaring Methods FCalling Methods FLocal Variables in Methods FPass by Value FOverloading Methods FMethod.
1 Class Chapter Objectives Use a while loop to repeat a series of statements Get data from user through an input dialog box Add error checking.
Creating Web Services Presented by Ashraf Memon Presented by Ashraf Memon.
1 Advanced Programming Examples Output. Show the exact output produced by the following code segment. char[,] pic = new char[6,6]; for (int i = 0; i
Chapter 5 Methods 1. Motivations Method : groups statements that perform a function.  Level of abstraction (black box)  Code Reuse – no need to reinvent.
Chapter 5 : Methods Part 2. Returning a Value from a Method  Data can be passed into a method by way of the parameter variables. Data may also be returned.
C# Programming Methods.
Classes - Intermediate
Computer Programming Lab 9. Exercise 1 Source Code package excercise1; import java.util.Scanner; public class Excercise1 { public static void main(String[]
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Methods. Creating your own methods Java allows you to create custom methods inside its main body. public class Test { // insert your own methods right.
INF120 Basics in JAVA Programming AUBG, COS dept Lecture 07 Title: Methods, part 1 Reference: MalikFarrell, chap 1, Liang Ch 5.
Looping Examples I.
התוכנית: using System; using System.Collections.Generic;
Algorithms and programming
BİL527 – Bilgisayar Programlama I
using System; namespace Demo01 { class Program
Sum of natural numbers class SumOfNaturalNumbers {
Lecture 11 B Methods and Data Passing
Starting Out with Java: From Control Structures through Objects
Functions Used to write code only once Can use parameters.
Computing Adjusted Quiz Total Score
Lecture 11 C Parameters Richard Gesick.
Chapter 5 Function Basics
Group Status Project Status.
Code Animation Examples
Method Overloading in JAVA
Pass by Reference.
Chapter 6 Methods.
Chapter 5 Methods.
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
Methods and Data Passing
class PrintOnetoTen { public static void main(String args[]) {
1D Arrays and Lots of Brackets
Module 4 Loops and Repetition 4/7/2019 CSE 1321 Module 4.
Module 3 Selection Structures 4/3/2019 CSE 1321 Module 3.
Methods and Data Passing
CSE Module 1 A Programming Primer
CSE Module 1 A Programming Primer
Methods and Data Passing
Building Java Programs
Module 4 Loops and Repetition 4/15/2019 CSE 1321 Module 4.
Object-Oriented Programming and class Design
Building Java Programs
Ps Module 7 – Part II 2D Arrays and LISTS 5/26/2019 CSE 1321 Module 7.
CSE Module 1 A Programming Primer
Parameters, Overloading Methods, and Random Garbage
Methods and Data Passing
MIS 222 – Lecture 12 10/9/2003.
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 4/4/2019 CSE 1321 Module 5

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

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 4/4/2019 CSE 1321 Module 4

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; 4/4/2019 CSE 1321 Module 4

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 (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

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) 4/4/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 4/4/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); 4/4/2019 CSE 1321 Module 5

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 10 4/4/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); 4/4/2019 CSE 1321 Module 5

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) { Console.WriteLine(math(7.5, 9.5)); // Prints 71.25 Console.WriteLine (math(4)); // Prints 16 Console.WriteLine (math()); // Prints ”Why does this work?” 4/4/2019 CSE 1321 Module 4