copyright © don kusseee 1120-ch6#621 cns 1120 chapter 6 general subs or programmer-defined...

62
Copyright © Don Kusseee 1 120-Ch6#62 1 CNS 1120 Chapter 6 General Subs or Programmer-Defined Functions 1120-04.PPT

Upload: kristina-miller

Post on 18-Jan-2018

231 views

Category:

Documents


0 download

DESCRIPTION

Copyright © Don Kusseee 1120-Ch6#623 Sub or Functions An event sub-procedure that is called (run or invoked) whenever some event occurs A general sub-procedure can be built and called from code, not called by an event. Very useful when some operation must be done frequently -and called from many different locations within the program. Reusable code the objective

TRANSCRIPT

Page 1: Copyright © Don Kusseee 1120-Ch6#621 CNS 1120 Chapter 6 General Subs or Programmer-Defined Functions 1120-04.PPT

Copyright © Don Kusseee 1120-Ch6#62

1

CNS 1120Chapter 6

General Subs or Programmer-Defined Functions

1120-04.PPT

Page 2: Copyright © Don Kusseee 1120-Ch6#621 CNS 1120 Chapter 6 General Subs or Programmer-Defined Functions 1120-04.PPT

Copyright © Don Kusseee 1120-Ch6#62

2

Programming Language components

• Base type• Keywords or reserved words• Language provided libraries

– Objects, Functions, Procedures• Add-ins purchased libraries

– Objects, Functions, Procedures• User defined libraries

– Objects, Functions, Procedures

Page 3: Copyright © Don Kusseee 1120-Ch6#621 CNS 1120 Chapter 6 General Subs or Programmer-Defined Functions 1120-04.PPT

Copyright © Don Kusseee 1120-Ch6#62

3

Sub or Functions

• An event sub-procedure that is called (run or invoked) whenever some event occurs

• A general sub-procedure can be built and called from code, not called by an event.

• Very useful when some operation must be done frequently -and called from many different locations within the program.

• Reusable code the objective

Page 4: Copyright © Don Kusseee 1120-Ch6#621 CNS 1120 Chapter 6 General Subs or Programmer-Defined Functions 1120-04.PPT

Copyright © Don Kusseee 1120-Ch6#62

4

Sub or Functions

• Chunks of standard VB code that are used to do one single task very well

• Can be called (used) by any code – Private on the Form (saved in the .FRM file)– Public in the Program (saved in a .BAS file)– Friend

• Many parameters can be passed in & used

Page 5: Copyright © Don Kusseee 1120-Ch6#621 CNS 1120 Chapter 6 General Subs or Programmer-Defined Functions 1120-04.PPT

Copyright © Don Kusseee 1120-Ch6#62

5

Sub - Functions Examples

• X = MsgBox(“Prompt”,Graphic )• S = InputBox(“Prompt”,”Title”)• I = CInt(S)• S = Str(I)• Payment = PMT(Rate, Periods, Principal)• cmdCut_Click• S = LCase(S)

Page 6: Copyright © Don Kusseee 1120-Ch6#621 CNS 1120 Chapter 6 General Subs or Programmer-Defined Functions 1120-04.PPT

Copyright © Don Kusseee 1120-Ch6#62

6

Code reuse

• Code reuse is hard to do, but is pushed farthest in VB with objects and controls

• Programmers prefer reinventing the wheel• C, C++, Java are starting to improve• The ideal would be to go to Lowes &

pickup a 10/32 bolts, washers and nuts• The way that electronic “chips” (CPU,

memory, interfaces) are used now

Page 7: Copyright © Don Kusseee 1120-Ch6#621 CNS 1120 Chapter 6 General Subs or Programmer-Defined Functions 1120-04.PPT

Copyright © Don Kusseee 1120-Ch6#62

7

New Programming Concept

• Modular programming• Break a large program into parts

– Divide the workload– Make the design easier– Test more completely – Allow program modification– Make the change easy

Page 8: Copyright © Don Kusseee 1120-Ch6#621 CNS 1120 Chapter 6 General Subs or Programmer-Defined Functions 1120-04.PPT

Copyright © Don Kusseee 1120-Ch6#62

8

Functions

CodeCode

Fun 1Fun 1

Write some code in the main program, and Write some code in the main program, and create a function that could be in the maincreate a function that could be in the mainprogram, but because you will use it severalprogram, but because you will use it severaltimes, you put it in a function rather than thetimes, you put it in a function rather than themain code. The process will be learned main code. The process will be learned later. later.

