Page 189 - Open Soource Technologies 304.indd
P. 189

Unit 8: Understanding Function and Procedure



               It is easier to debug a program, a program with procedures, which breaks a program into discrete
               logical limits.
               Procedures used in one program can act as building blocks for other programs with slight
               modifications.
               A Procedure can be Sub, Function or Property Procedure.
               Functions are similar to normal procedures but the main purpose of the functions is to accept
               certain inputs and pass them on to the main program to finish the execution. They are two types
               of function, the built-in functions (or internal functions) and the functions created by the
               programmers.
               8.1 Function Procedure


               Visual Basic has many built-in function. In one respect, function are like miniature programs.
               They use input, they process the input, and they have output. Some function we encountered
               earlier are listed in Table 8.1
                                   Table 8.1: Some Visual Basic Built-in Function.

                         Function           Example           Input        Ouput
                            Int        Int(2.6) is 2         number        number

                            Chr        Chr(65) is "A"        number        string

                            Asc        Asc("Apple") is 65    string        number

               Format Number (12345.628,1) is 12,345.6 number, number string
               Although the input can involve several values, the output always consists of a single value. The
               items inside the parentheses can be literals (as in Table 5.1), variables, or expressions.
               In addition to using built-in function, we can define function of our own. These new function,
               called function procedure or user-defined function, are defined in much the same way as Sub
               procedure and are used in the same way as built-in function. Like built-in function, function
               procedure have a single output that can be of any data type. Function procedure can be used in
               expressions in exactly the same way as built-in function. Programs refer to them as if they were
               literals, variables, or expressions. function procedure are defined by function blocks of the form
               function FunctionName(ByVal var1 As Type1, _
                                     ByVal var2 As Type2, ...) As DataType
                 statement(s)
                 Return expression
               End function
               The variables appearing in the top line are called parameters. Variables declared by statements
               inside the function block have local scope. Function names should be suggestive of the role
               performed and must conform to the rules for naming variables. The type DataType, which
               specifies the type of the output, will be one of String, Integer, Double, and so on. In the preceding
               general code, the next-to-last line specifies the output, which must be of type DataType. Like Sub
               procedure, function procedure are typed directly into the Code window. (The last line, End
               function, will appear automatically after the first line is entered into the Code window.)


                                      LOVELY PROFESSIONAL UNIVERSITY                        183
   184   185   186   187   188   189   190   191   192   193   194