1Lect7 GC20111/2/2015. 2 General Procedures Function Procedure Sub Procedures, Part I Sub Procedures, Part II Modular Design Lect7 GC20111/2/2015.

Slides:



Advertisements
Similar presentations
Sub and Function Procedures
Advertisements

1 5.3 Sub Procedures, Part II Passing by Value Passing by Reference Sub Procedures that Return a Single Value Debugging.
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.
Chapter 4 General Procedures
Chapter 4 - Visual Basic Schneider
Chapter 4 (cont) Sec. 4.1, 4.2, 4.4 Procedures (User-defined)
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 Sub Procedures, Part II (Continue). Chapter 42 Local Variable A variable declared inside a Sub procedure with a Dim statement Space reserved.
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
Chapter 41 General Procedures Sub Procedures, Part I Sub Procedures, Part II Function Procedures.
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.
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.
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.
CSCI 3327 Visual Basic Chapter 6: Methods: A Deeper Look UTPA – Fall 2011.
PROGRAMMING Functions. Objectives Understand the importance of modular programming. Know the role of functions within programming. Use functions within.
Lecture 8 Visual Basic (2).
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.
Sub procedures School of Business Eastern Illinois University © Abdou Illia, Spring 2002 (Week 6, Friday 2/21/03)
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 9: Writing Procedures Visual Basic.NET Programming: From Problem Analysis to Program Design.
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.
6c – Function Procedures Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming.
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
PSU CS 106 Computing Fundamentals II VB Declarations HM 5/4/2008.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Created by Alia Al-Abdulkarim 2008 Visual Basic Vs. Java.
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.
Chapter 5 : Methods Part 2. Returning a Value from a Method  Data can be passed into a method by way of the parameter variables. Data may also be returned.
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.
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.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
CS0004: Introduction to Programming
Sub Procedures And Functions
Subprograms Functions Procedures.
A variable is a name for a value stored in memory.
Functions Chapter 6-Part 2.
Method.
Chapter#8 General Procedures
Chapter 4 - Visual Basic Schneider
Chapter 6 Sub Procedures
5.2 Sub Procedures, Part I Defining and Calling Sub Procedures
6 Chapter Functions.
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 6 – Methods Topics are:
Chapter 5 - General Procedures
Methods.
Fundamental Programming
STARTING OUT WITH Visual Basic 2008
Chapter 4 General Procedures
Standard Version of Starting Out with C++, 4th Edition
Presentation transcript:

1Lect7 GC20111/2/2015

2 General Procedures Function Procedure Sub Procedures, Part I Sub Procedures, Part II Modular Design Lect7 GC20111/2/2015

3 Devices for Modularity Visual Basic has two ways for breaking problems into smaller pieces: Function procedures Sub procedures Lect7 GC20111/2/2015

4 Function Procedures User-Defined Functions Having One Parameter User-Defined Functions Having Several Parameters User-Defined Functions Having No Parameters User-Defined Boolean-Valued Functions Lect7 GC20111/2/2015

5 Some Built-In Functions Function: Int Example: Int(2.6) is 2 Input: number Output: number Function: Math.Round Example: Math.Round(1.23, 1) is 1.2 Input: number, number Output: number Lect7 GC20111/2/2015

6 Some Built-In Functions (continued) Function: FormatPercent Example: FormatPercent(0.12, 2) is 12.00% Input: number, number Output: string Function: FormatNumber Example: FormatNumber(12.62, 1) is 12.6 Input: number, number Output: string Lect7 GC20111/2/2015

7 Function Procedures Function procedures (aka user-defined functions) always return one value Syntax: Function FunctionName(ByVal var1 As Type1, ByVal var2 As Type2,...) As ReturnDataType statement(s) Return expression End Function Lect7 GC20111/2/2015

Example With One Parameter Function FtoC(ByVal t As Double) As Double 'Convert Fahrenheit temp to Celsius Return (5 / 9) * (t - 32) End Function 8 Lect7 GC20111/2/2015

Header of the FtoC Function Procedure 9 Lect7 GC20111/2/2015

10 Example 1: Form txtTempF txtTempC Lect7 GC20111/2/2015

Example 1: Code Using Function Private Sub Convert(...) Handles btnCnvrt.Click Dim fTemp, cTemp As Double fTemp = txtTempF.Text cTemp = FtoC(fTemp) txtTempC.Text = cTemp End Sub Function FtoC(ByVal t As Double) As Double Return (5 / 9) * (t - 32) End Function 11 Lect7 GC20111/2/2015

Example 1: Code Without Using Function 12 Private Sub btnConvert_Click(...) _Handles btnConvert.Click Dim fTemp, cTemp As Double fTemp = txtTempF.Text cTemp = (5 / 9) * (fTemp - 32) txtTempC.Text = cTemp End Sub Lect7 GC20111/2/2015

13 Example 1: Output Lect7 GC20111/2/2015

14 User-Defined Function Having Several Parameters Function Pay(ByVal wage As Double, ByVal hrs As Double) As Double Dim amt As Double ‘ Total amount of salary per hour Select Case hrs Case Is <= 40 amt = wage * hrs Case Is > 40 ‘the wage (salary/hour) increases 50% per every extra hour (extra hours >40) amt = wage * 40 +(0.5 * wage * (hrs – 40)) End Select Return amt End Function Lect7 GC20111/2/2015

15 Example 2: Form txtWage txtHours txtEarnings Lect7 GC20111/2/2015

