Download presentation
Presentation is loading. Please wait.
Published byArchibald Ronald Harris Modified over 8 years ago
1
Copyright © 2014 Pearson Education, Inc. Chapter 6 Procedures and Functions
2
Copyright © 2014 Pearson Education, Inc. Topics 6.1 Procedures 6.2 Passing Arguments to Procedures 6.3 Functions
3
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
4
Copyright © 2014 Pearson Education, Inc. Procedures 6.1
5
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
6
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
7
Copyright © 2014 Pearson Education, Inc. Passing Arguments to Procedures 6.2
8
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)
9
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
10
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
11
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
12
Copyright © 2014 Pearson Education, Inc. Functions 6.3
13
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
14
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
15
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
16
Copyright © 2014 Pearson Education, Inc. The Form and Controls Study this well
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.