MainMain# 1# 1

Page 9: Copyright © Don Kusseee 1120-Ch6#621 CNS 1120 Chapter 6 General Subs or Programmer-Defined Functions 1120-04.PPT

Copyright © Don Kusseee 1120-Ch6#62

9

Functions

CodeCode

Fun 1Fun 1

Write some code in the main program, and Write some code in the main program, and create a function that could be in the maincreate a function that could be in the mainprogram, but because you will use it severalprogram, but because you will use it severaltimes, you put it in a function rather than thetimes, you put it in a function rather than themain code. The process will be learned main code. The process will be learned later.later. After the function’s code is run, you After the function’s code is run, you return to the main program, to the next linereturn to the main program, to the next lineof code.of code.

MainMain# 1# 1

Page 10: Copyright © Don Kusseee 1120-Ch6#621 CNS 1120 Chapter 6 General Subs or Programmer-Defined Functions 1120-04.PPT

Copyright © Don Kusseee 1120-Ch6#62

10

Functions

CodeCode

Fun 2Fun 2

Fun 1Fun 1

Now a second function is used. This codeNow a second function is used. This codecould have been part of the main code, orcould have been part of the main code, orpart of function 1. Functions usually performpart of function 1. Functions usually performonly one task, and a new function is built for only one task, and a new function is built for each task. each task.

MainMain# 1# 1 #2#2

Page 11: Copyright © Don Kusseee 1120-Ch6#621 CNS 1120 Chapter 6 General Subs or Programmer-Defined Functions 1120-04.PPT

Copyright © Don Kusseee 1120-Ch6#62

11

Functions

CodeCode

Fun 2Fun 2

Fun 1Fun 1

Now a second function is used. This codeNow a second function is used. This codecould have been part of the main code, orcould have been part of the main code, orpart of function 1. Functions usually performpart of function 1. Functions usually performonly one task, and a new function is built for only one task, and a new function is built for each task.each task. After function 2 us run, control is After function 2 us run, control isreturned to the main program, and the nextreturned to the main program, and the nextline of code.line of code.

MainMain# 1# 1 #2#2

Page 12: Copyright © Don Kusseee 1120-Ch6#621 CNS 1120 Chapter 6 General Subs or Programmer-Defined Functions 1120-04.PPT

Copyright © Don Kusseee 1120-Ch6#62

12

CodeCode Fun 3Fun 3

Fun 2Fun 2

Fun 1Fun 1

Function 3 is now called (used). AgainFunction 3 is now called (used). Againthis code could have been part of the this code could have been part of the main code, or could have been part of function main code, or could have been part of function 2’s code, but is so long it was not built that way. 2’s code, but is so long it was not built that way. The programmer (or his boss ) makes those The programmer (or his boss ) makes those decisions. The user can not tell how the programdecisions. The user can not tell how the program is built.is built.

MainMain# 1# 1 #2#2 # 3# 3

Page 13: Copyright © Don Kusseee 1120-Ch6#621 CNS 1120 Chapter 6 General Subs or Programmer-Defined Functions 1120-04.PPT

Copyright © Don Kusseee 1120-Ch6#62

13

CodeCode

CodeCode

Fun 3Fun 3

Fun 2Fun 2

Fun 1Fun 1

When function 3 has completed, When function 3 has completed, control returns to the main programcontrol returns to the main programand rather than call a new (or recalland rather than call a new (or recallan old function); new code is written an old function); new code is written in the main program. If that code werein the main program. If that code weregoing to be used more than once, the going to be used more than once, the code could have been written as a code could have been written as a function, rather than main code. function, rather than main code.

MainMain# 1# 1 #2#2 # 3# 3

Page 14: Copyright © Don Kusseee 1120-Ch6#621 CNS 1120 Chapter 6 General Subs or Programmer-Defined Functions 1120-04.PPT

Copyright © Don Kusseee 1120-Ch6#62

14

CodeCode

CodeCode

CodeCode

Fun 3Fun 3

Fun 2Fun 2

Fun 4Fun 4

Fun 1Fun 1

A new function is calledA new function is calledand after completion control is returned to the and after completion control is returned to the main function, and more code is added to main.main function, and more code is added to main.

MainMain# 1# 1 #2#2 # 3# 3

# 4# 4

Page 15: Copyright © Don Kusseee 1120-Ch6#621 CNS 1120 Chapter 6 General Subs or Programmer-Defined Functions 1120-04.PPT

