Visual Basic Lesson 10:
So onto subroutines and functions... Wooh! Subroutines and functions are really useful pieces of coding, but before we talk about writing our own, we need to look at the standard ones which are offered by VB. On top of that we also need to define what exactly a subroutine is and what exactly a function is.
Firstly let us look at functions. Functions, like variables, still have to have a name, if they didn't then it would be impossible to distinguish between them. This all comes down to self-documenting code when writing your own, because a function name tells the programmer what the function does - to a certain extent anyway. Functions also have parameters. This sounds fairly fancy, but essentially the parameters are variables within brackets.
A standard example of a parameter:
(Variable)
There will be a more practical example of a parameter when it comes to an actual example function.
Another key feature of a function is that it must always return a value! Every single time. So because it has to return a value you must always have something on the left with an equals sign, with something else on the right.
For example:
Current = Now()
A further example of a function is:
Dim current As Date
Current = Now()
M = Month(Current)
Print Hour(Current), Minute (Current)
Two useful built in functions are the midfunction and the substring function.
Okay, now onto subroutines. Like functions, subroutines have to have a name and they can have parameters. But the difference is that subroutines may or may not return a value and the returned value doesn't have to be used as an assignment.
Here is an example of a program designed in class:
This program is designed to use the substring function in order to reverse a name which the user has inputted, in order to display it backwards.
And that's essentially built in subroutines and functions. Next time we will look at writing our own subroutines.
Bye!