Week Procedures And Functions 7 A procedure is a collection of statements that performs a task.

Slides:



Advertisements
Similar presentations
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 6- 1 STARTING OUT WITH Visual Basic 2008 FOURTH EDITION Tony Gaddis.
Advertisements

Chapter 6, Slide 1Starting Out with Visual Basic 3 rd Edition Chapter 6 Sub Procedures And Functions.
Addison Wesley is an imprint of © 2011 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 6 Procedures and Functions.
Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and.
Sub and Function Procedures
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Eight Sub and Function Procedures.
1.
Chapter 5 Menus, Common Dialog Boxes, Sub Procedures, and Function Procedures Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved.
VBA Modules, Functions, Variables, and Constants
Example 2.
Multiple Forms & Procedures. Form Methods: –Show, Hide, Activate, Close Events: –Load, Activated, Closing, Closed.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 6 Modularizing Your Code with Methods.
Chapter 41 General Procedures Sub Procedures, Part I Sub Procedures, Part II Function Procedures.
Procedures and Functions
5.05 Apply Looping Structures
Apply Sub Procedures/Methods and User Defined Functions
Visual Basic Fundamental Concepts. Integrated Development Enviroment Generates startup form for new project on which to place controls. Features toolbox.
CS0004: Introduction to Programming Variables – Numbers.
Chapter 6 Procedures and Functions Instructor: Bindra Shrestha University of Houston – Clear Lake CSCI
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.
Why to Create a Procedure
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. Chapter 5 Menus, Common Dialog Boxes, Sub Procedures, and Function Procedures.
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC BUILDING BLOCKS Bilal Munir Mughal 1 Chapter-5.
CS0004: Introduction to Programming Subprocedures and Modular Design.
VB Procedures. Procedures. Sub procedure: Private/Public Sub SubName(Arguments) … End Sub Private: Can only be accessed by procedures in the same form.
Arrays and 2D Arrays.  A Variable Array stores a set of variables that each have the same name and are all of the same type.  Member/Element – variable.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 7 Sub and Function Procedures.
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
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
IMS 3253: Subroutines 1 Dr. Lawrence West, MIS Dept., University of Central Florida Topics Procedures Subroutines Parameters –By Value.
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 10: Chapter 6: Slide 1 Unit 10 Sub Procedures and Functions Chapter 6 Sub.
Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony Gaddis Chapter 5: Methods.
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC PROGRAMMING FUNDAMENTALS Bilal Munir Mughal 1 Chapter-8.
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
Visual Basic Programming I 56:150 Information System Design.
Debugging, Static Variables, ByRef, ByValue Chapt. 6 in Deitel, Deitel and Nieto.
Created by Alia Al-Abdulkarim 2008 Visual Basic Vs. Java.
Creating Menus Menu Bar – behaves like standard Windows menus Can be used in place of or in addition to buttons to execute a procedure Menu items are controls.
HNDIT Rapid Application Development
BACS 287 Programming Fundamentals 5. BACS 287 Programming Fundamentals This lecture introduces the following topics: – Procedures Built-in Functions User-defined.
Starting Out with Visual Basic.NET 2 nd Edition Chapter 6 Sub Procedures And Functions.
MIC305 Week 6 Beyond controls Review of properties Differences with VB6: using classes and instances Programming constructs.
Copyright © 2014 Pearson Education, Inc. Chapter 6 Procedures and Functions.
COM148X1 Interactive Programming Lecture 8. Topics Today Review.
1 Computer Programming Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
Addison Wesley is an imprint of © 2011 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 3 Variables and Calculations.
National Diploma Unit 4 Introduction to Software Development Procedures and Functions.
Introduction to Methods ISYS 350. Methods Methods can be used to break a complex program into small, manageable pieces – This approach is known as divide.
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,
Lecture 7 Methods (functions and subroutines) Parameter Passing
CS0004: Introduction to Programming
Sub Procedures Chapter 6-Part 1. Chapter 6 Part 12 Event Procedures Code that occurs based upon event. Mouse click Got focus Repetitive code that might.
Sub Procedures And Functions
Visual Basic Fundamental Concepts
Programming Right from the Start with Visual Basic .NET 1/e
Subprograms Functions Procedures.
Royal University of Phnom Penh
Functions Chapter 6-Part 2.
Method.
Visual Basic..
Chapter 6 Sub Procedures
Procedures and Functions
CIS16 Application Development and Programming using Visual Basic.net
Tonga Institute of Higher Education
Chapter 6 – Methods Topics are:
Methods.
STARTING OUT WITH Visual Basic 2008
Presentation transcript:

Week Procedures And Functions 7 A procedure is a collection of statements that performs a task

2 Introduction A procedure is a collection of statements that performs a task Event handlers are a type of procedure A function is a collection of statements that performs a task and returns a value to the VB statement that executed it Functions work like intrinsic functions, such as CInt and IsNumeric A method can be either a procedure or a function

