MIC 305 Session 3 - PowerPoint PPT Presentation

1 / 31
About This Presentation
Title:

MIC 305 Session 3

Description:

Creating Visual Basic Sub Procedures. Creating User-defined Function Procedures ... Visual Basic has two types of procedures: Sub. Function ... – PowerPoint PPT presentation

Number of Views:18
Avg rating:3.0/5.0
Slides: 32
Provided by: fay87
Category:
Tags: mic | basic | session | visual

less

Transcript and Presenter's Notes

Title: MIC 305 Session 3


1
MIC 305Session 3
  • General Procedures

2
Outline Objective
  • Creating Visual Basic Sub Procedures
  • Creating User-defined Function Procedures
  • Parameter Passing Mechanism
  • Modularizing in Programming Languages

3
What is Modularization
  • Breaking the program into subtasks
  • A Sub procedure or Function performs a
    well-defined task
  • Easier to test, debug and correct

4
Why use Sub procedures and Functions?
  • Provide abstract operations
  • Make programs easier to write, debug and maintain.

5
Modularizing Programs in Visual Basic
  • In Visual Basic, there are two types of
    procedures
  • Sub
  • Function

6
Passing Arguments to Subs
  • When you define a Sub procedure sometimes you
    need to transfer variables that are used in
    different Subs. This is called passing in
    programming languages.

7
Sub Procedures Properties
  • may be called
  • may be passed data called arguments
  • may return values to the calling program
  • may change the data stored in a received
    variable

8
Components of Sub Procedure
  • name used to identify the Sub procedure
  • parameters a Sub procedure accepts values from
    the caller through its parameters it may also
    send values back to the caller through its
    parameters.

9
Sub Procedure's Name
  • The rules for naming Sub Procedures are the same
    as naming variables.
  • In this text, Sub procedure names begin with
    uppercase letters in order to distinguish them
    from variable names.

10
Syntax of a Sub Procedure
  • Private Sub ProcedureName ( )
  • statement(s)
  • End Sub

11
Creating Visual Basic Sub Procedure
  • Activate a code window
  • Select Add Procedure from the Tools menu
  • Type in the name of the Sub procedure
  • Click on Private in Scope frame
  • Press the Enter key or click the OK button
  • Type the statements of the Sub procedure into
    this window

12
Example of Call to a Sub Procedure
  • Private Sub cmdCompute_Click()
  • Dim num As Single
  • num Val(InputBox("Enter a number"))
  • Call Triple(num)
  • End Sub

13
Sub Procedure Triple
  • Private Sub Triple(num As Single)
  • ' Multiply the value of the number by 3
  • picResult.Print "The number is" 3 num
  • End Sub

14
Passing Arguments to Sub Procedures
  • Arguments Variables or expressions placed in
    parentheses in a Call statement.
  • Not only is the value of the argument passed to
    the parameter, but the value of the parameter is
    passed back to the argument.

15
Example of Arguments
  • Call Triple(num)

16
Parameters
  • Variables placed in parentheses after a Sub
    Procedure's name.
  • When the procedure is called, the values of the
    corresponding arguments are placed in the
    parameters.

17
Example of Parameters
  • Private Sub Triple(num As Single)

18
Passing arguments to parameters
  • Call Triple(num )
  • Private Sub Triple (num As Single)

Argument
Parameter
19
Passing Arguments to Parameters
  • Call Add (x, y )
  • Private Sub Add ( num1 As Single, num2 As Single)

Arguments
Parameters
20
Important Rules for Passing Arguments to a Sub
  • The number of arguments and parameters must
    match.
  • The data type of each argument must match its
    corresponding parameter.

21
Local Variables
  • A variable that is used only in a specific
    procedure (Sub or Function).
  • The scope of the local variable is the portion of
    a Sub or Function in which that variable has
    been defined.

22
Local Variables
  • Declared within a procedure definition
  • Private to a procedure definition
  • Variables in different procedures are totally
    independent
  • different procedures can have variables with the
    same names however, each variable will have its
    own memory location

23
Advantages of Local Variables
  • Extremely useful for team programming
  • To protect side effect (which is an accidental
    change of the value of the variable)

24
Example of Local Variables
  • Private Sub cmdButton_Click()
  • Dim var1 As Integer, var2 As Integer,num As
    Integer
  • var1 2
  • var2 4
  • Call Add(num)
  • picBox.Print num
  • End Sub

25
Sub Add
  • Private Sub Add(num As Integer)
  • Dim var1 As Integer, var2 As Integer
  • num var1 var2
  • End Sub

26
Form-Level Variables
  • Form-level variables are visible to every
    procedure (Global variable).
  • Form-level variables appear at the top of the
    code window.

27
How to create Form-Level Variables?
  • Activate the code window
  • Click on the down arrow to the right of the
    object list box
  • Click on General
  • Click on Declaration in the procedure list box
  • Type in Dim statements for form-level variables

28
Example
  • ' In Declaration section of General
  • Dim num1 As Single, num2 As Single

29
Review
  • Visual Basic has two types of procedures
  • Sub
  • Function
  • Each Sub procedure performs a distinct task.
  • The Call statement causes a Sub procedure to be
    executed.

30
Review
  • Values can be passed between the calling program
    and Sub by passing arguments.
  • The number and type of arguments in the calling
    program and Sub must match.

31
Review
  • Structure charts are useful in determining how to
    divide a program into Sub procedures.
  • Modularizing programs offers significant
    advantages.
  • Easier to maintain
  • Extremely useful for team programming
  • Side effect can be prevented
Write a Comment
User Comments (0)
About PowerShow.com