© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 10: Chapter 6: Slide 1 Unit 10 Sub Procedures and Functions Chapter 6 Sub.

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.
Sub and Function Procedures
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Starting Out with Java: From Control Structures through Objects Fourth.
Chapter 7: Sub and Function Procedures
1.
Chapter 4 General Procedures
VBA Modules, Functions, Variables, and Constants
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
Apply Sub Procedures/Methods and User Defined Functions
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.
1 Visual Basic for Applications (VBA) for Excel Prof. Yitzchak Rosenthal.
CHAPTER SIX Reducing Program Complexity General Sub Procedures and Developer-defined Functions.
Why to Create a Procedure
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC BUILDING BLOCKS Bilal Munir Mughal 1 Chapter-5.
CS0004: Introduction to Programming Subprocedures and Modular Design.
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
Chapter 9: Writing Procedures Visual Basic.NET Programming: From Problem Analysis to Program Design.
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 8: Chapter 5: Slide 1 Unit 8 List Boxes and the Do While Looping Structure.
IMS 3253: Subroutines 1 Dr. Lawrence West, MIS Dept., University of Central Florida Topics Procedures Subroutines Parameters –By Value.
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.
6c – Function Procedures Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming.
Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony Gaddis Chapter 5: Methods.
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
CHAPTER SIX Reducing Program Complexity General Sub Procedures and Developer-defined Functions.
Created by Alia Al-Abdulkarim 2008 Visual Basic Vs. Java.
Week Procedures And Functions 7 A procedure is a collection of statements that performs a task.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-1 Why Write Methods? Methods are commonly used to break a problem down.
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.
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.
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.
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
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,
Visual Basic Declaring Variables Dim x as Integer = 0 In the statement above, x is being declared as an Integer (whole number) and is initialised.
Sub Procedures and Functions Visual Basic. Sub Procedures Slide 2 of 26 Topic & Structure of the lesson Introduction to Modular Design Concepts Write.
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
Royal University of Phnom Penh
Functions Chapter 6-Part 2.
by Tony Gaddis and Godfrey Muganda
Method.
Chapter Topics Chapter 5 discusses the following main topics:
Visual Basic..
Chapter 6 Sub Procedures
5.2 Sub Procedures, Part I Defining and Calling Sub Procedures
Starting Out with Java: From Control Structures through Objects
Procedures and Functions
Chapter 5: Methods Starting Out with Java: From Control Structures through Objects Third Edition by Tony Gaddis.
Chapter 6 – Methods Topics are:
Starting Out with Java: From Control Structures through Objects
The structure of programming
STARTING OUT WITH Visual Basic 2008
Chapter 5: Methods Starting Out with Java: From Control Structures through Objects Third Edition by Tony Gaddis.
Chapter 5: Methods Starting Out with Java: From Control Structures through Objects Third Edition by Tony Gaddis.
Presentation transcript:

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 10: Chapter 6: Slide 1 Unit 10 Sub Procedures and Functions Chapter 6 Sub Procedures and Functions

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 10: Chapter 6: Slide 2 Introduction In Visual Basic.NET There Are Two Broad Categories of Procedures: Sub Procedures and Function Procedures

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 10: Chapter 6: Slide 3 Chapter 6 Topics A Sub Procedure is a collection of statements that performs a task  The word Sub is an abbreviation for the older term “subroutine”  Event procedures belong to this category A Function procedure is a collection of statements that performs a task and then returns a value to the part of the program that executed it  Function procedures work like intrinsic functions, such as Val and IsNumeric

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 10: Chapter 6: Slide 4 Sub Procedures You Can Write Your Own General Purpose Sub Procedures That Perform Specific Tasks General Purpose Sub Procedures Are Not Triggered by Events but Called From Statements in Other Procedures

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 10: Chapter 6: Slide 5 Sub Procedure Uses As Event handling code As General code that is executed by statements in other procedures

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 10: Chapter 6: Slide 6 Uses for Sub Procedures To Modularize one's program  Break it into small, manageable hunks To Simplify one's program  Create individual sub procedures to perform tasks that are done repeatedly

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 10: Chapter 6: Slide 7 Sample Sub 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

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 10: Chapter 6: Slide 8 Declaring a Sub Procedure AccessSpecifier gives accessibility to the program ParameterList is a list of variable values that are being passed to the sub procedure 'Sub' and 'End' are keywords [AccessSpecifier] Sub ProcedureName ([ParameterList]) [Statement(s)] End Sub

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 10: Chapter 6: Slide 9 Local Variables Within the Sub Procedure, declare the variable names that are needed  These work as they do in other procedures  They are only accessible from inside the sub procedure in which they are declared  (Their values are not saved from one call to the sub procedure to the next)

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 10: Chapter 6: Slide 10 Static Local Variables Declared with These local variables do maintain their values from one call to the sub procedure to the next Static VariableName As DataType

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 10: Chapter 6: Slide 11 Passing Values to a Sub Procedure When a Procedure Is Called, Values May Be Passed to It

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 10: Chapter 6: Slide 12 Passing Parameters By Value, Example The value, 5, is copied into the storage location of 'number' in DisplayValue Then DisplayValue executes DisplayValue(5) Sub DisplayValue(ByVal number As Integer) ' This procedure displays a value in a message box. MessageBox.Show(number.ToString) End Sub

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 10: Chapter 6: Slide 13 Passing Multiple Parameters Value of first argument is copied to first Second to second, etc. ShowSum(5, 10) Sub ShowSum(ByVal num1 As Integer, ByVal num2 As Integer) ' This procedure accepts two arguments, and prints ' their sum on the form. Dim sum As Integer sum = num1 + num2 MessageBox.Show("The sum is " & sum.ToString) End Sub

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 10: Chapter 6: Slide 14 Passing Arguments by Reference Keyword 'ByVal' is used to indicate passing a parameter by value (value is copied) Keyword 'ByRef' indicates passing a parameter in another way:  Use of the corresponding variable in the sub procedure refers to the corresponding storage location in the calling program  There is no corresponding storage location in the sub procedure (to receive a copy)

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 10: Chapter 6: Slide 15 Function Procedures A Function Procedure Returns a Value to the Part of the Program That Called the Function Procedure

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 10: Chapter 6: Slide 16 Declaring a Function Procedure New keyword 'Function' Plus 'As DataType' qualifier to state the data type of the returned value Return value is specified in a Return expression [AccessSpecifier] Function FunctionName ([ParameterList]) _ As DataType [Statements] End Function

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 10: Chapter 6: Slide 17 Arguments Passed and Value Returned total = Sum(value1, value2) Function Sum(ByVal num1 As Single, ByVal num2 As Single) _ As Single Dim result As Single result = num1 + num2 Return result End Function

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 10: Chapter 6: Slide 18 Returning Nonnumeric Values Function IsValid(num As Integer) As Boolean Dim status As Boolean If num >= 0 And num <= 100 Then status = True Else status = False End If Return status End Function Function FullName(ByVal first As String, ByVal last As String)_ As String Dim name As String name = last & ", " & first Return name End Function