Copyright © Don Kusseee 1120-Ch6#62

15

CodeCode

CodeCode

CodeCode

Fun 3Fun 3

Fun 2Fun 2

Fun 4Fun 4

Fun 1Fun 1

Function 2 is reused. OneFunction 2 is reused. Oneof the main benefits of functions is to be able to of the main benefits of functions is to be able to reuse them with out copying the code and makingreuse them with out copying the code and makingthe program longer. After completion, control is the program longer. After completion, control is returned to the main program, the next line.returned to the main program, the next line.

MainMain# 1# 1 #2#2 # 3# 3

# 4# 4

Page 16: Copyright © Don Kusseee 1120-Ch6#621 CNS 1120 Chapter 6 General Subs or Programmer-Defined Functions 1120-04.PPT

Copyright © Don Kusseee 1120-Ch6#62

16

CodeCode

CodeCode

CodeCode

CodeCode

Fun 3Fun 3

Fun 2Fun 2

Fun 4Fun 4

Fun 1Fun 1

Notice that Function 2 is Notice that Function 2 is called (used) twice in a row. Often, code is writtencalled (used) twice in a row. Often, code is writtento “sneak up on” the answer. Do something, then to “sneak up on” the answer. Do something, then check if you found the answer, if not, repeat the check if you found the answer, if not, repeat the call until you do find the answer. Return to new call until you do find the answer. Return to new code in main when the function is finished.code in main when the function is finished.

MainMain# 1# 1 #2#2 # 3# 3

# 4# 4

Page 17: Copyright © Don Kusseee 1120-Ch6#621 CNS 1120 Chapter 6 General Subs or Programmer-Defined Functions 1120-04.PPT

Copyright © Don Kusseee 1120-Ch6#62

17

CodeCode

CodeCode

CodeCode

CodeCode

Fun 3Fun 3

Fun 2Fun 2

Fun 4Fun 4

Fun 5Fun 5

Fun 1Fun 1

New function called.New function called.

MainMain# 1# 1 #2#2 # 3# 3

# 4# 4

# 5# 5

Page 18: Copyright © Don Kusseee 1120-Ch6#621 CNS 1120 Chapter 6 General Subs or Programmer-Defined Functions 1120-04.PPT

Copyright © Don Kusseee 1120-Ch6#62

18

Fun 6Fun 6

Functions

CodeCode

CodeCode

CodeCode

CodeCode

Fun 3Fun 3

Fun 2Fun 2

Fun 4Fun 4

Fun 5Fun 5

Fun 1Fun 1

New function called.New function called.That function calls a second new function. That function calls a second new function. Functions can call functions as well as mainFunctions can call functions as well as main can call functions. can call functions.

MainMain# 1# 1 #2#2 # 3# 3

# 4# 4

# 5# 5

# 6# 6

Page 19: Copyright © Don Kusseee 1120-Ch6#621 CNS 1120 Chapter 6 General Subs or Programmer-Defined Functions 1120-04.PPT

Copyright © Don Kusseee 1120-Ch6#62

19

CodeCode

CodeCode

CodeCode

CodeCode

Fun 3Fun 3

Fun 2Fun 2

Fun 4Fun 4

Fun 5Fun 5

Fun 7Fun 7

Fun 6Fun 6

Fun 1Fun 1

Function 6 calls a new function 7.Function 6 calls a new function 7.When Function 7 is completed, When Function 7 is completed, control will return to the calling control will return to the calling function 6. function 6.

MainMain# 1# 1 #2#2 # 3# 3

# 4# 4

# 5# 5

# 6# 6

# 7# 7

Page 20: Copyright © Don Kusseee 1120-Ch6#621 CNS 1120 Chapter 6 General Subs or Programmer-Defined Functions 1120-04.PPT

Copyright © Don Kusseee 1120-Ch6#62

20

CodeCode

CodeCode

CodeCode

CodeCode

Fun 3Fun 3

Fun 2Fun 2

Fun 4Fun 4

Fun 5Fun 5

Fun 7Fun 7

Fun 8Fun 8

Fun 6Fun 6

Fun 1Fun 1

After the return from function 7, After the return from function 7, function 6 calls function 8. function 6 calls function 8.

MainMain# 1# 1 #2#2 # 3# 3

# 4# 4

# 5# 5

# 6# 6

# 7# 7 #8#8

