5.2 Sub Procedures, Part I Defining and Calling Sub Procedures

Slides:



Advertisements
Similar presentations
Sub and Function Procedures
Advertisements

Spring Semester 2013 Lecture 5
1 Procedural Programming Paradigm Stacks and Procedures.
1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
1 5.3 Sub Procedures, Part II Passing by Value Passing by Reference Sub Procedures that Return a Single Value Debugging.
1 Chapter 5 - General Procedures 5.1 Function Procedures 5.2 Sub Procedures, Part I 5.3 Sub Procedures, Part II 5.4 Modular Design.
1 CS 106, Winter 2009 Class 10, Section 4 Slides by: Dr. Cynthia A. Brown, Instructor section 4: Dr. Herbert G. Mayer,
1 CS 106, Winter 2009 Class 11, Section 4 Slides by: Dr. Cynthia A. Brown, Instructor section 4: Dr. Herbert G. Mayer,
Chapter 4 General Procedures
1 Memory Model of A Program, Methods Overview l Closer Look at Methods l Memory Model of JVM »Method Area »Heap »Stack l Preview: Parameter Passing.
Chapter 4 - VB.Net by Schneider1 Chapter 4 General Procedures 4.1 Sub Procedures, Part I 4.2 Sub Procedures, Part II 4.3 Function Procedures 4.4 Modular.
Chapter 41 Sub Procedures, Part II Passing by Value Passing by Reference Local Variables Class-Level Variables Debugging.
Chapter 41 Function Procedures (Continue I). Chapter 42 Lab Sheet 4.7: Code Private Sub btnCalculate_Click(...) _ Handles btnCalculate.Click Dim a, b.
Chapter 41 General Procedures Sub Procedures, Part I Sub Procedures, Part II Function Procedures.
Apply Sub Procedures/Methods and User Defined Functions
Subroutines and Functions Chapter 6. Introduction So far, most of the code has been inside a single method for an event –Fine for small programs, but.
Chapter 5 - VB 2008 by Schneider1 Chapter 5 - General Procedures 5.1 Sub Procedures, Part I 5.2 Sub Procedures, Part II 5.3 Function Procedures 5.4 Modular.
Why to Create a Procedure
1 Chapter 5 - General Procedures 5.1 Function Procedures 5.2 Sub Procedures, Part I 5.3 Sub Procedures, Part II 5.4 Modular Design.
CSCI 3327 Visual Basic Chapter 6: Methods: A Deeper Look UTPA – Fall 2011.
CS0004: Introduction to Programming Subprocedures and Modular Design.
1 Chapter 5 - General Procedures 5.1 Function Procedures 5.2 Sub Procedures, Part I 5.3 Sub Procedures, Part II 5.4 Modular Design.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 7 Sub and Function Procedures.
Subroutines and Functions. Introduction So far, most of the code has been inside a single method for an event –Fine for small programs, but inconvenient.
Subprograms CE 311 K - Introduction to Computer Methods Daene C. McKinney.
110-G1 Motivation: Within a program, may have to perform the same computation over and over Many programs share the same computation (e.g. sorting) To.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 5.
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 10: Chapter 6: Slide 1 Unit 10 Sub Procedures and Functions Chapter 6 Sub.
1Edited By Maysoon Al-Duwais. 2 General Procedures Function Procedure Sub Procedures, Part I Sub Procedures, Part II Modular Design Edited By Maysoon.
Week Procedures And Functions 7 A procedure is a collection of statements that performs a task.
6b – Sub Procedure With Parameters Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming.
Sub Procedures; Passing Values Back From Sub Procedures Passing by reference Passing by value.
Classes - Intermediate
Starting Out with Visual Basic.NET 2 nd Edition Chapter 6 Sub Procedures And Functions.
Subroutines and Functions Chapter 6. Introduction So far, all of the code you have written has been inside a single procedure. –Fine for small programs,
1Edited By Maysoon Al-Duwais. 2 Devices for Modularity Visual Basic has two ways for breaking problems into smaller pieces: Sub procedures Function procedures.
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
1Lect7 GC20111/2/ General Procedures Function Procedure Sub Procedures, Part I Sub Procedures, Part II Modular Design Lect7 GC20111/2/2015.
CS0004: Introduction to Programming
Sub Procedures And Functions
Programming Logic and Design Seventh Edition
Procedures and Modular Programming
Functions Chapter 6-Part 2.
Method.
ARRAYS.
Chapter#8 General Procedures
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.
More Object Oriented Programming
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.
Call Stacks, Arguments and Returns
Chapter 4 - Visual Basic Schneider
Lecture 11 C Parameters Richard Gesick.
Defining Your Own Classes
6.12 Default Arguments.
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
6 Chapter Functions.
Classes, Encapsulation, Methods and Constructors (Continued)
Procedures and Functions
Introduction to Visual Programming
Chapter#8 General Procedures
Chapter 7: Using Functions, Subs, and Modules
Chapter 5 - General Procedures
CSCI 3327 Visual Basic Chapter 6: Methods: A Deeper Look
Chapter 5 - General Procedures
Fundamental Programming
Functions Imran Rashid CTO at ManiWeber Technologies.
Standard Version of Starting Out with C++, 4th Edition
Corresponds with Chapter 5
Introduction to Computer Programming IT-104
Presentation transcript:

