Difference Between Functions & Procedures in Visual Basic

104 8

    Procedures

    • Programmers typically find themselves creating code to perform some action more than once for a given program. When this happens it is time to write a procedure that the program can call from anywhere. This makes testing and maintenance much easier since the code is in only one place. In addition to contributing to a smaller code size, your program can pass arguments to a procedure and enhance its capabilities.

    Sub Procedures

    • A sub procedure is a procedure that executes a set of instructions but does not return a value to the calling line of code. Sub procedures typically perform such actions as printing, file input and output, data validation and other generic functions. If you want to calculate bonus points with either a button on the form or a menu item, you can write the code in a sub procedure and call it from both event-handling procedures.

    Function Procedures

    • A function procedure is a procedure that executes a set of instructions and returns a value to the calling line of code. Function procedures are normally called from assignment statements which assign a value to a data variable that is the result of the actions performed by the function procedure. For example, if you want to compute an average of two grades, you could write a function procedure that accepts the five grades as parameters and returns the average. The statement to call the function procedure would look like this:

      AverageGrade = GradeAvgFunction(Grade1, Grade2)

    Arguments (or Parameters)

    • Both types of procedures accept input in the form of arguments, or parameters. The procedure must be written in a way that it is expecting the arguments, and the arguments must be passed in the exact order expected by the procedure. Otherwise, unpredictable results may occur. This capability serves to make a procedure more flexible and thus more reusable. In our example above, the GradeAvgFunction function procedure must be expecting Grade1 and Grade2 in that exact order. While the order of the arguments would not matter in this example, it would matter greatly if your procedure is supposed to calculate a sales commission.

Source...
Subscribe to our newsletter
Sign up here to get the latest news, updates and special offers delivered directly to your inbox.
You can unsubscribe at any time

Leave A Reply

Your email address will not be published.