Supplementary for method, DCO10803, Quarter 3, 2002-3 Page 1 of 7 Object-Oriented Programming and Design DCO10803 Supplementary for method  Prototype.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
Chapter 7: User-Defined Functions II
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Lecture 2 Basics of C#. Members of a Class A field is a variable of any type that is declared directly in a class. Fields are members of their containing.
Inheritance Inheritance Reserved word protected Reserved word super
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk.
SCOPE & I/O CSC 171 FALL 2004 LECTURE 5. CSC171 Room Change Thursday, September 23. CSB 209 THERE WILL BE A (group) QUIZ! - topic: the CS department at.
1 Modularity In “C”. 2 General Syntax data_type function_Name (parameter list) { … return expression; // return output } Body of the function is a compound.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
1 Methods Overview l Closer Look at Methods l Parameter passing l Passing parameters by value l Passing parameters by reference.
Rossella Lau Lecture 5, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 5: Class construction  Encapsulation 
C Lecture Notes Functions (Cont...). C Lecture Notes 5.8Calling Functions: Call by Value and Call by Reference Used when invoking functions Call by value.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
Chapter 7: User-Defined Methods
Parameters, Arguments, Local Variables, and Scope CSC 1401: Introduction to Programming with Java Week 8 – Lecture 1 Wanda M. Kunkle.
The different kinds of variables in a Java program.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
COMP More About Classes Yi Hong May 22, 2015.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Class Example - Rationals Rational numbers are represented by the ratio of two integers, a numerator and a denominator, e.g., 2/3. This is opposed to irrational.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
Dale Roberts CSCI 230 Functions Scope, Parameter Passing, Storage Specifiers Department of Computer and Information Science, School of Science, IUPUI Dale.
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
Java methods Methods break down large problems into smaller ones Your program may call the same method many times saves writing and maintaining same code.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
1 MORE ON MODULAR DESIGN: MODULE COMMUNICATIONS. 2 WHEN A FUNCTION IS INVOKED, MEMORY IS ALLOCATED LOCALLY FOR THE FORMAL PARAMETERS AND THE VALUE OF.
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];
Classes - Intermediate
Methods.
Utilities (Part 2) Implementing static features 1.
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
COP 2220 Computer Science I Topics –Breaking Problems Down –Functions –User-defined Functions –Calling Functions –Variable Scope Lecture 4.
Invoking methods in the Java library. Jargon: method invocation Terminology: Invoking a method = executing a method Other phrases with exactly the same.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
JAVA METHODS (FUNCTIONS). Why are they called methods? Java is a strictly object-oriented programming language Methods are functions inside of objects.
Chapter 7 User-Defined Methods.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 7 User-Defined Methods.
Introduction to Programming
Chapter 7: User-Defined Functions II
C Functions -Continue…-.
Object Oriented Systems Lecture 03 Method
Programming Fundamentals Lecture #7 Functions
User-Defined Functions
Chapter 5 - Functions Outline 5.1 Introduction
Scope, Parameter Passing, Storage Specifiers
Classes, Objects, and Methods
Group Status Project Status.
Classes, Encapsulation, Methods and Constructors (Continued)
CHAPTER 6 GENERAL-PURPOSE METHODS
Defining methods and more arrays
CS2011 Introduction to Programming I Methods (II)
BBIT 212/ CISY 111 Object Oriented Programming (OOP)
Chapter 7: User-Defined Functions II
Chapter 9: Value-Returning Functions
Local variables and how to recognize them
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
Corresponds with Chapter 5
Chapter 6: Methods CS1: Java Programming Colorado State University
Presentation transcript:

Supplementary for method, DCO10803, Quarter 3, Page 1 of 7 Object-Oriented Programming and Design DCO10803 Supplementary for method  Prototype (signature)  Call and parameter passing (Extracted from former lecture 2 of DCO10103) -- By Rossella Lau

Supplementary for method, DCO10803, Quarter 3, Page 2 of 7 method body method header Revisit of Quadratic.java method call import java.io.*; import java.lang.*; // can be imported automatically public class Quadratic {//assumes b^2 > 4ac …… // the standard 3 I/O objects private static double root1, root2; //the fields to store the roots private static void main(String[] args) throws IOException { …… // input a, b, c findRoot(a, b, c); stdOut.println("x1 = " + root1 + ", x2 = " + root2); } protected static void findRoot(double para_a, double para_b, double para_c) { double t = java.lang.Math.sqrt(para_b * para_b - 4 * para_a * para_c); root1 = (-para_b + t) / (2d * para_a); root2 = (-para_b - t) / (2d * para_a); }

Supplementary for method, DCO10803, Quarter 3, Page 3 of 7 Methods  In Java, a function inside a class is called a method Function/method prototype – method header definition:  value type resulted by the function  type may be any primitive data or object (valued function) e.g., calculateNetPay()  type may be void – no value is resulted from the function e.g., findRoot()  the name of the function  number of and types of the parameters  e.g., para_a, para_b, para_c

Supplementary for method, DCO10803, Quarter 3, Page 4 of 7 Method definition  The body of the method defines what about the method is inside {} after the method header  It uses the actual value of the parameter(s) (passed by a calling statement) to compute a result  It can also define its local variables  the values or the identifiers of the variables are not visible to any outsider.  it cannot use variables defined inside other methods, e.g., findRoot() cannot use a, b, c of main().  It should return a value for a valued method (return type of the method is not void).

Supplementary for method, DCO10803, Quarter 3, Page 5 of 7 Variable scopes and life of variables  The visibility mentioned for local variables also called the scope of variables  A method is a scope – Local variables and parameters of a method are only visible to the method  A block – a series of statements quoted by braces is also a scope  A class is a scope – class data/objects are visible to the whole class – i.e. visible to all methods inside the class – similar to global variables  Variables are born when its associated scope is invoked and "died" when the scope is terminated

Supplementary for method, DCO10803, Quarter 3, Page 6 of 7 Invoking a method  To invoke a method means to execute a method or to call a method  The statement that invokes (calls) a method should pass the required number of parameters respectively  In some situation, the passed value may be promoted to another type: e.g., from an integer to a double  The method call results in a value returned by a valued method  can be used in an expression  may need to be stored for further use – it is more efficient if the result is required in more than one statement – avoid invoking more than once if the method call is the same

Supplementary for method, DCO10803, Quarter 3, Page 7 of 7 Execution of methods  A method is invoked by a calling action. E.g.,  findSmallest(5, 9, 7);  A calling action activates a called method  A called method returns the execution control to the statement after its calling statement or the next action of the calling statement s = findSmallest(5, 9, 7); stdOut.println(s); … int findSmallest(a,b,c){ ……/data declaration if (a < b) return result; }