Page 21: Copyright © Don Kusseee 1120-Ch6#621 CNS 1120 Chapter 6 General Subs or Programmer-Defined Functions 1120-04.PPT

Copyright © Don Kusseee 1120-Ch6#62

21

Functions

CodeCode

CodeCode

CodeCode

CodeCode

Fun 3Fun 3

Fun 2Fun 2

Fun 4Fun 4

Fun 5Fun 5

Fun 7Fun 7

Fun 8Fun 8

Fun 6Fun 6

Fun 1Fun 1

Function 8 completes, returns controlFunction 8 completes, returns controlto function 6. Function 6 now to function 6. Function 6 now completes to Function 5. Thatcompletes to Function 5. That completes and returns to completes and returns to the main program the main program and new code. and new code.

CodeCode

Page 22: Copyright © Don Kusseee 1120-Ch6#621 CNS 1120 Chapter 6 General Subs or Programmer-Defined Functions 1120-04.PPT

Copyright © Don Kusseee 1120-Ch6#62

22

Functions

CodeCode

CodeCode

CodeCode

CodeCode

CodeCode

Fun 3Fun 3

Fun 2Fun 2

Fun 4Fun 4

Fun 5Fun 5

Fun 8Fun 8

Fun 7Fun 7

Fun 6Fun 6

Fun 1Fun 1

Function 4 is calledFunction 4 is calledagain.again.

Page 23: Copyright © Don Kusseee 1120-Ch6#621 CNS 1120 Chapter 6 General Subs or Programmer-Defined Functions 1120-04.PPT

Copyright © Don Kusseee 1120-Ch6#62

23

Functions

CodeCode

CodeCode

CodeCode

CodeCode

CodeCode

Fun 3Fun 3

Fun 2Fun 2

Fun 4Fun 4

Fun 5Fun 5

Fun 8Fun 8

Fun 7Fun 7

Fun 6Fun 6

Fun 1Fun 1

Fun 9Fun 9Function 4 completesFunction 4 completesand returns to the main codeand returns to the main codeand new function 9 is called.and new function 9 is called.

Page 24: Copyright © Don Kusseee 1120-Ch6#621 CNS 1120 Chapter 6 General Subs or Programmer-Defined Functions 1120-04.PPT

Copyright © Don Kusseee 1120-Ch6#62

24

CodeCode

CodeCode

CodeCode

CodeCode

CodeCode

Fun 3Fun 3

Fun 2Fun 2

Fun 4Fun 4

Fun 5Fun 5

Fun 8Fun 8

Fun 7Fun 7

Fun 6Fun 6

Fun 1Fun 1

Fun 9Fun 9Function 9 calls itself.Function 9 calls itself.This is called This is called recursionrecursion. It is used frequently to . It is used frequently to ““sneak up on an answer”. sneak up on an answer”. RecursionRecursion is considered is consideredone of the primary measures of a language.one of the primary measures of a language.

Page 25: Copyright © Don Kusseee 1120-Ch6#621 CNS 1120 Chapter 6 General Subs or Programmer-Defined Functions 1120-04.PPT

Copyright © Don Kusseee 1120-Ch6#62

25

CodeCode

CodeCode

CodeCode

CodeCode

CodeCode

Fun 3Fun 3

Fun 2Fun 2

Fun 4Fun 4

Fun 5Fun 5

Fun 8Fun 8

Fun 7Fun 7

Fun 6Fun 6

Fun 1Fun 1

Fun 9Fun 9Function 9 againFunction 9 againcalls itself.calls itself.

Page 26: Copyright © Don Kusseee 1120-Ch6#621 CNS 1120 Chapter 6 General Subs or Programmer-Defined Functions 1120-04.PPT

Copyright © Don Kusseee 1120-Ch6#62

26

CodeCode

CodeCode

CodeCode

CodeCode

CodeCode

Fun 3Fun 3

Fun 2Fun 2

Fun 4Fun 4

Fun 8Fun 8

Fun 7Fun 7

Fun 6Fun 6

Fun 1Fun 1

Fun 9Fun 9Function 9 recursively calls itself again.Function 9 recursively calls itself again.It reaches a solution, and returns from Function 9It reaches a solution, and returns from Function 9to Function 9, and returns from Function 9 toto Function 9, and returns from Function 9 toFunction 9, and returns from Function 9 to main.Function 9, and returns from Function 9 to main.

