Tag: Functions

  • Functions: Passing by value and by reference

    This is really one of the simplest concepts ever, but since this is something that you need to know as well as something you can benefit from knowing, I think it can be worth a post. Consider this this code:

    Class MyProgram
    
        Shared Sub Main()
            Dim X As Integer = 10
            DoSomething(X)
            Console.WriteLine(X)
        End Sub
    
        Private Shared Sub DoSomething(ByVal X As Integer)
            X += 1
        End Sub
    
    End Class
    

    The output you get here is of course 10. X in DoSomething is different from X is Main, and the variable X in DoSomething is just initialized with the value 10, and then it is increased by one to 11 and thereafter it is destroyed, leaving X in main with the value 10. This is because the parameter is passed by value (ByVal).

    If I was to change the parameter in DoSomething to be passed by reference instead, as in the following code, the output would be 11.

    Class MyProgram
    
        Shared Sub Main()
            Dim X As Integer = 10
            DoSomething(X)
            Console.WriteLine(X)
        End Sub
    
        Private Shared Sub DoSomething(ByRef X As Integer)
            X += 1
        End Sub
    
    End Class
    

    This is because DoSomething no longer holds an own integer variable, just a reference to the variable that is passed by the caller, Main. When X in Main is passed to DoSomething, and DoSomething is manipulating that value, the value is changed.

    When I work with objects, the rules are the same, but the effect is different. If I was to pass a bitmap to a function, and the function then is manipulated, the original bitmap will be affected no matter if it is passed by value or by reference (ByRef). This is because the value of the bitmap object variable is a reference to the actual object, and a reference to a object variable also is a reference to the same actual object.

    Consider this code:

    Class MyProgram
    
        Shared Sub Main()
            Dim B As New System.Drawing.Bitmap(100, 100)
            DoSomething(B)
            Console.WriteLine(B.Width.ToString())
        End Sub
    
        Private Shared Sub DoSomething(ByVal B As System.Drawing.Image)
            B = New System.Drawing.Bitmap(200, 200)
        End Sub
    
    End Class
    

    The output will be 100, because when you change the value of an object variable (the value of an object variable is the reference to the actual object) the passing variable is not affected, because the code holds two variables: B in Main and B in DoSomething. Changing the value of B in DoSomething will not affect B in Main.

    Changing the ByVal in DoSomething to ByRef, would give an output of 200, since the code then only would contain one variable: B in Main. DoSomething will simply refer to that variable.

  • Functions: Shadowing

    Over the years, functions has grown to become a quite complex and flexible feature in Visual Basic. The striking flexibility of the parameters, the modifiers that can apply a function and the function attributes, gives you great possibilities when you are coding. Some of the techniques include multiple return values and recursion and some of the features include passing by value, passing by reference, parameter arrays and shadowing. Shadowing is a feature that I find very useful when designing dialogs.

    The ShowDialog function, that is inherited from the Form class, is a common subject of shadowing for me. It holds two overloads, one parameterless and one that takes the owner form. Shadowing lets you create a new version of a function, no matter if it is overridable or not, and at the same time block all current overloads. So any form that I want to add a mandatory parameter to, I shadow the ShowDialog function. This is a typical function declaration in my Windows applications:

    Public Shadows Function ShowDialog(ByVal Owner As IWin32Window, _
    ByVal CustomerID As Integer) As DialogResult

    By automagic, the current versions of ShowDialog in my form disappears, and this function (the function that does the shadowing) is the one that can be called to display the form as a dialog. A implementation could look like this:

    Public Shadows Function ShowDialog(ByVal Owner As IWin32Window, _
    ByVal CustomerID As Integer) As DialogResult
       Me.mCustomerID = CustomerID
       Return MyBase.ShowDialog(Owner)
    End Function

    The initialization would then be done in the Load event.

    Shadowing is one of the killer features that from what I know is quite unique for Visual Basic.

  • Defining and calling functions

    You can define your own functions in a PowerShell script file (PS1-file) or directly from the PowerShell console. To do this, use the keyword function, followed by the definition encapsulated in curly brackets. This code creates a function that returns a string containing the phrase “String returned from a function!”:

    function myFunction { “String returned from a function!” }

    To call this function, simply type myFunction and press Enter. A tip for avoiding typing long lines, you do line breaks by pressing Shift+Enter after the first line. PowerShell then enters a line editing mode. To send the text to PowerShell end exit the line editor, just pres Enter twice. PowerShell might look like this after entering and leaving the line editor:

    PS H:\> function myFunction
    >> {
    >> “String returned from a function!”
    >> }
    >>

    If the function you’re designing requires parameters, you can declare them in a comma separated list directly after the function name. This line defines the sum function that takes two integer parameters and returns the sum of the given values.

    function sum([int]$p1, [int]$p2) { $p1+$p2 }

    To call a function with parameters, enter the function name followed by the parameter values (constants or variables) separated by nothing but a whitespace.

    sum 10 20

    To nest the function, encapsulate it in parenthesis. This call returns the value 46:

    sum (sum 10 15) 21