• Good coding proc. parameter & intermediate value (All)

    Home » Forums » AskWoody support » Productivity software by function » MS Access and database help » Good coding proc. parameter & intermediate value (All)

    Author
    Topic
    #393583

    Hey,

    In my quest for writing better code, as I’m no real programmer, I didn’t find the exact information about the following… In case the answer on either of these questions is ‘yes’, I’ll be very glad with any suggestion, link to a thread, site or white paper where this is described properly…

    1. Suppose you have a procedure fed with one or more parameters. Are there any disadvantages if I use these parameters directly in code, instead of assigning them first to explicitly declared variables? I figured out that you loose performance when you don’t do this with form controls/fields, but I guess chances are small that this applies to parameters too.
    2. Are there any disadvantages if I use the function name also in intermediate steps during the procedure, instead of using variables as long as possible, and the function only in the last step?

    For example:
    Public Function function1(strName as String) as String
    function1 = function2(strDirName)
    function1 = LCase(function1)
    End Function

    Viewing 1 reply thread
    Author
    Replies
    • #715182

      When you pass some data to a procedure as an argument it is a variable. If passed by reference (the default in VB/VBA) the variable points back to the variable in the calling procedure. If passed by value (ByVal) the variable is passed as a copy of the variable. There is no need to turn around and assign this value to another variable unless there is a reason to do so. If you don’t want the called procedure to change the value of the variable referenced in the calling procedure, pass the argument by value.

      As for assiging values to Function names, according to VB Help for Function Statement: “To return a value from a function, assign the value to the function name. Any number of such assignments can appear anywhere within the procedure.” In other words there’s no rule that says you can only assign a value to a function name once, at end of the procedure.

      For more info, for a start, recommend open VB Help, select “Contents” pane and then check out “Visual Basic Conceptual Topics” topics, these may be useful in explaining how some of these concepts work in VB/VBA. Also recommend check out “Visual Basic Language Reference” topics (also under Contents), under “Statements” look up “Sub Statement”, “Function Statement”, and related topics. Not that VB Help Files are the most coherent reference available, but may serve as an good introduction to these topics.

      HTH

      • #715352

        Thanks Mark… you told me all I needed to know bow.
        And I’ll check out these help pages too. I seem to have skipped them a bit too easily …

      • #715353

        Thanks Mark… you told me all I needed to know bow.
        And I’ll check out these help pages too. I seem to have skipped them a bit too easily …

    • #715183

      When you pass some data to a procedure as an argument it is a variable. If passed by reference (the default in VB/VBA) the variable points back to the variable in the calling procedure. If passed by value (ByVal) the variable is passed as a copy of the variable. There is no need to turn around and assign this value to another variable unless there is a reason to do so. If you don’t want the called procedure to change the value of the variable referenced in the calling procedure, pass the argument by value.

      As for assiging values to Function names, according to VB Help for Function Statement: “To return a value from a function, assign the value to the function name. Any number of such assignments can appear anywhere within the procedure.” In other words there’s no rule that says you can only assign a value to a function name once, at end of the procedure.

      For more info, for a start, recommend open VB Help, select “Contents” pane and then check out “Visual Basic Conceptual Topics” topics, these may be useful in explaining how some of these concepts work in VB/VBA. Also recommend check out “Visual Basic Language Reference” topics (also under Contents), under “Statements” look up “Sub Statement”, “Function Statement”, and related topics. Not that VB Help Files are the most coherent reference available, but may serve as an good introduction to these topics.

      HTH

    Viewing 1 reply thread
    Reply To: Reply #715182 in Good coding proc. parameter & intermediate value (All)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information:




    Cancel