Presentation is loading. Please wait.

Presentation is loading. Please wait.

Example 2.

Similar presentations


Presentation on theme: "Example 2."— Presentation transcript:

1 Example 2

2 Example 2

3 Example 3

4 Example 3

5 Sub Procedures versus Function Procedures A Sub Procedure is a procedure that performs an action A Function Procedure may perform an action, but it also returns a value (the return value) to the point in a procedure from which it was called

6 Creating a ‘Function Procedure’
2 ways to create a Function Procedure [1] simply type in the procedure definition in the code window [2] use the tools / add procedure menu command Same way as creating a sub procedures

7 tools / add procedure menu command

8 Creating a ‘Function Procedure’
The Function statement declares the name, arguments, and code that form the body of a function, as well as the data type of the value returned [Private | Public] Function Name [(arguments)] [As Data Type] statements End Function

9 Calling a ‘Function Procedure’
A Function Procedure is almost always used as part of an expression in a statement ReturnedValue = FunctionName( ) Remember the use of system-defined functions stUserName = Inputbox(“Enter User Name”, “User Input”)

10

11

12

13 Passing Arguments

14 Passing ARGUMENTS to Procedures At times there may be a need to use the value of a variable in one procedure and also in a second procedure, that is called from the first The variable could be declared at Module Level, but this makes it visible to all other procedures, and the scope of variables should always be kept as narrow as possible In order to keep the scope of the variable as narrow as possible, consider declaring the variable as Local and passing it (as an argument) to any Called Procedures

15 Passing ARGUMENTS to Procedures
When passing arguments to procedures the ProcedureName has an argument inside the Parenthesis. This specifies that when the procedure is called, an argument must be supplied When a Procedure definition names an argument, any call to that procedure must supply an argument. Also, the argument must be of the same Data Type in both the definition and call locations The Name of the argument does not have to be the same in both locations When multiple arguments are specified in both the Procedure header and the Call of the procedure, the number of arguments, their sequence, and their data types must match in both locations

16 Very Important to Remember
In a procedure, the argument list you enter establishes the number of arguments, their data type, and their sequence. When using multiple arguments, the sequence of the arguments is critical, just as when you see the predefined VB functions Very Important to Remember

17 Sub Procedures and Function Procedures A Comparison The two examples, one using Sub Procedure, and the second using Function Procedures, do exactly the same thing in relation to the outputted results, however, they are coded differently Both programs are checking to see if the user has entered a value greater than the minimum allowable loan. The value entered by the user is displayed in a text box, and whether the loan amount is “Valid” or an “Invalid” request is also printed to the form

18

19

20 General Declarations

21 Scope of Variables: General Sub Procedures
Example 1

22 Scope of Variables: General Sub Procedures
Example 1

23 Scope of Variables: General Sub Procedures
Example 1

24 Scope of Variables: General Sub Procedures and Function Procedures
Example 2 The user clicks on the command button

25 Scope of Variables: General Sub Procedures and Function Procedures
Example 2

26 Scope of Variables: General Sub Procedures and Function Procedures
Example 2 Function Name Argument Data Type of the RETURN VALUE

27 Scope of Variables: General Sub Procedures and Function Procedures
Example 2

28 Scope of Variables: General Sub Procedures and Function Procedures
Example 3

29 Scope of Variables: General Sub Procedures and Function Procedures
Example 3

30 Scope of Variables: General Sub Procedures and Function Procedures
Example 3

31 Scope of Variables: General Sub Procedures and Function Procedures
Example 3

32 Scope of Variables: General Sub Procedures and Function Procedures
Example 3

33 Example 4

34 Change Trivial from a sub procedure to a function procedure as demonstrated below (the code in the black rectangles has been added) Example 4

35 Notice what has happened to the value of X as a result of these changes
Example 4

36 Passing Arguments ByVal and ByRef

37 Passing Arguments ByVal and ByRef
The arglist argument has the following syntax and parts: ([ByVal | ByRef] varname ) Part ByVal Indicates that the argument is passed by value ByRef Indicates that the argument is passed by reference Varname Name of the variable representing the argument

38 Passing Arguments ByVal and ByRef
by value (ByVal) A way of passing the value, rather than the address, of an argument to a procedure This allows the procedure to access a copy of the variable As a result, the variables actual value cannot be changed by the procedure to which it is passed

39 Passing Arguments ByVal and ByRef
by reference (ByRef) A way of passing the address, rather than the value, of an argument to a procedure A pointer to the variable is passed to the procedure This allows the procedure to access the actual variable As a result, the variables actual value can be changed by the procedure to which it is passed

40 Passing Arguments ByVal and ByRef
If you declare an argument without using either the ByVal or ByRef keyword, the argument will be passed by reference (ByRef) by default

41 Passing Arguments ByVal and ByRef
A Single Argument in a SUB Procedure [1] GetNumbers (X) [2] GetNumbers X In situation [1] and [2] there is no need for the optional word CALL In situation [1] the argument X never changes If you add the keyword CALL – it does change! In situation [2] the argument X changes to the new value

42

43

44

45 Call Assess (X)

46 Assess X

47 Passing Arguments ByVal and ByRef
Multiple Argument [1] Call GetNumbers (X, Y) [2] GetNumbers X, Y In situation [1] CALL is required, otherwise it is interpreted as a function In situation [1] and [2] the arguments X and Y change to the new values Need to specify ByVal for unchanged values of X and Y

48 Passing Arguments ByVal and ByRef
Note that when you use the Call keyword, arguments must be enclosed in parentheses. If you omit the Call keyword, you must also omit the parentheses around the arguments

49

50


Download ppt "Example 2."

Similar presentations


Ads by Google