5.2 Sub Procedures, Part I Defining and Calling Sub Procedures Variables and Expressions as Arguments Sub Procedures Calling Other Sub Procedures

General Form of Sub Procedure Sub procedures have no return type.

Calling a Sub Procedure The statement that invokes a Sub procedure is referred to as a calling statement. A calling statement looks like this: ProcedureName(arg1, arg2,..., argN) When you call a Sub procedure you get no data back. Therefore you can’t use it in an expression.

Passing Values DisplaySum(2, 3) Sub DisplaySum(ByVal num1 As Double, ByVal num2 _ As Double) Dim z As Double z = num1 + num2 lstOutput.Items.Add("The sum of " & num1 & " and " & num2 & " is " & z & ".") End Sub In the Sub procedure, 2 will be stored in num1 and 3 will be stored in num2

Arguments and Parameters Sum(2, 3) Sub DisplaySum(ByVal num1 As Double, ByVal num2 _ As Double) Arguments (actual parameters) Formal parameters You don’ t have to type this. ByVal is the default

Several Calling Statements DisplaySum(2, 3) DisplaySum(4, 6) DisplaySum(7, 8) Output: The sum of 2 and 3 is 5. The sum of 4 and 6 is 10 The sum of 7 and 8 is 15.

Example 5.2.1

Example 5.2.1 Calls to DisplaySum. You can pass literals, variables, or any other expression as arguments, as long as their data types match the formal parameter declarations.

Passing Strings and Numbers Demo("CA", 38) Sub Demo(ByVal state As String, ByVal pop _ As Double) lstOutput.Items.Add = state & " has population " & pop & " million." End Sub Note: The statement Demo(38, "CA") would not be valid. The types of the arguments must be in the same order as the types of the parameters.

Variables and Expressions as Arguments Dim s As String = "CA" Dim p As Double = 19 Demo(s, 2 * p) Sub Demo(ByVal state As String, ByVal pop _ As Double) lstOutput.Items.Add = state & " has population " & pop & " million." End Sub Note: The argument names need not match the parameter names. For instance, s versus state. An argument can be ANY expression that satisfies the type of the formal parameter.

Calls to CalculateDensity. Example 5.2.2 Calls to CalculateDensity.

Example 5.2.2 If you have to do the same task in several parts of the program, it’s good to make a procedure out of it. That way you don’t have to repeat the code in multiple places.

Sub Procedure Having No Parameters Sub DescribeTask() lstBox.Items.Clear() lstBox.Items.Add("This program displays") lstBox.Items.Add("the name and population") lstBox.Items.Add("of a state.") End Sub

Sub Procedure Calling Another Sub Procedure Private Sub btnDisplay_Click(...) Handles _ btnDisplay.Click Demo("CA", 37) End Sub Sub Demo(ByVal state As String, ByVal pop _ As Double) DescribeTask() lstOutput.Items.Add("") lstOutput.Items.Add = state & " has population " & pop & " million."

Output This program displays the name and population of a state. CA has population 37 million.

Example 5.2.3

Example 5.2.3 Modularity – take the big task and break it into subtasks. Here, the two subtasks are implemented in the procedures DescribeTask and CalculateDensity.

Example 5.2.4 This example is modified a bit. Not exactly identical with the textbook.

Calls to Sub procedures. Example 5.2.4 Calls to Sub procedures.

Stack 1 deep.

Stack 2 deep.

You can select another frame from the stack to see the local data of that procedure. The green highlighting indicates the statement where the selected procedure is paused at.

Stack 3 deep.

Stack 2 deep.

Stack 1 deep.

Stack 2 deep.

Stack 1 deep.