Copyright © 2014 Pearson Education, Inc. Chapter 6 Procedures and Functions.

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
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Starting Out with Java: From Control Structures through Objects Fourth.
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
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
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
Apply Sub Procedures/Methods and User Defined Functions
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
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 SIX Reducing Program Complexity General Sub Procedures and Developer-defined Functions.
Why to Create a Procedure
5-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Enhancing the Wage Calculator Application Introducing Function Procedures and.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
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.
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.
© 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.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming Third Edition.
Variables & Function Calls. Overview u Variables  Programmer Defined & Intrinsic  Data Types  Calculation issues u Using Functions  The val() function.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions Outline 5.1Introduction 5.2Program Modules.
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.
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.
HNDIT Rapid Application Development
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
BACS 287 Programming Fundamentals 5. BACS 287 Programming Fundamentals This lecture introduces the following topics: – Procedures Built-in Functions User-defined.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming Third Edition.
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.
National Diploma Unit 4 Introduction to Software Development 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,
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 I Programming
Subprograms Functions Procedures.
Royal University of Phnom Penh
Functions Chapter 6-Part 2.
Method.
Chapter Topics Chapter 5 discusses the following main topics:
Chapter 6 Sub Procedures
STARTING OUT WITH Visual Basic 2008
Starting Out with Java: From Control Structures through Objects
Procedures and Functions
CIS16 Application Development and Programming using Visual Basic.net
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
Methods.
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:

Copyright © 2014 Pearson Education, Inc. Chapter 6 Procedures and Functions

Copyright © 2014 Pearson Education, Inc. Topics 6.1 Procedures 6.2 Passing Arguments to Procedures 6.3 Functions

Copyright © 2014 Pearson Education, Inc. 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 part of the program that executed it –You have already worked with Visual Basic’s built-in functions, such as CInt and IsNumeric A method can be either a procedure or a function

Copyright © 2014 Pearson Education, Inc. Procedures 6.1

Copyright © 2014 Pearson Education, Inc. 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 Tutorial 6-1 examines an application with a procedure

Copyright © 2014 Pearson Education, Inc. Declaring a Procedure The general format of a procedure declaration is as follows: AccessSpecifier AccessSpecifier is optional and establishes accessibility to the program Sub and End are keywords ProcedureName ProcedureName used to refer to procedure –Use Pascal casing to capitalize 1st character of the name and each new word in the name ParameterList ParameterList is a list of variables or values being passed to the sub procedure –A parameter is a special variable that receives a value being passed into a procedure [AccessSpecifier] Sub ProcedureName ([ParameterList]) [Statements] [Statements] End Sub

Copyright © 2014 Pearson Education, Inc. Passing Arguments to Procedures 6.2

Copyright © 2014 Pearson Education, Inc. Arguments An argument is value passed to a procedure For example: –Calls the CInt function –Passes txtInput.Text as an argument Two ways to pass arguments: –by value Temporary copy of the original argument –by reference The original argument and can be changed CInt(txtInput.Text)

Copyright © 2014 Pearson Education, Inc. 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 Tutorial 6-3 demonstrates passing arguments DisplayValue(5) DisplayValue(5) ' Call DisplayValue procedure Sub DisplayValue(ByVal intNumber As Integer) ' This procedure displays a value in a message box. MessageBox.Show(intNumber.ToString) End Sub

Copyright © 2014 Pearson Education, Inc. Passing Multiple Arguments Multiple arguments separated by commas Value of first argument is copied to first Second to second, etc. ShowSum(intValue1, intValue2) ' Call ShowSum procedure Sub ShowSum(ByVal intNum1 As Integer, ByVal intNum2 As Integer) Dim intSum As Integer 'Local variable to hold a sum 'Get the sum of the two arguments. intSum = intNum1 + intNum2 'Display the sum. MessageBox.Show("The sum is " & intSum.ToString()) End Sub

Copyright © 2014 Pearson Education, Inc. More about Passing Arguments by Reference 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

Copyright © 2014 Pearson Education, Inc. Functions 6.3

Copyright © 2014 Pearson Education, Inc. 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] [Statements] End Function

Copyright © 2014 Pearson Education, Inc. Function Call Example The Sum function –Passes the variables dblValue1 and dblValue2 as arguments –Data types must agree with parameter list –Assigns the value returned by the Sum function to the variable dblTotal, agrees with return value Tutorial 6-5 demonstrates function use dblTotal = Sum(dblValue1, dblValue2) Function Sum(ByVal dblNum1 As Double, ByVal dblNum2 As Double) Function Sum(ByVal dblNum1 As Double, ByVal dblNum2 As Double) As Double As Double Return dblNum1 + dblNum2 Return dblNum1 + dblNum2 End Function End Function

Copyright © 2014 Pearson Education, Inc. Returning Nonnumeric Values Functions can return nonnumeric values, such as strings and Boolean values strCustomerFullName("John", "Martin") strCustomer = FullName("John", "Martin") FullName(ByVal strFirst As String, Function FullName(ByVal strFirst As String, ByVal strLast As String) As String ByVal strLast As String) As String ' Local variable to hold the full name Dim strName As String ' Append the last name to the first ' name and assign the result to strName. strName = strFirst & " " & strLast ' Return the full name. Return strName End Function

Copyright © 2014 Pearson Education, Inc. The Form and Controls Study this well