VB FUN

  • Upload
    deepi

  • View
    217

  • Download
    0

Embed Size (px)

Citation preview

  • 7/24/2019 VB FUN

    1/2

    FUNCTIONS& PROCUDURES:

    1. Procedures andfunctions provides a means of producing structured programs.

    2. Rather than repeating the same operations at several different places in the program, theycan be placed in a procedure or function.

    3. This not only makes the program easier to read, but saves a lot of time and effort in

    maintaining the program.PROCEDURES:

    1. A procedure is a block of code that performs some operation.

    2. A procedure is a block of isual !asic statements inside "ub, #nd "ub statements.3. Procedures do not return values.

    basic Syntax for a procedure is:

    $Private % Public&$"tatic& "ub proc'ame ($arglist&)

    Public

    *ndicates that the procedure is available to all modules. *f +ption Private is

    used in the module, the procedure is not available to modules outside of

    the proect.

    Private*ndicates that the procedure is only available to other procedures or

    functions in the current module or form.

    "tatic*ndicates that all variables declared -ithin the procedure are retained, even

    -hen the procedure is out of scope.

    proc'ame

    The name of the procedure. ust be uni/ue to the module if declared

    Private, other-ise uni/ue to the proect. The name of the procedurefollo-s the naming rule for ariables.

    arglist

    A list of variables passed to the procedure as arguments, and their data

    types. ultiple arguments are separated by commas. Arguments may be

    +ptional, and may be Read +nly.

    Visual Basic supports two ways of passin para!"t"rs to functions# By $alu" an% y

    r"f"r"nc"#

    byval :'# 0or this, -e have t-o key-ords. !yal and !yRef. hen -e pass arguments by value,

    the function -orks only -ith the copies of the values.

    (# This may lead to performance overheads, -hen -e -ork -ith large amounts of data.

    )# !yal indicates the variable -as passed by value, and any changes made -ithin theprocedure to the variable -ill not be reflected to -here the procedure -as called as its

    only a copy.

    S*+P,E PRO-R*+ FOR B.V*, :

    Private 0unction Add(!yal As *nteger, !yal y As *nteger) As *nteger

    Add 4 5 y#nd 0unction

    1

  • 7/24/2019 VB FUN

    2/2

    Private "ub 0orm67oad()

    8im a As *nteger 8im b As *nteger

    8im c As *nteger

    a 4 32

    b 4 9:

    c 4 Add(a, b)

    sg!o c

    #nd "ubby ref :

    1. hen -e pass values by reference, the function receives a reference to the actual values.2. The original values are affected, -hen modified. This -ay of passing values is more time

    and space efficient. +n the other hand, it is more error prone.

    3. !yRef indicates the variable is passed by reference, and any changes made -ithin theprocedure to the variable -ill be reflected to -here the procedure -as called.

    :. !yRef is the default in isual !asic 9

    S*+P,E PRO-R*+ FOR B.REF :

    Pri$at" Su Swap/ByR"f 0 *s Int""r1 ByR"f y *s Int""r2

    8im temp As *nteger

    temp 4

    4 y y 4 temp

    #nd "ub

    Pri$at" Su DisplayVals/ByR"f a *s Int""r1 ByVal *s Int""r2

    sg!o ;a 4 ; < ="tr(a) < vb=r7f < ;b 4 ; < ="tr(b)

    #nd "ub

    Pri$at" Su For!3,oa%/2

    8im a As *nteger

    8im b As *nteger

    a 4 1>

    b 4 12

    8isplay values, s-ap, and display again

    8isplayals a, b

    Call Swap(a, b)

    DisplayVals a, b

    En% Su

    2