Page 27: Copyright © Don Kusseee 1120-Ch6#621 CNS 1120 Chapter 6 General Subs or Programmer-Defined Functions 1120-04.PPT

Copyright © Don Kusseee 1120-Ch6#62

27

CodeCode

CodeCode

CodeCode

CodeCode

CodeCode

Fun 3Fun 3

Fun 2Fun 2

Fun 4Fun 4

Fun 5Fun 5

Fun 8Fun 8

Fun 7Fun 7

Fun 6Fun 6

Fun 1Fun 1

Fun 9Fun 9

Function 6 is called directly now,Function 6 is called directly now,Not through Function 5.Not through Function 5.

Page 28: Copyright © Don Kusseee 1120-Ch6#621 CNS 1120 Chapter 6 General Subs or Programmer-Defined Functions 1120-04.PPT

Copyright © Don Kusseee 1120-Ch6#62

28

CodeCode

CodeCode

CodeCode

CodeCode

CodeCode

Fun 3Fun 3

Fun 2Fun 2

Fun 4Fun 4

Fun 5Fun 5

Fun 8Fun 8

Fun 7Fun 7

Fun 6Fun 6

Fun 1Fun 1

Fun 9Fun 9

Function 6 calls function 7,Function 6 calls function 7,7 returns to 6, and 6 calls 8.7 returns to 6, and 6 calls 8.8 returns to 6, and 6 returns to main.8 returns to 6, and 6 returns to main.

Page 29: Copyright © Don Kusseee 1120-Ch6#621 CNS 1120 Chapter 6 General Subs or Programmer-Defined Functions 1120-04.PPT

Copyright © Don Kusseee 1120-Ch6#62

29

CodeCode

CodeCode

CodeCode

CodeCode

CodeCode

CodeCode

CodeCode

Fun 3Fun 3

Fun 2Fun 2

Fun 4Fun 4

Fun 5Fun 5

Fun 8Fun 8

Fun 7Fun 7

Fun 6Fun 6

Fun 1Fun 1

Fun 9Fun 9

Main calls 5, it calls 6, it calls 7, which returns to 6, Main calls 5, it calls 6, it calls 7, which returns to 6, 6 now calls 8, which returns to 6, 6 now calls 8, which returns to 6, which returns to 5, which which returns to 5, which returns to main, returns to main, and new code.and new code.

Page 30: Copyright © Don Kusseee 1120-Ch6#621 CNS 1120 Chapter 6 General Subs or Programmer-Defined Functions 1120-04.PPT

Copyright © Don Kusseee 1120-Ch6#62

30

CodeCode

CodeCode

CodeCode

CodeCode

CodeCode

CodeCode

CodeCode

Fun 3Fun 3

Fun 2Fun 2

Fun 4Fun 4

Fun 5Fun 5

Fun 8Fun 8

Fun 7Fun 7

Fun 6Fun 6

Fun 1Fun 1

Fun 9Fun 9

The program could continue with more calls.The program could continue with more calls.

Page 31: Copyright © Don Kusseee 1120-Ch6#621 CNS 1120 Chapter 6 General Subs or Programmer-Defined Functions 1120-04.PPT

Copyright © Don Kusseee 1120-Ch6#62

31

Sub or Functions

• Many non - object oriented languages (C, QBasic and Pascal) are built using functions

• 90 to 99 % of any C program are functions• Non OOP languages, functions are very an

important concept• In OOP it is also important, but functions are

incorporated more completely in objects as methods

Page 32: Copyright © Don Kusseee 1120-Ch6#621 CNS 1120 Chapter 6 General Subs or Programmer-Defined Functions 1120-04.PPT

Copyright © Don Kusseee 1120-Ch6#62

32

Sub or Functions

• Not required - can be done in spaghetti code• Modularity is to ease the programming task• Break a large program into smaller parts

– Divide the workload– Simplify the design– Improve testing completeness & ease – Make modification possible – Improve the ability to change the code

Page 33: Copyright © Don Kusseee 1120-Ch6#621 CNS 1120 Chapter 6 General Subs or Programmer-Defined Functions 1120-04.PPT

Copyright © Don Kusseee 1120-Ch6#62

33

Functions Vs Sub

• Function returns a one and only one valuestr = InputBox( ) returns a stringnum=MsgBox( )returns a integer (button number)num = Sqr( ) returns a number (double)‘Note required variable on left side

• Sub-Procedures do not return a value– MsgBox statement (procedure) returns nothing

