Computer Science Canada

[Tutorial] Functions

Author:  MysticAngel [ Tue May 20, 2003 11:01 pm ]
Post subject:  [Tutorial] Functions

VB is really easy compared to turing. Cuz in VB all u have to do is stick in the buttons and all that stuff and just write the code for all those stuff.

first thing you got to do for every progam is write

Option Explicit

Then you carry on with your code

It's worth making very clear that a function always returns a single value and never more. Although a function's argument list can sometimes contain five or more arguments, a function never returns more than one value while procedures can have more than one ouput or none.

eg.
code:


 'This is a function that will return the name of the month depending on the month number.A function is written by its name followed by a byVal (always a value in functions) and its variables declared and declaring the function itself


Function MonthName(ByVal intMonth As Integer) As String

    If intMonth = 1 Then
        MonthName = "January"
    ElseIf intMonth = 2 Then
        MonthName = "Febuary"
    ElseIf intMonth = 3 Then
        MonthName = "March"
    ElseIf intMonth = 4 Then
        MonthName = "April"
    ElseIf intMonth = 5 Then
        MonthName = "May"
    ElseIf intMonth = 6 Then
        MonthName = "June"
    ElseIf intMonth = 7 Then
        MonthName = "July"
    ElseIf intMonth = 8 Then
        MonthName = "August"
    ElseIf intMonth = 9 Then
        MonthName = "September"
    ElseIf intMonth = 10 Then
        MonthName = "Ocotober"
    ElseIf intMonth = 11 Then
        MonthName = "November"
    ElseIf intMonth = 12 Then
        MonthName = "December"
    End If
End Function


'A function is usually ended by just saying end function.


'cmdDisplay is the name of the command button
'it declares another variable and then sets it to the input box (textbox)
'it then calls the function above and passes on the parameters and then then ouputs it in the picture box

Private Sub cmdDisplay_click ()
       Dim intMonth As Integer       
       intMonth = txtYear.Text
       picDate.Print MonthName (intMonth)
End Sub


In Any subprograms it is necessary and important that when you call the functions or procedures the parameters should match exactly the way you declared them. else you would get an error.

hope this is good enough.

Author:  Tony [ Wed May 21, 2003 9:22 am ]
Post subject: 

thx for the explanation on functions, I'm glad people finally post stuff on languages other then turing Very Happy +15Bits

Also could you explain what Option Explicit does?

Author:  MysticAngel [ Wed May 21, 2003 10:44 am ]
Post subject: 

Option Explicit explicitly declares all variables using the Dim, Private, Public, or ReDim statements. If you attempt to use an undeclared variable name, an error occurs at compile time.

If you don't use the Option Explicit statement, all undeclared variables are of Object type.

Option Explicit should be declared at the very beginning even before you delcare any global variabels (if any)

Author:  Tony [ Wed May 21, 2003 2:16 pm ]
Post subject: 

thx Smile Cuz I keep on getting those errors and then have to look for typoes in my variable names Laughing

Author:  MysticAngel [ Thu May 22, 2003 1:11 pm ]
Post subject: 

yeah, that used to happen to me most of the time,so i would go through the entire code and see where i have gone wrong and i would realize that i hadnt written the option explicit. Smile


: