CS2011 Introduction to Programming I Methods (I)

Slides:



Advertisements
Similar presentations
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.
Advertisements

Introduction to Computers and Programming Lecture 11: Introduction to Methods Professor: Evan Korth New York University.
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.
Introduction to Computers and Programming Introduction to Methods in Java.
Sub Programs To Solve a Problem, First Make It Simpler.
CS 106 Introduction to Computer Science I 02 / 25 / 2008 Instructor: Michael Eckmann.
Introduction to Computers and Programming Lecture 13: User defined methods Instructor: Evan Korth New York University.
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
CS 106 Introduction to Computer Science I 02 / 23 / 2007 Instructor: Michael Eckmann.
Introduction to Methods
METHODS Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian
CPS120: Introduction to Computer Science Functions.
ECE122 Feb. 22, Any question on Vehicle sample code?
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Procedural programming in Java Methods, parameters and return values.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
SE-1010 Dr. Mark L. Hornick 1 Java Programming Basics.
1 Structure of a C Program (continued) Presentation original from Dr. Turner’s class USF - COP C for Engineers Summer 2008.
1 Brief Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
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.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
CMSC 150 METHODS AND CLASSES CS 150: Wed 25 Jan 2012.
CMSC 150 METHODS AND CLASSES CS 150: Wed 25 Jan 2012.
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapters 4 and 5: Excerpts l Class and Method Definitions l Information.
Creating and Using Class Methods. Definition Class Object.
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.
Methods.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
COP 2220 Computer Science I Topics –Breaking Problems Down –Functions –User-defined Functions –Calling Functions –Variable Scope Lecture 4.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Java 5 Class Anatomy. User Defined Classes To this point we’ve been using classes that have been defined in the Java standard class library. Creating.
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.
Chapter 9: Value-Returning Functions
Chapter 6: Methods CS1: Java Programming Colorado State University
Chapter 5 Functions DDC 2133 Programming II.
Chapter 5 Function Basics
Chapter 6 Methods 1.
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Functions Inputs Output
Static Methods 14-Nov-18.
Introduction to Computer Programming
Functions I Creating a programming with small logical units of code.
Chapter 5 Function Basics
Group Status Project Status.
CS2011 Introduction to Programming I Objects and Classes
Unit 3 Test: Friday.
CS2011 Introduction to Programming I Arrays (II)
CS2011 Introduction to Programming I Loop Statements (II)
CS2011 Introduction to Programming I Methods (II)
CS2011 Introduction to Programming I Arrays (I)
CS2011 Introduction to Programming I Selections (II)
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
Chapter 6 Methods.
Chapter 5 Methods.
CS302 - Data Structures using C++
CS2011 Introduction to Programming I Elementary Programming
CS2011 Introduction to Programming I Selections (I)
Based on slides created by Bjarne Stroustrup & Tony Gaddis
CSE 190p University of Washington Michael Ernst
Functions I Creating a programming with small logical units of code.
Standard Version of Starting Out with C++, 4th Edition
CPS125.
Corresponds with Chapter 5
Methods and Data Passing
Introduction to Methods and Interfaces
Chapter 6: Methods CS1: Java Programming Colorado State University
Presentation transcript:

CS2011 Introduction to Programming I Methods (I) Chengyu Sun California State University, Los Angeles

Example: Range Sum Find the sum of integers from 1 to 10, 20 to 37, and 35 to 49 Without method With method

Define A Method Method Header public static int sum( int begin, int end ) { int sum = 0; for( int i=begin; i <= end; ++i ) sum += i; return sum; } Method Body

Method Header A.K.A. method signature Method name A list of parameters ?? public static int sum ( int begin, int end ) Access modifier (in this course everything is public) Type of the returned value, a.k.a. return type

Parameters Function as variables in the method A.K.A. formal parameters Some methods don't have parameters, e.g. length() in String

Return Value A method may return a value: return <expression>; A return statement immediately terminates the execution of a method A method may return no value, in which case the return type in the method header should be void You can still use an empty return statement to end the method early Example: void splitName(String name)

Common Mistakes Related to Return Value Return value doesn't match return type Not returning value in some branches Try to return more than one value Unreachable code

Call A Method A.K.A. invoke a method For example: int n = sum( 20, 37 ); Values to assigned to the parameters (in order) Actual parameters, a.k.a. arguments Difference between calling a value returning method and a void method

Trace A Call Use Eclipse's debugger to trace the TestMax example

Why Use Methods … (a) encapsulate reusable logic Examples Range sum Math.sin(), Math.cos(), Math.random() …

… Why Use Methods … (b) encapsulate certain concept or operation (and it makes code more readable) Example: if( year%4 == 0 && year%100 != 0 || year%400 == 0 ) if( isLeapYear(year) )

… Why Use Methods (c) facilitate top-down programming – break down a big problem into smaller problems Create a method for solving the big problem, by calling methods that solve each smaller problem.

Example: Factorial n! = 1 * 2 * 3 … * (n-1) * n Terminate the method early when n < 0 Parameters and return value Return an error value Don't put too much in a method (also see the Range Sum example) Easier to test Maximize reusability

Example: Longest Palindrome Prefix/Suffix Method naming conventions Start with a lower-case letter Usually starts with a verb Usually starts with is for methods that return a boolean value