Page 34: Copyright © Don Kusseee 1120-Ch6#621 CNS 1120 Chapter 6 General Subs or Programmer-Defined Functions 1120-04.PPT

Copyright © Don Kusseee 1120-Ch6#62

34

To create and use a Sub / Function

• Declare the function - add/procedure tool• Define the function - write the VB code • Use the function - Call the function

Page 35: Copyright © Don Kusseee 1120-Ch6#621 CNS 1120 Chapter 6 General Subs or Programmer-Defined Functions 1120-04.PPT

Copyright © Don Kusseee 1120-Ch6#62

35

Declare the functionsTools|Add Procedure

• Name ?• Return a value ?• Scope ?• Static ?

Page 36: Copyright © Don Kusseee 1120-Ch6#621 CNS 1120 Chapter 6 General Subs or Programmer-Defined Functions 1120-04.PPT

Copyright © Don Kusseee 1120-Ch6#62

36

Define the Function / Sub code

• Using standard VB code• May use local or global variables• May change parameters • Can start events• May use “passed” in variables• Could call other Functions and/or Sub• Could call other forms and objects

Page 37: Copyright © Don Kusseee 1120-Ch6#621 CNS 1120 Chapter 6 General Subs or Programmer-Defined Functions 1120-04.PPT

Copyright © Don Kusseee 1120-Ch6#62

37

Sub or Functions

• Chunks of standard VB code that are used to do one single task very well

• Can be called (used) by any code – Private on the Form (saved in the .FRM file)– Public in the Program (saved in a .BAS file)

• Many parameters can be passed in & used

Page 38: Copyright © Don Kusseee 1120-Ch6#621 CNS 1120 Chapter 6 General Subs or Programmer-Defined Functions 1120-04.PPT

Copyright © Don Kusseee 1120-Ch6#62

38

Code Sub-procedure Vs Function

Private Sub George (list of parameters) VB Code ‘ ProcedureEnd SubPrivate Function George (parameters) as Long VB Code ‘ Function George = X ‘is the value to be returnedEnd Function

Page 39: Copyright © Don Kusseee 1120-Ch6#621 CNS 1120 Chapter 6 General Subs or Programmer-Defined Functions 1120-04.PPT

Copyright © Don Kusseee 1120-Ch6#62

39

Example Sub

Private Sub MyBeep( ) BEEP ‘ Note indentationEnd SubPrivate Sub MyBeepTwo( ) BEEP ‘ Note indentation BEEPEnd Sub

Page 40: Copyright © Don Kusseee 1120-Ch6#621 CNS 1120 Chapter 6 General Subs or Programmer-Defined Functions 1120-04.PPT

Copyright © Don Kusseee 1120-Ch6#62

40

Sample SubPrivate Sub MyBeep( ) ‘ no passed parameters BEEPEnd SubPrivate Sub MyBeep( N As Integer)’ N passed For I = 1 to N BEEP Next End Sub

Page 41: Copyright © Don Kusseee 1120-Ch6#621 CNS 1120 Chapter 6 General Subs or Programmer-Defined Functions 1120-04.PPT

Copyright © Don Kusseee 1120-Ch6#62

41

Call the Sub / Function

• Included in some standard VB code– Name the Function (and the parameters passed)

• this is the “call”x = Call MsgBox (“This is a parameter”)

– Computer transfers to that code and runs– When the function ends, the return is placed in the

variable x, then operation returns to the calling statement, and continues with the next line of code after the call

Page 42: Copyright © Don Kusseee 1120-Ch6#621 CNS 1120 Chapter 6 General Subs or Programmer-Defined Functions 1120-04.PPT

Copyright © Don Kusseee 1120-Ch6#62

42

Passing information in Sub

• Parameters (information) passed into– Name the variable containing values– Data type of variables passed in– Order of variable to be passed in

Private Sub X(ByVal M As Integer, ByVal N As Single)– X is the function or procedure name– M is the first parameter and is of type Integer– N is the second parameter and is of type Single

Page 43: Copyright © Don Kusseee 1120-Ch6#621 CNS 1120 Chapter 6 General Subs or Programmer-Defined Functions 1120-04.PPT

Copyright © Don Kusseee 1120-Ch6#62

43

Passing information in Functions

• The parameter passing is the same as in Sub• Private Function K(x As Double) As String

– The As String defines the one return data type• S = K( P )

– The call - with the name K– The parameters passed - P must be of type Double– The return value - S must be of type String

