Methods (a.k.a functions)

Slides:



Advertisements
Similar presentations
Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
Advertisements

Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
CIT 590 Intro to Programming Java lecture 4. Agenda Types Collections – Arrays, ArrayLists, HashMaps Variable scoping Access modifiers – public, private,
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,
Access to Names Namespaces, Scopes, Access privileges.
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
28-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Block Scope By Greg Butler Purpose The purpose of this presentation is to familiarize the student with the concept of variable scope, as it relates to.
SE-1010 Dr. Mark L. Hornick 1 Defining Your Own Classes Part 3.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Procedural programming in Java Methods, parameters and return values.
RECITATION 4. Classes public class Student { } Data Members public class Student { private String name; public String id; }
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
1 Building Java Programs Chapter 3: Introduction to Parameters and Objects These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They.
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.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
Building java programs, chapter 3 Parameters, Methods and Objects.
FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.
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.
Files Review For output to a file: –FileOutputStream variable initialized to filename (String) and append/not append (boolean) –PrintWriter variable initialized.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
CSC 142 F 1 CSC 142 References and Primitives. CSC 142 F 2 Review: references and primitives  Reference: the name of an object. The type of the object.
Methods.
OOP Basics Classes & Methods (c) IDMS/SQL News
COP 2220 Computer Science I Topics –Breaking Problems Down –Functions –User-defined Functions –Calling Functions –Variable Scope Lecture 4.
05 Method Calling. 2 What is a Method? Declaring a Method Method Calling Method Call Stack Parameter Passing Pass by Value Outline.
Exam 2 EXAM 2 Thursday!!! 25% of Final Grade Know: loops, switch/case Files Input failure (e.g. scan.hasNextInt())
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
Methods. Creating your own methods Java allows you to create custom methods inside its main body. public class Test { // insert your own methods right.
Methods Matthew Harrison. Overview ● There are five main aspects of methods... ● 1) Modifiers – public, private ● 2) Method Name ● 3) Parameters ● 4)
Functions + Overloading + Scope
Chapter 7 User-Defined Methods.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 7 User-Defined Methods.
Suppose we want to print out the word MISSISSIPPI in big letters.
Examples of Classes & Objects
AKA the birth, life, and death of variables.
Department of Computer Science
Lecture 4 D&D Chapter 5 Methods including scope and overloading Date.
Namespaces, Scopes, Access privileges
Methods.
CS1S467 GUI Programming LECTURE 15 Methods (2 of 2)
Class Structure 16-Nov-18.
Lecture 11 C Parameters Richard Gesick.
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
Classes & Objects: Examples
Variables and Their scope
Group Status Project Status.
Classes, Encapsulation, Methods and Constructors (Continued)
Class Structure 7-Dec-18.
Class Structure 2-Jan-19.
CS2011 Introduction to Programming I Arrays (II)
CS2011 Introduction to Programming I Methods (II)
Writing Functions.
CS2011 Introduction to Programming I Methods (I)
Chapter 6 Methods.
AKA the birth, life, and death of variables.
class PrintOnetoTen { public static void main(String args[]) {
Namespaces, Scopes, Access privileges
BBIT 212/ CISY 111 Object Oriented Programming (OOP)
Class Structure 25-Feb-19.
Scope of variables class scopeofvars {
Methods/Functions.
Corresponds with Chapter 5
ITE “A” GROUP 2 ENCAPSULATION.
Chapter 6: Methods CS1: Java Programming Colorado State University
Presentation transcript:

Methods (a.k.a functions) organize programs into sections allow sections of one program to be re-used in another program enhance the readability of a program 2

methods can be called from anywhere private static void meth2() { System.out.println(“in meth2”); } private static void meth3() System.out.println(“in meth3”); meth2(); // call meth2 Call Graph of program public static void main(String args[] ) { meth1(); meth2(); meth3(); } private static void meth1() System.out.println(“in meth1”); main meth1 meth2 meth3 meth2

Methods can take parameters Parameters allow you to pass data to the function Parameters must have a name & data type What are the parameters and data types of this function? Why can’t I print message from inside the function body? public static void main(String args[] ) { String message = “Hello World”; printMsg( message ); // Go DO the method CALL } private static void printMsg( String msg ) // method DEFINITION System.out.println( msg );

parameters are NOT modified by the method You are just sending in a COPY of the value public static void main( String args[] ) { int x = 3, y = 10; add2(x); // prints 5 System.out.println( “x: “ + x + “, y: “ + y ); // x: 3 y: 10 add2(y); // prints 12 } private static void add2(int p) p += 2; System.out.println( p );

Return statement - sends a value back from a method transfer a value back from a function you can only send back ONE piece of information can only be used with functions that are non-void e.g. private static float distance(int x1, int y1, int x2, int y2); private static int taxcode(float salary); rectArea main height length area

Example of parameters and return stmt public static void main( String args[] ) { int lo=1, hi=100; int rangeSum = calcRangeSum( lo, hi ); System.out.println("sum of 1 thru 100= " + rangeSum ); } // END main private static int calcRangeSum( int lo, int hi) int sum=0; // local variable - lives & dies in this method for (int i=lo ; i<=hi ; ++i) sum+=i; return sum; // sum of all the numbers from lo to hi } // END calcRangeSum

Embedded function calls public static void main() { int a = 5, b = 8, g; g = meth5(meth6(a), meth5(a, b)); System.out.println( “g is “ + g ); } private static int meth5(int v, int w) return v + w; private static int meth6(int p) return p - 2;

Scope You can have variables with the same name in different methods. public static void main() { int a = 4, b = 6, c; int a = 15, c = 9; // ILLEGAL can’t re-declare in same scope } private static void meth1(int x, int y){ int a = 7, b = 5; // OK: different block i.e. {} ... private static void meth2(int a, int b) // OK: different a & b int c; // OK: different c than main’s

Scope Actual and Formal parameter names do NOT have to match the values map by POSITION public static void main() { int g = 50, h = 90, k; k = meth3(g, h); System.out.println( k ); k = meth3(h, g); k = meth3(h, h); } private static int meth3(int g, int h) return g - h;