Download presentation
Presentation is loading. Please wait.
1
Subprograms Intro & Procedures
2
Objectives Learn about subprograms: What they are How to define them
How to use them
3
Introduction Imagine it was graduation time and the school was getting ready for the ceremony. One aspect of this is prepping the diplomas Each diploma, say 400 of them, require the principal’s signature. This can be both time consuming and painful for the principal. It would be nice if there were a way to quicken this process… There is, it is called a notarized stamp. A legal rubber stamp copy of a signature that can be used in place of the real thing.
4
Introduction All the principal needs to do is sign their name and send it off to the company who creates the physical stamp. Now with a little ink, the principal can quickly and easily stamp all the diplomas in a fraction of the time This is thanks to a little extra upfront effort that reduced the work load later. Our goal to today is to make the programming equivalent of the stamp in our programs
5
Subprogram or Method A Subprogram, sometimes referred to as a Method in Java, is a program within a program that performs a SINGLE task and can be REUSED to perform that same single task as many times as desired. Up until now, we have called these code blocks E.g. println, pow, nextLine, etc. are all examples of subprograms that someone else created for our use in our programs. Note that we use these subprograms potentially many times in a single program.
6
Setup Much like the principal’s stamp, before he could make use of it he first had to create it. Then, as long as he had ink (similar to a parameter) the stamp could be used forever. This is the same for subprograms that WE create. We must first define the subprogram, much like we do a variable, before we can refer to it by name.
7
Two Kinds There are two types of subprograms we will make use of:
A Procedure (This lesson) A Function (Next lesson) For each of these we will clearly show: How to define, Where to define How to call it within our programs
8
Procedure A procedure is a mini-program within a program that performs a single task, e.g. println How to define a Procedure General Syntax: (Yellow are the parts you add/change) private static void Identifier(type1 identifier1, type2 identifier2, …) { //code that is executed fully, whenever the //Subprogram is called } Example: private static void OutputUserInfo(String name, int age) { System.out.println(“Name: “ + name); System.out.println(“Age: “ + age); }
9
Breakdown private static void OutputUserInfo(String name, int age) {
Needed if you plan to use the subprogram inside of main Tells the Java compiler this is a procedure specifically private static void OutputUserInfo(String name, int age) { System.out.println(“Name: “ + name); System.out.println(“Age: “ + age); } The code to be executed EACH time OutputUserInfo 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
10
Where to do define code? We define our subprograms outside and under the main subprogram public class MyProgram { public static void main(String[] args) { } private static void OutputUserInfo(String name, int age) { System.out.println("Name: " + name); System.out.println("Age: " + age); } } This program defines but never uses the OutputUserInfo procedure
11
Time to Stamp Now that the subprogram is defined, we can freely use it as much as we please. We use a subprogram in the same way we use a variable, we simply refer to it by name inside of a code block, either main or another subprogram we create The term we use when we want to use a subprogram is call. We call a subprogram. If the subprogram has parameters, we must satisfy the contract and give all needed data and in the correct order. 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
12
Example: Literal values
public class MyProgram { public static void main(String[] args) { OutputUserInfo(“Bob”, 17); OutputUserInfo(“Judy”, 23); } private static void OutputUserInfo(String name, int age) { System.out.println("Name: " + name); System.out.println("Age: " + age); } }
13
Example: User values Displays: Name: Fred Age: 21
import java.util.Scanner; public class MyProgram { static Scanner input = new Scanner(System.in); public static void main(String[] args) { String userName = “Fred”; int userAge = 21; OutputUserInfo(userName, userAge); System.out.print (“Enter Name: “); userName = input.nextLine(); System.out.print (“Enter Age: “); userAge = Integer.parseInt(input.nextLine()); OutputUserInfo(userName, userAge); } private static void OutputUserInfo(String name, int age) { System.out.println("Name: " + name); System.out.println("Age: " + age); } } Displays: Name: Fred Age: 21 Assuming the user types John and 82 will Display: Name: John Age: 82 “Fred” copied to name variable 21 copied to age REMEMBER: The VALUE stored in userName, userAge variables is COPIED into the name and age variables which are NEWLY defined each time OutputUserInfo is called, because they are deleted after the previous call
14
Why Do we Use Subprograms
We use subprograms for 2 reasons: Reusability: Imagine you were creating a program that calculated each individual student’s average. If you did not use subprograms you would need to copy and paste the 5 or 6 lines needed once for each of the 1400 students in the school. With subprograms you would only need one line per student to call an CalculateStudentAverage subprogram you would create. Readability: Much like the use of constants and variables in the place or magic numbers, we use subprograms to clean up complicated code. Even code that is only done once in a program can benefit from subprograms. Imagine having code that handles collision detection in a game you create. That code can get quite complicated, but by putting it into a subprogram you could replace the complicated code with one simple line that calls the code in the logic of your program making it much cleaner to read and understand.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.