Page 44: Copyright © Don Kusseee 1120-Ch6#621 CNS 1120 Chapter 6 General Subs or Programmer-Defined Functions 1120-04.PPT

Copyright © Don Kusseee 1120-Ch6#62

44

Invoke or Call or Use aSub or Function

• The sub name ( list of real parameters)– Private Sub X(ByVal M As Integer, N As Single)

• Calling a sub– X(S, R) ‘ S is an Integer, R is a Single– X(47, -3.08) ‘ Literal values passed in– X( -12, R) ‘Literal -12, R is a Single

• Parameters are positional related

Page 45: Copyright © Don Kusseee 1120-Ch6#621 CNS 1120 Chapter 6 General Subs or Programmer-Defined Functions 1120-04.PPT

Copyright © Don Kusseee 1120-Ch6#62

45

Call parameters

• Must include the correct number of parameters - Error Not Optional

• Parameters must be the correct type - Error Type Mismatch

• Parameters can be variables or literals or the returned result of other VB code (function)

• Optional keyword allowedPrivate Sub Art(Optional brush As Single)

Page 46: Copyright © Don Kusseee 1120-Ch6#621 CNS 1120 Chapter 6 General Subs or Programmer-Defined Functions 1120-04.PPT

Copyright © Don Kusseee 1120-Ch6#62

46

Function parameters

• Must match call number - Error Not Optional• Must match call type - Error Type Mismatch• Declare the name of the variable to be used in

the function– Variables are local to the function.– Variable name can be the call variables names– Variables names are usually not the from call

names

Page 47: Copyright © Don Kusseee 1120-Ch6#621 CNS 1120 Chapter 6 General Subs or Programmer-Defined Functions 1120-04.PPT

Copyright © Don Kusseee 1120-Ch6#62

47

Pass by Value VS Pass by Ref

• Pass by value - usual method– Makes a copy and passes the copy into function– Changes in the function don’t effect the original

• Pass by reference - necessary at some times– Points to the original data for use (Global)– Changes in the function do effect the original

• Also possible to pass objects

Page 48: Copyright © Don Kusseee 1120-Ch6#621 CNS 1120 Chapter 6 General Subs or Programmer-Defined Functions 1120-04.PPT

Copyright © Don Kusseee 1120-Ch6#62

48

Pass by Value

• Private Sub Art(ByVal brush As Single)• Pass by Value A new chunk of memory is

defined and contains a copy of what is in the original memory location

• Pass by Ref - the memory used by the variable name is the same memory location. There is only one location

• ByVal are the preferred parameters

Page 49: Copyright © Don Kusseee 1120-Ch6#621 CNS 1120 Chapter 6 General Subs or Programmer-Defined Functions 1120-04.PPT

Copyright © Don Kusseee 1120-Ch6#62

49

General Sub Vs Event Procedures

• General Sub– Name does Not associated code with a control– May be located in a code Module– Parameter list is determined by programmer

• Event Procedure– Control_event ( ) is the name– Can not be in code Module– Parameter list can not be changed

Page 50: Copyright © Don Kusseee 1120-Ch6#621 CNS 1120 Chapter 6 General Subs or Programmer-Defined Functions 1120-04.PPT

Copyright © Don Kusseee 1120-Ch6#62

50

Scope

• Variables - Local, Module, Global • Forms - all are Global • Sub & Functions

– Private - Module (global to form)– Public Global (global to program)

Page 51: Copyright © Don Kusseee 1120-Ch6#621 CNS 1120 Chapter 6 General Subs or Programmer-Defined Functions 1120-04.PPT

Copyright © Don Kusseee 1120-Ch6#62

51

Desk checking code

• Pretend you are the computer• Keep track of the current value contained in

each variable, use a variable table.• Follow each possible path (test it, which

may require forcing some values)• This analysis is required in bullet proofing

code

Page 52: Copyright © Don Kusseee 1120-Ch6#621 CNS 1120 Chapter 6 General Subs or Programmer-Defined Functions 1120-04.PPT

Copyright © Don Kusseee 1120-Ch6#62

52

Diagnostic or Debugging

• Establish breakpoints F9 • Run code with a breakpoint - examination

variables as you go– Step over functions & Sub shift F8– Step into functions & Sub F8

• Change the values to test various paths• Test all the possible paths

Page 53: Copyright © Don Kusseee 1120-Ch6#621 CNS 1120 Chapter 6 General Subs or Programmer-Defined Functions 1120-04.PPT

