Introduction to Procedures and Functions |
|
Introduction to Procedures |
A procedure is a small program that is meant to solve a relatively small problem. In fact, sometimes it is called a sub-program. A program developer writes the procedure and a user (the user is either you or another programmer) would only see the result. There are two categories of procedures you will use in your programs: those that have already been created thus made available to you, and those you will create yourself. |
You can create a procedure manually or, if you are using Microsoft Visual Studio, code can be generated for you. To manually create a procedure, start by typing the Sub keyword followed by a name (like everything else, a procedure must have a name). At the end of the procedure, you must type End Sub. Therefore, the primary formula of a procedure is: Sub ProcedureName() End Sub The name of a procedure should follow the rules of names in the Visual Basic language. In addition: |
Like a procedure, a function is a sub-program used to perform an assignment. The main difference between a procedure and a function is that, after carrying its assignment, a function gives back a result. We also say that a function "returns a value". To distinguish both, there is a different syntax you use for a function. You can manually create a function or, if you are using Microsoft Visual Basic, ask it to create code for you. To manually create a function, you use the Function keyword followed by a name and parentheses. Unlike a procedure, because a function returns a value, you must specify the type of value the function will produce. To give this information, on the right side of the closing parenthesis, you can type the As keyword, followed by a data type. To indicate where a function stops, type End Function. As seen for a procedure, a function can have an access modifier. The rules for access modifiers are the same as we described for a procedure. If you are using Microsoft Visual Basic and you want the studio to generate code for you, right-click inslde a module and click Insert Snippet... In the menu, double-click Code Patterns, If, For Each, Property, etc... Double-click Properties, Procedures, Events. Double-click Define a Function. The minimum formula used to create a function is: AccessModifier(s) Function FunctionName() As DataType End Function The Function keyword is required. The name of a function follows the same rules and suggestions we reviewed for sub procedures. The As keyword may be required (in the next sections, we will review the alternatives to the As DataType expression). The DataType indicates the type of value the function will return. If the function will produce a word or a group of words, you can create it as String. The other data types are also valid in the contexts we reviewed them. Here is an example: Function GetFullName() As String End Function As done with the variables, you can also use a type character as the return type of a function and omit the As DataType expression. The type character is typed on the right side of the function name and before the opening parenthesis. An example would be GetFullname$(). As with the variables, you must use the appropriate type character for the function:
Here is an example: Function GetFullName$() End Function As mentioned already, the section between the Function and the End Function lines is the body of the function. It is used to describe what the function does. As done on a sub procedure, one of the actions you can perform in a function is to declare a (local) variable and use it as you see fit. Here is an example: Function CallMe() As String Dim Salute As String Salute = "You can call me Al" End Function
After performing an assignment in a function, to indicate the value it returns, somewhere after the assignment and before the End Function line, you can type the name of the function, followed by the = sign, followed by the value the function returns. Here is an example in which a function returns a name: Function GetFullName$() Dim FirstName As String, LastName As String FirstName = InputBox("Enter First Name: ") LastName = InputBox("Enter Last Name: ") GetFullName = LastName & ", " & FirstName End Function Alternatively, instead of using the name of the function to indicate the value it returns, you can type Return, followed by the value or the expression that the function returns. Based on this, the above function could also be created as follows: Function GetFullName$() Dim FirstName As String, LastName As String FirstName = InputBox("Enter First Name: ") LastName = InputBox("Enter Last Name: ") Return LastName & ", " & FirstName End Function You can also use some local variables in the function to perform an assignment and then assign their result to the name of the function.
As done for the sub procedure, in order to use a function in your program, you must call it. Like a sub procedure, to call a function, you can simply type its name in the desired section of the program. Here is an example: Sub Main() CallMe End Sub Since the primary purpose of a function is to return a value, to better take advantage of such a value, you can assign the name of a function to a variable in the section where you are calling the function. Here is an example: Module Exercise Function GetFullName$() Dim FirstName As String, LastName As String FirstName = InputBox("Enter First Name: ") LastName = InputBox("Enter Last Name: ") Return LastName & ", " & FirstName End Function Friend Sub Main() Dim FullName$ FullName = GetFullName() MsgBox(FullName) End Sub End Module Here is an example of running this program:
The Visual Basic Language uses a special procedure and function called Main. Main is the entry point of a program as we have used it so far. Particularly, Main can be used as a procedure or a function. So far, we have used Main only as a procedure. To use Main as a function, type the Function keyword required for each function, followed by Main(), followed by As Integer. When declared like this, the Main function must return an integer. The most regularly return value is 0, which indicates that the function ended successfully. Here is an example: Module Exercise Friend Function GetFullName$() Dim FirstName As String, LastName As String FirstName = InputBox("Enter First Name: ") LastName = InputBox("Enter Last Name: ") Return LastName & ", " & FirstName End Function Friend Function Main() As Integer Dim FullName$ FullName = GetFullName() MsgBox(FullName) Return 0 End Function End Module
Depending on an author, in the Visual Basic language, the word "procedure" includes either a procedure created with the Sub keyword, or a function created with the Function keyword. In the same way, for the rest of our lessons, the word procedure will be used to represent both types. Only when we want to be precise will we use the expression "a procedure" or "a sub-procedure" to explicitly mean the type of procedure that does not return a value. When the word "function" is used in our lessons, it explicitly refers to the type of procedure that returns a value.
If you are using a text editor to write your code, you can use the Edit main menu to do a search on the name of the procedure. If you are using Microsoft Visual Basic, in the top section of the Class View, click the name of the module in which the procedure exists. In the bottom section of the Class View, double-click the name of the procedure. The Code Editor would jump to where the procedure was defined. As an alternative, if you see a section where the procedure gets called in the Code Editor, right-click its name and click Go To Definition. This would select the name of the procedure where it is defined.
You can rename a procedure the same you would proceed for a variable. If you are using a text editor like Notepad, you can do a search on its name and replace all instances with a new name. If you are using Microsoft Visual Basic, Find the name of the procedure where it is defined and change it:
Then click the button under the new name, click the arrow on the button and click Rename. |
|
||
Home | Copyright © 2008-2016, FunctionX, Inc. | Next |
|