Example 2: Partial Code Private Sub btnCalculate_Click(...) _ Handles btnCalculate.Click Dim hourlyWage, hoursWorkded As Double hourlyWage = txtWage.Text hoursWorked = txtHours.Text txtEarnings.Text = FormatCurrency(Pay(hourlyWage, hoursWorked)) End Sub 16 Function call Lect7 GC20111/2/2015

17 Example 2: Output Lect7 GC20111/2/2015

18 User-Defined Function Having No Parameters Function CostOfItem() As Double Dim price As Double = CDbl(txtPrice.Text) Dim quantity As Integer = CDbl(txtQuantity.Text) Dim cost = price * quantity Return cost End Function Lect7 GC20111/2/2015

19 User-Defined Boolean-Valued Function Function IsVowelWord(ByVal word As String) As Boolean If word.IndexOf("A") = -1 Then Return False End If. If word.IndexOf("U") = -1 Then Return False End If Return True End Function Lect7 GC20111/2/2015

20 Sub Procedures, Part I Defining and Calling Sub Procedures Variables and Expressions as Arguments Sub Procedures Calling Other Sub Procedures Lect7 GC20111/2/2015

21 General Form of Sub Procedure Lect7 GC20111/2/2015

22 Calling a Sub Procedure The statement that invokes /calls a Sub procedure is referred to as a calling statement. A calling statement looks like this: ProcedureName(arg1, arg2,..., argN) Lect7 GC20111/2/2015

23 Naming Sub Procedures The rules for naming Sub procedures are the same as the rules for naming variables. Lect7 GC20111/2/2015

24 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 Lect7 GC20111/2/2015

25 Arguments and Parameters Sum(2, 3) Sub DisplaySum(ByVal num1 As Double, ByVal num2 _ As Double) arguments parameters displayed automatically Lect7 GC20111/2/2015

26 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. Lect7 GC20111/2/2015

27 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. Lect7 GC20111/2/2015

28 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. Lect7 GC20111/2/2015

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 29 Lect7 GC20111/2/2015

30 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." End Sub Lect7 GC20111/2/2015

Output This program displays the name and population of a state. CA has population 37 million. 31 Lect7 GC20111/2/2015

32 Sub Procedures, Part II Passing by Value Passing by Reference Sub Procedures that Return a Single Value Lifetime and Scope of Variables and Constants Debugging Lect7 GC20111/2/2015

33 ByVal and ByRef Parameters in Sub procedure headers are proceeded by ByVal or ByRef ByVal stands for By Value ByRef stands for By Reference Lect7 GC20111/2/2015

34 Passing by Value When a variable argument is passed to a ByVal parameter, just the value of the argument is passed. After the Sub procedure ends, the variable has its original value. Lect7 GC20111/2/2015

35 Example Public Sub btnOne_Click (...) Handles _btnOne.Click Dim n As Double = 4 Triple(n) txtBox1.Text = “n = “ & n End Sub Sub Triple(ByVal num As Double) num = 3 * num txtBox2.Text = “num = “ & num End Sub Output: num = 12 n = 4 Argument name (n) is different than parameter name (num) 124 num Memory (RAM): n Lect7 GC20111/2/2015

36 Same Example: n num Public Sub btnOne_Click (...) Handles _btnOne.Click Dim num As Double = 4 Triple(num) txtBox1.Text = “2. num = “ & num End Sub Sub Triple(ByVal num As Double) num = 3 * num txtBox2.Text = “1. num = “ & num End Sub Output: 1. num = num = 4 Argument name (num) is same as the parameter name (num) 124 num Memory (RAM): num Lect7 GC20111/2/2015

37 Passing by Reference When a variable argument is passed to a ByRef parameter, the parameter is given the same memory location as the argument. After the Sub procedure terminates, the variable has the value of the parameter. Lect7 GC20111/2/2015

38 Example Public Sub btnOne_Click (...) Handles _ btnOne.Click Dim num As Double = 4 Triple(num) txtBox2.Text = “2. num = “ & num End Sub Sub Triple(ByRef num As Double) num = 3 * num txtBox1.Text = “1. num = “ & num End Sub Output: 1. num = num = 12 Argument name (num) is the same as parameter name (num) 4 12 num Memory (RAM): X Lect7 GC20111/2/2015

39 Example: num n Private Sub btnOne_Click(...) Handles _btnOne_Click Dim n As Double = 4 Triple(n) txtBox1.Text = “n = “ & num End Sub Sub Triple(ByRef num As Double) num = 3 * num txtBox1.Text = “num = “ & num End Sub Output: num = 12 n = 12 Argument name (n) is different than parameter name (num) num Memory (RAM): n 4 12 X Lect7 GC20111/2/2015

Most Common Use of ByRef: Get Input (Read Input) Sub InputData(ByRef wage As Double, ByRef hrs As Double) wage = CDbl(txtWage.Text) hrs = CDbl(txtHours.Text) End Sub 40 Lect7 GC20111/2/2015

41 Lifetime and Scope of a Variable Lifetime: Period during which it remains in memory. Scope: In Sub procedures, defined same as in event procedures. Suppose a variable is declared in procedure A that calls procedure B. While procedure B executes, the variable is alive, but out of scope. Lect7 GC20111/2/2015

42 Functions vs. Sub procedures Both can perform similar tasks Both can call other procedures Use a function when you want to return one and only one value Lect7 GC20111/2/2015