Copyright © Don Kusseee 1120-Ch6#62

53

Exit

• Can be used in either a Sub or Function• Usual the code ends with the End Sub or

End Function• For an abnormal end ( usually reserved for

error situations)– Exit Function– Exit Sub

Page 54: Copyright © Don Kusseee 1120-Ch6#621 CNS 1120 Chapter 6 General Subs or Programmer-Defined Functions 1120-04.PPT

Copyright © Don Kusseee 1120-Ch6#62

54

Sub Main

• Usually VB starts with the first Form created in a project. It is possible to change to other than the first form, or it is possible to have a Sub or Function run before the first Form is displayed.

• If the name Sub or Function is Main ( )• Change in Project Properties

Page 55: Copyright © Don Kusseee 1120-Ch6#621 CNS 1120 Chapter 6 General Subs or Programmer-Defined Functions 1120-04.PPT

Copyright © Don Kusseee 1120-Ch6#62

55

Bullet proofing programs

• Now that code runs correctly for a correct input, how do we prevent an incorrect input from crashing the program?

• More time and effort is spent on bullet proofing, than on the prime code

Page 56: Copyright © Don Kusseee 1120-Ch6#621 CNS 1120 Chapter 6 General Subs or Programmer-Defined Functions 1120-04.PPT

Copyright © Don Kusseee 1120-Ch6#62

56

Frequent user input errors

• Use of $ and , and ( ) and - in number inputs• Alphabetic letter for a mathematics input• No value entered when required• No disk / file present for disk read• Disk locked or full for a disk write• 0 value entered• Nonsense value input salary = -5.32/hr

Page 57: Copyright © Don Kusseee 1120-Ch6#621 CNS 1120 Chapter 6 General Subs or Programmer-Defined Functions 1120-04.PPT

Copyright © Don Kusseee 1120-Ch6#62

57

Corrections for Frequent user input errors

• Prompt, so they know what is wanted• Check all inputs for correctness, signal an

error, prompt and allow re-enters• Use MsgBox to limit possible user inputs• Write error checking modules• Write programs that terminate gracefully

Page 58: Copyright © Don Kusseee 1120-Ch6#621 CNS 1120 Chapter 6 General Subs or Programmer-Defined Functions 1120-04.PPT

Copyright © Don Kusseee 1120-Ch6#62

58

KeyPress Event

• Each key stroke generates 3 events– Keydown– Keyup– Keypress

• Returns ANSI (ASCII) value of the key• Non-ANSI keys (Shift,Alt,Ctrl,…) cause no event• Placing a 0 in the event leaves txt.text unchanged

Page 59: Copyright © Don Kusseee 1120-Ch6#621 CNS 1120 Chapter 6 General Subs or Programmer-Defined Functions 1120-04.PPT

Copyright © Don Kusseee 1120-Ch6#62

59

GotFocus & LostFocus

• Events that allow the program to react to these actions

• Tab or Enter key will advance the focus to the next control

• Order established by build or by TabIndex• TabStop prevents user, tabbing to control• SetFocus allows the program to control

Page 60: Copyright © Don Kusseee 1120-Ch6#621 CNS 1120 Chapter 6 General Subs or Programmer-Defined Functions 1120-04.PPT

Copyright © Don Kusseee 1120-Ch6#62

60

Modal or Modeless

• Modeless (Application)- typical Win 95 window• Other windows programs can be used

Alt-tab allows rotates between programs

• Modal - grabs control of the system and will not allow any action except on the modal window

• System Modal takes control of whole system

Page 61: Copyright © Don Kusseee 1120-Ch6#621 CNS 1120 Chapter 6 General Subs or Programmer-Defined Functions 1120-04.PPT

Copyright © Don Kusseee 1120-Ch6#62

61

Password

• TextBox allows password use• Input letter is replaced with some user

defined symbol - PasswordChar• TextBox does not do any checking - that is

up to the programmer

Page 62: Copyright © Don Kusseee 1120-Ch6#621 CNS 1120 Chapter 6 General Subs or Programmer-Defined Functions 1120-04.PPT

Copyright © Don Kusseee 1120-Ch6#62

62

Key Terms

• Argument• Parameters• Errors• ByVal• ByRef• Exit Function• Exit Sub• Function

• Sub Procedure• Reusable code• Sub Main• Public or Private• GotFocus• SetFocus• LostFocus• KeyPress