Chapter 6 Methods Chapter 6 - Methods.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Chapter 7: User-Defined Functions II
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.
 2006 Pearson Education, Inc. All rights reserved Functions.
16/11/2015 9:05 AM6/11/2015 9:05 AM6/11/2015 9:05 AMFunctions Functions A function consists of: Name Name Arguments (also called parameters) Arguments.
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
ISBN Chapter 9 Subprograms. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Introduction Two fundamental abstraction facilities.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
1 MATERI PENDUKUNG METHOD Matakuliah: M0074/PROGRAMMING II Tahun: 2005 Versi: 1/0.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
 2007 Pearson Education, Inc. All rights reserved C++ as a Better C; Introducing Object Technology.
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
Introduction to Methods
Review of C++ Programming Part II Sheng-Fang Huang.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "methods" in Java Purpose Reuse code Modularize the program This.
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
1 Introduction Modules  Most computer programs solve much larger problem than the examples in last sessions.  The problem is more manageable and easy.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
CPS120: Introduction to Computer Science Functions.
Functions Top-down design Breaking a complex problem into smaller parts that we can understand is a common practice. The process of subdividing a problem.
CPS120: Introduction to Computer Science Lecture 14 Functions.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Liang, Introduction to C++ Programming, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Advanced Function Features.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 15,16 Java’s Methods.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Methods Methods are how we implement actions – actions that objects can do, or actions that can be done to objects. In Alice, we have methods such as move,
CSI 3125, Preliminaries, page 1 Class. CSI 3125, Preliminaries, page 2 Class The most important thing to understand about a class is that it defines a.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-1 Why Write Methods? Methods are commonly used to break a problem down.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "___________________" in Java Purpose –Reuse code –Modularize.
Chapter 5 Methods 1. Motivations Method : groups statements that perform a function.  Level of abstraction (black box)  Code Reuse – no need to reinvent.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
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.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
(C) 2010 Pearson Education, Inc. All rights reserved.  Best way to develop and maintain a large program is to construct it from small, simple pieces,
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
Announcements. Practice questions, with and without solutions will be uploaded by Friday 5 th November, make sure to check them before the weekend \\netstorage\Subjects\ITCA-b\Exam.
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
Functions + Overloading + Scope
Chapter 7: User-Defined Functions II
Methods Chapter 6.
Methods.
User-Defined Functions
Chapter 6 Methods: A Deeper Look
Group Status Project Status.
Chapter 6 – Methods Topics are:
Java Methods: A Deeper Look Academic 2019 Class: BIT23/BCS10 Chapter 06 Abdulaziz Yasin Nageye Faculty of Computing Java How to Program, 10/e 1 © Co py.
Unit-1 Introduction to Java
Corresponds with Chapter 5
C Parameter Passing.
Introduction to Methods and Interfaces
Presentation transcript:

Chapter 6 Methods Chapter 6 - Methods

Methods A method is separate piece of code that can be called from another method to perform a specific function. Each method must be defined within the body of a class. The method is called by naming both the method and the object that it is defined within in an expression, together with a list of parameters in parentheses. Chapter 6 - Methods

Example Both methods defined within class SquareInt Call to method square from method main Definition of method square Chapter 6 - Methods

Why Use Methods? Independent testing of sub-tasks. Reusable code. Design and test a method once, and re-use it whenever you need to solve a similar problem. Isolation from unintended side effects. The only variables from the caller that can be seen from a method are those in the argument list. Chapter 6 - Methods

Method Definitions Return type Method name Arguments Keyword(s) Method body Returned value Chapter 6 - Methods

The Pass-by-Value Scheme Arguments are passed to Java methods by value, meaning that each argument is copied, and the copy is passed to the method. If an argument is modified within the method, it modifies the copy, so the value in the calling method is unaffected. This behavior protects the Java program against unintended side effects. Chapter 6 - Methods

Passing Objects To Methods To pass an object (such as an array) to a method, Java makes a copy of the reference to the object, and passes that copy to the method. The method can use the reference to modify the object being referred to. Thus, an object passed to a method can be changed, and unintended side effects can occur. Chapter 6 - Methods

Duration and Scope Every variable in Java is characterized by a duration and a scope. The duration of a variable is the time during which it exists. The scope of a variable is the portion of the program from which the variable can be addressed. Chapter 6 - Methods

Automatic Variables Variables defined within a method body are called local variables or automatic variables. These variables are automatically created when the method body is executed, and destroyed when execution ends. New variables are created each time that the method executes—no information is preserved between executions. These variables are said to have automatic duration. Chapter 6 - Methods

Scope The scope of a variable is the portion of the program from which it can be addressed. There are two possible scopes for Java variables: Class Scope - accessible anywhere within a class Block Scope - only accessible with the code block (such as a method body) in which they are defined. Automatic variables have block scope—they can only be addressed from within the method in which they are defined. Chapter 6 - Methods

Method Overloading Java allows multiple methods to be defined with the same name within a given class, as long as they have different calling parameters. When a method is called, the Java compiler automatically compares the calling parameters with all method definitions, and selects the one with the matching set of parameters. Chapter 6 - Methods

Method Overloading (2) Calls int version Calls double version Method square with int calling parameter Method square with double calling parameter Chapter 6 - Methods