Download presentation
Presentation is loading. Please wait.
1
Subprograms Functions
2
Objectives Learn about functions: What they are How to define them
How to use them
3
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
4
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; }
5
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
6
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
7
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; } }
8
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; } }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.