Methods/Functions.

Slides:



Advertisements
Similar presentations
CSCI 160 Midterm Review Rasanjalee DM.
Advertisements

Classes  All code in a Java program is part of a class  A class has two purposes  Provide functions to do work for the programmer  Represent data.
Intro Programming By Trevor Decker Team 1743 By Trevor Decker Team 1743.
Static Methods Chapter 4. Chapter Contents Objectives 4.1 Introductory Example: Old MacDonald Had a Farm … 4.2 Getting Started with Methods 4.3 Example:
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
Object Orientated Concepts. Overview  The world is objects  Defining an object  Not a function, it’s a method  Lets inherit want we can  Objects.
COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
Hello, world! Dissect HelloWorld.java Compile it Run it.
Introduction to Methods
Introduction to Programming G51PRG University of Nottingham Revision 2 Essam Eliwa.
Unit 2: Java Introduction to Programming 2.1 Initial Example.
COMP More About Classes Yi Hong May 22, 2015.
Hello AP Computer Science!. What are some of the things that you have used computers for?
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Parameters. Overview A Reminder Why Parameters are Needed How Parameters Work Value Parameters Reference Parameters Out Parameters.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Methods (a.k.a. Functions)
F27SA1 Software Development 1 3. Java Programming 2 Greg Michaelson.
Static Methods. 2 Objectives Look at how to build static (class) methods Study use of methods calling, parameters, returning values Contrast reference.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
RECITATION 4. Classes public class Student { } Data Members public class Student { private String name; public String id; }
Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski.
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.
Classes. Student class We are tasked with creating a class for objects that store data about students. We first want to consider what is needed for the.
Hello Computer Science!. Below is an example of a Hello World program in JAVA. While it is only three lines of code, there are many things that are happening.
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.
CSI 3125, Preliminaries, page 1 Compiling the Program.
CIS 234: Java Methods Dr. Ralph D. Westfall April, 2010.
The assignment expressions. The assignment operator in an assignment statement We have seen the assignment statement: Effect: var = expr; Stores the value.
2.1 Functions. Functions in Mathematics f x y z f (x, y, z) Domain Range.
Building java programs, chapter 3 Parameters, Methods and Objects.
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapters 4 and 5: Excerpts l Class and Method Definitions l Information.
Department of Computer Engineering Methods Computer Programming for International Engineers.
Chapter 5 Methods 1. Motivations Method : groups statements that perform a function.  Level of abstraction (black box)  Code Reuse – no need to reinvent.
Classes - Intermediate
Methods.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
COP 2220 Computer Science I Topics –Breaking Problems Down –Functions –User-defined Functions –Calling Functions –Variable Scope Lecture 4.
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 24, 2009.
ITM 3521 ITM 352 Functions. ITM 3522 Functions  A function is a named block of code (i.e. within {}'s) that performs a specific set of statements  It.
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 26, 2009.
CS0004: Introduction to Programming
Computer Programming Your First Java Program: HelloWorld.java.
Information and Computer Sciences University of Hawaii, Manoa
CSC111 Quick Revision.
Java Course Review.
Building Java Programs
Functions CIS 40 – Introduction to Programming in Python
Building Java Programs
SELECTION STATEMENTS (1)
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Starting Out with Java: From Control Structures through Objects
Lecture 11 C Parameters Richard Gesick.
Pemrograman Dasar Methods PTIIK - UB.
An Introduction to Java – Part I, language basics
Functions Jay Summet.
Week 4 Lecture-2 Chapter 6 (Methods).
Introduction to Object-Oriented Programming
Building Java Programs
Java Looking at our first console application in Eclipse
Generic Programming.
Loops CGS3416 Spring 2019 Lecture 7.
ITM 352 Functions.
Corresponds with Chapter 5
Introduction to Methods and Interfaces
Presentation transcript:

Methods/Functions

What is a method? A named section of code Code reuse: Write once, run it many times In Java all methods are part of classes Static methods can be called without declaring an object of the class first.

Function calling Prompt this code to run by calling the function.

Detailed description about how to use a word I want to sing the chorus Their 'songs' are structured by contrasting textures and energies rather than verses and choruses Use word in separate text Refer to a word without its whole definition Detailed description about how to use a word Function Call Function Definition Use function in separate program. Refer to it by name Detailed code. How to use the function if it returns value public static void main(String[] args) { // TODO Auto-generated method stub chorus(); } private static void chorus() { // TODO Auto-generated method stub }

Parameters and Arguments Parameters: Variables in the parentheses of our function, that we can then use inside our function. Arguments: Specific values passed into our function call (ex. “Tony”) When you call a method, the arguments used must match the declaration's parameters in type and order.

Argument Order printPhoto (10,20,false); printPhoto (20,10,false); Matching the argument with the type of the parameters. Keep in mind that the order and type are very important. When you use multiple parameters and put arguments in a function call.

Return values Function Input output Parameters Return values Defined by return type Not only functions can take the input as parameters, they can also return data to you. This data is called return values of this function. Void means not returning any data Printing does not create output that we can interact with later on in separate programs. Done in internal code. public void functionName() { //internal block of code } In order to return values, changing the void to int, String, Boolean etc.

Demo Calculate the area and perimeter of a rectangle.

Return values To return a value you need: A return type - void, int, String, etc A return statement -- line of code that returns a value Return values let you use values in other function calls or programs !

Demo Methods working with Array