Subprograms Functions.

Slides:



Advertisements
Similar presentations
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
Advertisements

Writing Methods. Create the method Methods, like functions, do something They contain the code that performs the job Methods have two parts.
11 Chapter 5 METHODS. 22 INTRODUCTION TO METHODS A method is a named block of statements that performs a specific task. Other languages use the terms.
Computer Programming Lab(4).
Week 2 - Friday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
© 2007 Lawrenceville Press Slide 1 Chapter 7 Top-Down Development  Problem-solving approach  Breaking a task down into smaller subtasks  First level.
ITEC 320 Lecture 16 Packages (1). Review Questions? –HW –Exam Nested records –Benefits –Downsides.
Lesson 7: Improving the User Interface
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
1 Introduction to Java Brief history of Java Sample Java Program Compiling & Executing Reading: => Section 1.1.
Lecture # 5 Methods and Classes. What is a Method 2 A method is a set of code which is referred to by name and can be called (invoked) at any point in.
Chapter 4 Procedural Methods. Learning Java through Alice © Daly and Wrigley Objectives Identify classes, objects, and methods. Identify the difference.
Week 2 - Wednesday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
1 Fencepost loops “How do you build a fence?”. 2 The fencepost problem Problem: Write a class named PrintNumbers that reads in an integer called max and.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
1 Simple Methods Chap. 4 Study Sections 4.1 – 4.4 Writing Reusable Formulas.
Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
Introduction to Methods. Previously discussed There are similarities in make up of that can help you remember the construct of a class a class in the.
1 Building Java Programs Chapter 3: Introduction to Parameters and Objects These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They.
Classes: user-defined types. Organizing method with a class A class is used to organize methods * Methods that compute Mathematical functions * The Scanner.
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.
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.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
Reading input from the console input. Java's console input The console is the terminal window that is running the Java program I.e., that's the terminal.
Introduction to array: why use arrays ?. Motivational example Problem: Write a program that reads in and stores away 5 double numbers After reading in.
Martin T. Press.  Main Method and Class Name  Printing To Screen  Scanner.
Methods.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Class Everything in Java is in a class. The class has a constructor that creates the object. If you do not supply a constructor Java will create a default.
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.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 6_1 GEORGE KOUTSOGIANNAKIS Copyright: FALL 2015 Illinois Institute of Technology- George Koutsogiannakis 1.
JAVA METHODS (FUNCTIONS). Why are they called methods? Java is a strictly object-oriented programming language Methods are functions inside of objects.
Reading Parameters CSCI 201 Principles of Software Development Jeffrey Miller, Ph.D.
Methods Matthew Harrison. Overview ● There are five main aspects of methods... ● 1) Modifiers – public, private ● 2) Method Name ● 3) Parameters ● 4)
User Input ICS2O.
Functions + Overloading + Scope
Chapter 9: Value-Returning Functions
Chapter 2 Clarifications
Chapter 7 User-Defined Methods.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 7 User-Defined Methods.
Exercise 1- I/O Write a Java program to input a value for mile and convert it to kilogram. 1 mile = 1.6 kg. import java.util.Scanner; public class MileToKg.
Logger, Assert and Invariants
Compiling and Running a Java Program
CS116 OBJECT ORIENTED PROGRAMMING II LECTURE 1 Part II
Introduction to Methods in java
TK1114 Computer Programming
Something about Java Introduction to Problem Solving and Programming 1.
Chapter 4 Procedural Methods.
Conditional Loops.
Writing Methods.
Pemrograman Dasar Methods PTIIK - UB.
Introduction to Classes and Methods
Java Language Basics.
Subprograms Intro & Procedures.
Java Programming Function Introduction
More on Classes and Objects
Chapter 7 Procedural Methods.
Introduction to Java Brief history of Java Sample Java Program
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
Subprograms Parameters.
February , 2009 CSE 113 B.
February 2 - 6, 2009 CSE 113 B.
Functions Imran Rashid CTO at ManiWeber Technologies.
Java Programming Function Introduction
Local variables and how to recognize them
Consider the following code:
Presentation transcript:

Subprograms Functions

Objectives Learn about functions: What they are How to define them How to use them

Two Kinds There are two types of subprograms we will make use of: A Procedure (Previous lesson) A Function (This lesson) For each of these we will clearly show: How to define, Where to define How to call it within our programs

Function A function is a mini-program within a program that performs a single task AND returns a result to the programmer, E.g. sqrt, pow How to define a Function General Syntax: (Yellow are customizable, Blue differentiate functions and procedures) private static returnType Identifier(type1 identifier1, type2 identifier2, …) { //code that is executed fully, whenever the //Subprogram is called return value that matches the same type as returnType } Example: private static double CalcFloorArea(double length, double width) { double area; area = length * width; return area; }

Breakdown Needed if you plan to use the subprogram inside of main Tells the Java compiler this is a function that will return a double to the programmer private static double CalcFloorArea(double length, double width) { double area; area = length * width; return area; } The code to be executed EACH time CalcFloorArea is called The name of our subprogram. Naming follows the same rules as variables except we use MixedCase to differentiate between variables and subprograms quickly. This means EACH word starts with a capital This is were parameters are defined, just like regular variables REMEMBER: Not all subprograms will need them, but the ( , ) are ALWAYS necessary Notice area is a local variable defined inside CalcFloorArea. It is not a parameter because it is NOT needed data For the task, it is what is being calculated. area, like length and width will be deleted when the function completes

Time to Stamp Using a function is the same as a procedure, except for one major difference. It is our job as programmers to store the result of the function in a variable matching the return type of the function Therefore we must use a variable The next two slides will demonstrate this: The first slide will use literal data The second slide will ask the user for data and pass that data into the subprogram

Example: Literal values public class MyProgram { public static void main(String[] args) { double floorArea = 0; floorArea = CalcFloorArea(2.5,2); System.out.println(“Floor Area: “ + floorArea); } private static double CalcFloorArea(double length, double width) { double area; area = length * width; return area; } }

Example: User values import java.util.Scanner; public class MyProgram { static Scanner input = new Scanner(System.in); public static void main(String[] args) { double roomLength = 0; double roomWidth = 0; System.out.print (“Enter Length: “); roomLength = Double.parseDouble(input.nextLine()); System.out.print (“Enter Width: “); roomWidth = Double.parseDouble(input.nextLine()); double floorArea = 0; floorArea = CalcFloorArea(roomLength, roomWidth); System.out.println(“Floor Area: “ + floorArea); } private static double CalcFloorArea(double length, double width) { double area; area = length * width; return area; } }