Below is a basic example of a function:
This is really basic example and in reality a function will be often be more complicated that one line.
Note we can call the function by stating the function name.
This runs the code in the function.
Now for a more complicated example. In the above example no data was passed between the caller and the function.
When we create the function volume, we state that we expect 3 variable to be passed. These are all defined as the double datatype. Notice outside the brackets we define the function itself as a double. This allows us to assign the function a value inside the function - this value in then return to the caller.
In the "Sub" we see that there is a message box created and some text is combined with the returned result to create a meaningful message for the user. To combine the double and string, the double needs to be converted into a string with the CStr() function.
Code Snipped:
Function Volume(a As Double, b As Double, c As Double) As Double
Volume = a * b * c
End Function
Sub a6_functions()
MsgBox "Volume is : " + CStr(Volume(5, 6, 7))
End Sub
Related Sections
Python - Learn the python from the basics up. This fast track example code course will get you creating powerful python programs in no time.Must Read Articles
VBA and Microsoft Excel - Getting the most out of excel with VBA programming
VBA and Microsoft Word - Getting the most out of word with VBA programming
Application Object - Using the applications features in your code.
Installing VBA and First Program - What do you need to start using visual basic application VBA.