Procedures You Can Write Your Own General Purpose Procedures That Perform Specific Tasks General Purpose Procedures Are Not Triggered by Events but Called From Statements in Other Procedures

4 Procedure Uses An event handler is a type of procedure Automatically executed when an event such as a mouse click occurs General purpose procedures are triggered by statements in other procedures, not by events Procedures help simplify & modularize code by: Breaking it into small, manageable pieces Performing a task that is needed repeatedly Dividing a program into a set of logical tasks

5 Sample Procedure Sub DisplayMessage() 'A Sub procedure that displays a message. lstOutput.Items.Add("") lstOutput.Items.Add("Hello from DisplayMessage.") lstOutput.Items.Add("") End Sub Private Sub btnGo_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnGo.Click ' This procedure calls the DisplayMessage procedure. lstOutput.Items.Add("Hello from btnGo_Click procedure.") lstOutput.Items.Add("Calling the DisplayMessage " & _ "procedure.") DisplayMessage() lstOutput.Items.Add("Now I am back ” _ & “in the btnGo_Click procedure.") End Sub Calls DisplayMessage procedure Returns to btnGo_Click

6 Declaring a Procedure AccessSpecifier is optional and establishes accessibility to the program Sub and End are keywords ProcedureName used to refer to procedure Use Pascal casing, capitalize 1 st character of the name and each new word in the name ParameterList is a list of variables or values being passed to the sub procedure [AccessSpecifier] Sub ProcedureName ([ParameterList]) [Statements] End Sub

7 More on the Access Specifier Private allows use only from that form Public allows use from other forms If not specified, default is Public Access specifiers won’t be used for now

8 Procedures and Static Variables Variables needed only in a procedure, should be declared within that procedure Creates a local variable with scope only within the procedure where declared Local variable values are not saved from one procedure call to the next To save value between procedure calls, use Static keyword to create a static local variable Static VariableName As DataType Scope is still only within the procedure But variable exists for lifetime of application

Passing Arguments to a Procedure When calling a procedure, you can pass it values known as arguments

10 Arguments Argument – a value passed to a procedure We’ve already done this with functions Value = CInt(txtInput.Text) Calls the CInt function and passes txtInput.Text as an argument A procedure must be declared with a parameter list in order to accept an argument

11 Passing Arguments By Value intNumber declared as an integer argument Storage location intNumber created by procedure A value, 5 in this case, must be supplied and is copied into the storage location for intNumber The DisplayValue procedure then executes DisplayValue(5)‘calls DisplayValue procedure Sub DisplayValue(ByVal intNumber As Integer) ' This procedure displays a value in a message box. MessageBox.Show(intNumber.ToString) End Sub

12 Passing Multiple Arguments Multiple arguments separated by commas Value of first argument is copied to first Second to second, etc. ShowSum(intValue1, intValue2)‘calls ShowSum procedure Sub ShowSum(ByVal intNum1 As Integer, _ ByVal intNum2 As Integer) ' This procedure accepts two arguments, and prints ' their sum on the form. Dim intSum As Integer intSum = intNum1 + intNum2 MessageBox.Show("The sum is " & intSum.ToString) End Sub

13 Passing Arguments ByVal or ByRef Arguments are usually passed ByVal New storage location created for procedure Storage location gets a copy of the value Any changes in value are made to the copy Calling procedure won’t “see” the changes Arguments can also be passed ByRef Procedure points to (references) argument’s original storage location Any changes are made to the original value Calling procedure “sees” the changes

ByVal or ByRef Argument Example The difference between parameters passed ByVal & ByRef Passed ByVal Calling procedure does not “see” changes made to the value of an argument Passed ByRef Calling procedure “sees” changes made to the value of an argument 14

Functions A Function Returns a Value to the Part of the Program That Called the Function

16 Declaring a Function New keyword Function Also new is As DataType which states the data type of the value to be returned Return value is specified in a Return expression [AccessSpecifier] Function FunctionName ([ParameterList]) _ As DataType [Statements] End Function

17 Function Call Example sngTotal = Sum(sngValue1, sngValue2) Function Sum(ByVal sngNum1 As Single, _ ByVal sngNum2 As Single) As Single Dim sngResult As Single sngResult = sngNum1 + sngNum2 Return sngResult End Function sngValue1 & sngValue2 must be data type Single Data types must agree with parameter list sngTotal must be Single, agrees with return value

18 Returning Nonnumeric Values Function IsValid(intNum As Integer) As Boolean Dim blnStatus As Boolean If intNum >= 0 And intNum <= 100 Then blnStatus = True Else blnStatus = False End If Return blnStatus End Function Function FullName(ByVal strFirst As String, _ ByVal strLast As String) As String Dim strName As String strName = strLast & ", " & strFirst Return strName End Function

More About Debugging Step Into Step Over Step Out

20 Debugging Involving Procedures Step Into - continue to debug by single-stepping through a procedure Step Over - run procedure without single- stepping, continue single-step after the call Step Out - end single-stepping in procedure, continue single-step after the call