Chapter 4 General Procedures

Slides:



Advertisements
Similar presentations
Modular Design Using Subroutines (functions later)
Advertisements

Chapter 6, Slide 1Starting Out with Visual Basic 3 rd Edition Chapter 6 Sub Procedures And Functions.
Sub and Function Procedures
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Eight Sub and Function Procedures.
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,
IS 1181 IS 118 Introduction to Development Tools VB Chapter 06.
VBA Modules, Functions, Variables, and Constants
Structured Programming structured programming: A technique for organizing and coding computer programs in which a hierarchy of modules is used, each having.
Chapter 4 - Visual Basic Schneider
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 1 Principles of Programming and Software Engineering.
Chapter 41 Sub Procedures, Part II (Continue). Chapter 42 Local Variable A variable declared inside a Sub procedure with a Dim statement Space reserved.
Chapter 41 Function Procedures (Continue I). Chapter 42 Lab Sheet 4.7: Code Private Sub btnCalculate_Click(...) _ Handles btnCalculate.Click Dim a, b.
CS241 PASCAL I - Control Structures1 PASCAL I - Control Structures Philip Fees CS241.
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
Chapter 41 General Procedures Sub Procedures, Part I Sub Procedures, Part II Function Procedures.
Chapter 4 Sec. 4.1, 4.2, 4.4 Procedures (User-defined)
Chapter 6: Functions.
Apply Sub Procedures/Methods and User Defined Functions
1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice.
1 INF110 Visual Basic Programming AUBG Fall semester 2009 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice Hall,
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 SIX Reducing Program Complexity General Sub Procedures and Developer-defined Functions.
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.
CHAPTER SIX Reducing Program Complexity General Sub Procedures and Developer-defined Functions.
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.
T ODAY ’ S Q UOTE “Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are–by.
1 Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops 6.3 List Boxes and Loops.
1 Chapter 3 – Variables, Input, and Output 3.1 Numbers 3.2 Strings 3.3 Input and Output.
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.
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.
Chapter 6 Sub Procedures
Chapter 9: Writing Procedures Visual Basic.NET Programming: From Problem Analysis to Program Design.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 6 Using Methods.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
Sub Procedures. A Sub procedure is a block of code that is executed in response to an event. There are two types of Sub procedures, 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.
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC PROGRAMMING FUNDAMENTALS Bilal Munir Mughal 1 Chapter-8.
Practical Programming COMP153-08S Week 5 Lecture 1: Screen Design Subroutines and Functions.
1 5.4 Modular Design Top-Down Design Structured Programming Advantages of Structured Programming.
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
Top-down approach / Stepwise Refinement & Procedures & Functions.
Computing Higher – SD Unit - Topic 8 – Procedure and Standard Algorithms P Lynch, St Andrew’s High School Unit 2 Software Development Process Topic.
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.
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
General Procedures Chapter 4. Different Procedures 4.1 Sub Procedures, Part I 4.2 Sub Procedures, Part II 4.3 Function Procedures 4.4 Modular Design (not.
Starting Out with Visual Basic.NET 2 nd Edition Chapter 6 Sub Procedures And Functions.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
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.
1Lect7 GC20111/2/ General Procedures Function Procedure Sub Procedures, Part I Sub Procedures, Part II Modular Design Lect7 GC20111/2/2015.
Lecture 7 Methods (functions and subroutines) Parameter Passing
CS0004: Introduction to Programming
Sub Procedures And Functions
Chapter#8 General Procedures
Chapter 4 - Visual Basic Schneider
5.2 Sub Procedures, Part I Defining and Calling Sub Procedures
Chapter#8 General Procedures
Chapter 5 - General Procedures
Chapter 5 - General Procedures
Chapter 4 General Procedures
Presentation transcript:

Chapter 4 General Procedures 4.1 Sub Procedures, Part I 4.2 Sub Procedures, Part II 4.3 Function Procedures 4.4 Modular Design Chapter 4 - VB.Net by Schneider

Chapter 4 - VB.Net by Schneider 4.1 Sub Procedures, Part I Sub Procedures Calling Other Sub Procedures Chapter 4 - VB.Net by Schneider

Devices for modularity VB.NET has two devices for breaking problems into smaller pieces: Sub procedures Function procedures Chapter 4 - VB.Net by Schneider

Chapter 4 - VB.Net by Schneider Sub Procedures Performs one or more related tasks General syntax Sub ProcedureName() statements End Sub Chapter 4 - VB.Net by Schneider

Calling a Sub procedure The statement that invokes a Sub procedure is also referred to as a call statement A call statement looks like this: ProcedureName() Chapter 4 - VB.Net by Schneider

Chapter 4 - VB.Net by Schneider Naming Sub procedures The rules for naming Sub procedures are the same as the rules for naming variables. Chapter 4 - VB.Net by Schneider

Chapter 4 - VB.Net by Schneider Example lstResult.Items.Clear() ExplainPurpose() lstResult.Items.Add("") Sub ExplainPurpose() lstResult.Items.Add("This program displays a sentence") lstResult.Items.Add("identifying two numbers and their sum.") End Sub Chapter 4 - VB.Net by Schneider

Chapter 4 - VB.Net by Schneider Passing You can send items to a Sub procedure Sum(2, 3) Sub Sum(num1 As Double, num2 As Double) In the Sum Sub procedure, 2 will be stored in num1 and 3 will be stored in num2 Chapter 4 - VB.Net by Schneider

Parameters and Arguments CalculateDensity("Alaska", 627000, 591000) Arguments – what you send to a Sub procedure Sub CalculateDensity(ByVal state As String, _ ByVal pop As Double, _ ByVal area As Double) Parameters – place holders for what the sub procedure receives Chapter 4 - VB.Net by Schneider

Chapter 4 - VB.Net by Schneider Figure 4.2 Chapter 4 - VB.Net by Schneider

Sub Procedures Calling Other Sub Procedures Private Sub btnDisplay_Click(...) Handles btnDisplay.Click FirstPart() End Sub Sub FirstPart() SecondPart() Sub SecondPart() lstOutput.Items.Add(2) Chapter 4 - VB.Net by Schneider

Chapter 4 - VB.Net by Schneider 4.2 Sub Procedures, Part II Passing by Value Passing by Reference Local Variables Class-Level Variables Debugging Chapter 4 - VB.Net by Schneider

Chapter 4 - VB.Net by Schneider Passing by Value ByVal stands for “By Value” ByVal parameters retain their original value after Sub procedure terminates Chapter 4 - VB.Net by Schneider

Chapter 4 - VB.Net by Schneider Passing by Reference ByRef stands for "By Reference" ByRef parameters can be changed by the Sub procedure and retain the new value after the Sub procedure terminates Chapter 4 - VB.Net by Schneider

Chapter 4 - VB.Net by Schneider Local Variables Variables declared inside a Sub procedure with a Dim statement Space reserved in memory for that variable until the End Sub – then the variable ceases to exist Chapter 4 - VB.Net by Schneider

Class-Level Variables Visible to every procedure in a form’s code without being passed Dim statements for Class-Level variables are placed Outside all procedures At the top of the program region Chapter 4 - VB.Net by Schneider

Chapter 4 - VB.Net by Schneider Scope Class-level variables have class-level scope and are available to all procedures in the class Variables declared inside a procedure have local scope and are only available to the procedure in which they are declared Chapter 4 - VB.Net by Schneider

Chapter 4 - VB.Net by Schneider Debugging Programs with Sub procedures are easier to debug Each Sub procedure can be checked individually before being placed into the program Chapter 4 - VB.Net by Schneider

Chapter 4 - VB.Net by Schneider 4.3 Function Procedures User-Defined Functions Having Several Parameters Comparing Function Procedures with Sub Procedures Collapsing a Procedure with a Region Directive Chapter 4 - VB.Net by Schneider

User-Defined Functions Functions always return one value Syntax: Function FunctionName(ByVal var1 As Type1, _ ByVal var2 As Type2, _ …) As dataType statement(s) Return expression End Function Chapter 4 - VB.Net by Schneider

Some Built-In Functions Example Input Output Int Int(2.6) is 2 number Chr Chr(65) is “A” string Asc Asc(“Apple”) is 65 FormatNumber FormatNumber(12345.628, 1) is 12,345.6 number, number Chapter 4 - VB.Net by Schneider

Chapter 4 - VB.Net by Schneider Sample Private Sub btnDetermine_Click(...) Handles btnDetermine.Click Dim name As String name = txtFullName.Text txtFirstname.Text = FirstName(name) End Sub Function FirstName(ByVal name As String) As String Dim firstSpace As Integer firstSpace = name.IndexOf(" ") Return name.Substring(0, firstSpace) End Function Function call The "As String" at the end of the line which begins "Function First Name" is the data type of the value being returned from this function Return statement Chapter 4 - VB.Net by Schneider

Having Several Parameters Private Sub btnCalculate_Click(...) Handles btnCalculate.Click Dim a, b As Double a = CDbl(txtSideOne.Text) b = CDbl(txtSideTwo.Text) txtHyp.Text = CStr(Hypotenuse(a, b)) End Sub Function Hypotenuse(ByVal a As Double, _ ByVal b As Double) As Double Return Math.Sqrt(a ^ 2 + b ^ 2) End Function ByVal is used in function parameters so the function will not inadvertently return more than one value Value in return statement is sent back to the place where the function was called – the function call "becomes" that value – which must then be stored, or printed, or used in some way by the calling routine. There is a one to one relationship between the arguments being passed to the function and the parameters that the function receives Chapter 4 - VB.Net by Schneider

User-Defined Functions Having No Parameters Private Sub btnDisplay_Click(...) _ Handles btnDisplay.Click txtBox.Text = Saying() End Sub Function Saying() As String Return InputBox("What is your" _ & " favorite saying?") End Function Chapter 4 - VB.Net by Schneider

Comparing Function Procedures with Sub Procedures Subs are accessed using a call statement Functions are called where you would expect to find a literal or expression For example: Result = functionCall lstBox.Items.Add (functionCall) Chapter 4 - VB.Net by Schneider

Functions vs. Procedures Both can perform similar tasks Both can call other subs and functions Use a function when you want to return one and only one value Chapter 4 - VB.Net by Schneider

Collapsing a Procedure with a Region Directive A procedure can be collapsed behind a captioned rectangle This task is carried out with a Region directive. To specify a region, precede the code to be collapsed with a line of the form #Region "Text to be displayed in the box." and follow the code with the line #End Region A procedure, or for that matter any block of code, can be collapsed behind a captioned rectangle just like the Windows Form Designer-generated code. This task is carried out with a so-called Region directive. To specify a region, precede the code to be collapsed with a line of the form #Region "Text to be displayed in the box." and follow the code with the line #End Region A tiny box holding a minus sign will appear to the left of the #Region line. To collapse the code, click on the minus sign. The code will be hidden behind a rectangle captioned Chapter 4 - VB.Net by Schneider

Chapter 4 - VB.Net by Schneider Region Directives Chapter 4 - VB.Net by Schneider

Chapter 4 - VB.Net by Schneider Collapsed Regions Chapter 4 - VB.Net by Schneider

Chapter 4 - VB.Net by Schneider 4.4 Modular Design Top-Down Design Structured Programming Advantages of Structured Programming Chapter 4 - VB.Net by Schneider

Chapter 4 - VB.Net by Schneider Design Terminology Large programs can be broken down into smaller problems "divide-and-conquer" approach called "stepwise refinement" Stepwise refinement is part of top-down design methodology Chapter 4 - VB.Net by Schneider

Chapter 4 - VB.Net by Schneider Top-Down Design General problems are at the top of the design Specific tasks are near the end of the design Top-down design and structured programming are techniques to enhance programmers' productivity Chapter 4 - VB.Net by Schneider

Top-Down Design Criteria The design should be easily readable and emphasize small module size. Modules proceed from general to specific as you read down the chart. The modules, as much as possible, should be single minded. That is, they should only perform a single well-defined task. Modules should be as independent of each other as possible, and any relationships among modules should be specified. Chapter 4 - VB.Net by Schneider

Top-Level Design HIPO Chart Chapter 4 - VB.Net by Schneider

Chapter 4 - VB.Net by Schneider Detailed HIPO Chart Chapter 4 - VB.Net by Schneider

Structured Programming Control structures in structured programming: Sequences: Statements are executed one after another. Decisions: One of two blocks of program code is executed based on a test for some condition. Loops (iteration): One or more statements are executed repeatedly as long as a specified condition is true. Chapter 4 - VB.Net by Schneider

Advantages of Structured Programming Goal to create correct programs that are easier to write understand modify "GOTO –less" programming Chapter 4 - VB.Net by Schneider

Comparison of Flow Charts Chapter 4 - VB.Net by Schneider

Chapter 4 - VB.Net by Schneider Easy to Write Allows programmer to first focus on the big picture and take care of the details later Several programmers can work on the same program at the same time Code that can be used in many programs is said to be reusable Chapter 4 - VB.Net by Schneider

Chapter 4 - VB.Net by Schneider Easy to Debug Procedures can be checked individually A driver program can be set up to test modules individually before the complete program is ready Using a driver program to test modules (or stubs) is known as stub testing Chapter 4 - VB.Net by Schneider

Chapter 4 - VB.Net by Schneider Easy to Understand Interconnections of the procedures reveal the modular design of the program. The meaningful procedure names, along with relevant comments, identify the tasks performed by the modules. The meaningful variable names help the programmer to recall the purpose of each variable. Chapter 4 - VB.Net by Schneider

Chapter 4 - VB.Net by Schneider Easy to Change Because a structured program is self-documenting, it can easily be deciphered by another programmer. Chapter 4 - VB.Net by Schneider

Object-Oriented Programming an encapsulation of data and code that operates on the data objects have properties, respond to methods, and raise events. Chapter 4 - VB.Net by Schneider